From 44e9b4ac2a11db5b7202f1f3cec61469f35d1567 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Tue, 13 Nov 2018 19:04:25 -0600 Subject: [PATCH] Add channel sort options --- src/invidious.cr | 9 +++++++-- src/invidious/channels.cr | 16 +++++++++++++--- src/invidious/views/channel.ecr | 33 ++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 96af04fa..83824869 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1889,6 +1889,9 @@ get "/channel/:ucid" do |env| page = env.params.query["page"]?.try &.to_i? page ||= 1 + sort_by = env.params.query["sort_by"]?.try &.downcase + sort_by ||= "newest" + begin author, ucid, auto_generated, sub_count = get_about_info(ucid) rescue ex @@ -1904,7 +1907,7 @@ get "/channel/:ucid" do |env| end end - videos, count = get_60_videos(ucid, page, auto_generated) + videos, count = get_60_videos(ucid, page, auto_generated, sort_by) templated "channel" end @@ -2432,6 +2435,8 @@ get "/api/v1/channels/:ucid" do |env| env.response.content_type = "application/json" ucid = env.params.url["ucid"] + sort_by = env.params.query["sort_by"]?.try &.downcase + sort_by ||= "newest" begin author, ucid, auto_generated = get_about_info(ucid) @@ -2442,7 +2447,7 @@ get "/api/v1/channels/:ucid" do |env| page = 1 begin - videos, count = get_60_videos(ucid, page, auto_generated) + videos, count = get_60_videos(ucid, page, auto_generated, sort_by) rescue ex error_message = {"error" => ex.message}.to_json halt env, status_code: 500, response: error_message diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index dcab5e29..f713d97b 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -163,7 +163,7 @@ def fetch_channel(ucid, client, db, pull_all_videos = true) return channel end -def produce_channel_videos_url(ucid, page = 1, auto_generated = nil) +def produce_channel_videos_url(ucid, page = 1, auto_generated = nil, sort_by = "newest") if auto_generated seed = Time.unix(1525757349) @@ -190,6 +190,16 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil) meta += page.size.to_u8.unsafe_chr meta += page + case sort_by + when "newest" + # Empty tags can be omitted + # meta += "\x18\x00" + when "popular" + meta += "\x18\x01" + when "oldest" + meta += "\x18\x02" + end + meta = Base64.urlsafe_encode(meta) meta = URI.escape(meta) @@ -254,14 +264,14 @@ def get_about_info(ucid) return {author, ucid, auto_generated, sub_count} end -def get_60_videos(ucid, page, auto_generated) +def get_60_videos(ucid, page, auto_generated, sort_by = "newest") count = 0 videos = [] of SearchVideo client = make_client(YT_URL) 2.times do |i| - url = produce_channel_videos_url(ucid, page * 2 + (i - 1), auto_generated: auto_generated) + url = produce_channel_videos_url(ucid, page * 2 + (i - 1), auto_generated: auto_generated, sort_by: sort_by) response = client.get(url) json = JSON.parse(response.body) diff --git a/src/invidious/views/channel.ecr b/src/invidious/views/channel.ecr index b9b75ef5..448f97e5 100644 --- a/src/invidious/views/channel.ecr +++ b/src/invidious/views/channel.ecr @@ -40,9 +40,32 @@ <% end %> -

- View channel on YouTube -

+
+ +
+
+
+
+ <% {"newest", "oldest", "popular"}.each do |sort| %> +
+ <% if sort_by == sort %> + <%= sort %> + <% else %> + + <%= sort %> + + <% end %> +
+ <% end %> +
+
+
+ +
+
+
<% videos.each_slice(4) do |slice| %>
@@ -55,13 +78,13 @@