summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2024-07-10 22:28:22 +0200
committerSamantaz Fox <coding@samantaz.fr>2024-07-10 22:28:22 +0200
commitbad92093bff66bfb0281e5276fa0e136a61ba330 (patch)
tree30a1929281bf2b084593881b7be790e9c5895e14
parent436a61e3bbcc39c43e459a991301b2eff60296b4 (diff)
parent72fe8af850b3dbddbdd3c60cf7e152a78a995859 (diff)
downloadinvidious-bad92093bff66bfb0281e5276fa0e136a61ba330.tar.gz
invidious-bad92093bff66bfb0281e5276fa0e136a61ba330.tar.bz2
invidious-bad92093bff66bfb0281e5276fa0e136a61ba330.zip
Channels: Add sort options to streams (#4224)
-rw-r--r--src/invidious/channels/videos.cr38
-rw-r--r--src/invidious/routes/api/v1/channels.cr3
-rw-r--r--src/invidious/routes/channels.cr7
3 files changed, 26 insertions, 22 deletions
diff --git a/src/invidious/channels/videos.cr b/src/invidious/channels/videos.cr
index 351790d7..6cc30142 100644
--- a/src/invidious/channels/videos.cr
+++ b/src/invidious/channels/videos.cr
@@ -1,4 +1,4 @@
-def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, sort_by = "newest", v2 = false)
+def produce_channel_content_continuation(ucid, content_type, page = 1, auto_generated = nil, sort_by = "newest", v2 = false)
object_inner_2 = {
"2:0:embedded" => {
"1:0:varint" => 0_i64,
@@ -16,6 +16,13 @@ def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, so
.try { |i| Base64.urlsafe_encode(i) }
.try { |i| URI.encode_www_form(i) }
+ content_type_numerical =
+ case content_type
+ when "videos" then 15
+ when "livestreams" then 14
+ else 15 # Fallback to "videos"
+ end
+
sort_by_numerical =
case sort_by
when "newest" then 1_i64
@@ -27,7 +34,7 @@ def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, so
object_inner_1 = {
"110:embedded" => {
"3:embedded" => {
- "15:embedded" => {
+ "#{content_type_numerical}:embedded" => {
"1:embedded" => {
"1:string" => object_inner_2_encoded,
},
@@ -62,6 +69,10 @@ def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, so
return continuation
end
+def make_initial_content_ctoken(ucid, content_type, sort_by) : String
+ return produce_channel_content_continuation(ucid, content_type, sort_by: sort_by)
+end
+
module Invidious::Channel::Tabs
extend self
@@ -69,10 +80,6 @@ module Invidious::Channel::Tabs
# Regular videos
# -------------------
- def make_initial_video_ctoken(ucid, sort_by) : String
- return produce_channel_videos_continuation(ucid, sort_by: sort_by)
- end
-
# Wrapper for AboutChannel, as we still need to call get_videos with
# an author name and ucid directly (e.g in RSS feeds).
# TODO: figure out how to get rid of that
@@ -94,7 +101,7 @@ module Invidious::Channel::Tabs
end
def get_videos(author : String, ucid : String, *, continuation : String? = nil, sort_by = "newest")
- continuation ||= make_initial_video_ctoken(ucid, sort_by)
+ continuation ||= make_initial_content_ctoken(ucid, "videos", sort_by)
initial_data = YoutubeAPI.browse(continuation: continuation)
return extract_items(initial_data, author, ucid)
@@ -138,21 +145,18 @@ module Invidious::Channel::Tabs
# Livestreams
# -------------------
- def get_livestreams(channel : AboutChannel, continuation : String? = nil)
- if continuation.nil?
- # EgdzdHJlYW1z8gYECgJ6AA%3D%3D is the protobuf object to load "streams"
- initial_data = YoutubeAPI.browse(channel.ucid, params: "EgdzdHJlYW1z8gYECgJ6AA%3D%3D")
- else
- initial_data = YoutubeAPI.browse(continuation: continuation)
- end
+ def get_livestreams(channel : AboutChannel, continuation : String? = nil, sort_by = "newest")
+ continuation ||= make_initial_content_ctoken(channel.ucid, "livestreams", sort_by)
+
+ initial_data = YoutubeAPI.browse(continuation: continuation)
return extract_items(initial_data, channel.author, channel.ucid)
end
- def get_60_livestreams(channel : AboutChannel, continuation : String? = nil)
+ def get_60_livestreams(channel : AboutChannel, *, continuation : String? = nil, sort_by = "newest")
if continuation.nil?
- # Fetch the first "page" of streams
- items, next_continuation = get_livestreams(channel)
+ # Fetch the first "page" of stream
+ items, next_continuation = get_livestreams(channel, sort_by: sort_by)
else
# Fetch a "page" of streams using the given continuation token
items, next_continuation = get_livestreams(channel, continuation: continuation)
diff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.cr
index 7faf200a..43a5c35b 100644
--- a/src/invidious/routes/api/v1/channels.cr
+++ b/src/invidious/routes/api/v1/channels.cr
@@ -208,11 +208,12 @@ module Invidious::Routes::API::V1::Channels
get_channel()
# Retrieve continuation from URL parameters
+ sort_by = env.params.query["sort_by"]?.try &.downcase || "newest"
continuation = env.params.query["continuation"]?
begin
videos, next_continuation = Channel::Tabs.get_60_livestreams(
- channel, continuation: continuation
+ channel, continuation: continuation, sort_by: sort_by
)
rescue ex
return error_json(500, ex)
diff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.cr
index fea49bbe..360af2cd 100644
--- a/src/invidious/routes/channels.cr
+++ b/src/invidious/routes/channels.cr
@@ -81,13 +81,12 @@ module Invidious::Routes::Channels
return env.redirect "/channel/#{channel.ucid}"
end
- # TODO: support sort option for livestreams
- sort_by = ""
- sort_options = [] of String
+ sort_by = env.params.query["sort_by"]?.try &.downcase || "newest"
+ sort_options = {"newest", "oldest", "popular"}
# Fetch items and continuation token
items, next_continuation = Channel::Tabs.get_60_livestreams(
- channel, continuation: continuation
+ channel, continuation: continuation, sort_by: sort_by
)
selected_tab = Frontend::ChannelPage::TabsAvailable::Streams