Add channel sort options

This commit is contained in:
Omar Roth 2018-11-13 19:04:25 -06:00
parent 9aeb9ec00f
commit 44e9b4ac2a
3 changed files with 48 additions and 10 deletions

View File

@ -1889,6 +1889,9 @@ get "/channel/:ucid" do |env|
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
sort_by = env.params.query["sort_by"]?.try &.downcase
sort_by ||= "newest"
begin begin
author, ucid, auto_generated, sub_count = get_about_info(ucid) author, ucid, auto_generated, sub_count = get_about_info(ucid)
rescue ex rescue ex
@ -1904,7 +1907,7 @@ get "/channel/:ucid" do |env|
end end
end end
videos, count = get_60_videos(ucid, page, auto_generated) videos, count = get_60_videos(ucid, page, auto_generated, sort_by)
templated "channel" templated "channel"
end end
@ -2432,6 +2435,8 @@ get "/api/v1/channels/:ucid" do |env|
env.response.content_type = "application/json" env.response.content_type = "application/json"
ucid = env.params.url["ucid"] ucid = env.params.url["ucid"]
sort_by = env.params.query["sort_by"]?.try &.downcase
sort_by ||= "newest"
begin begin
author, ucid, auto_generated = get_about_info(ucid) author, ucid, auto_generated = get_about_info(ucid)
@ -2442,7 +2447,7 @@ get "/api/v1/channels/:ucid" do |env|
page = 1 page = 1
begin begin
videos, count = get_60_videos(ucid, page, auto_generated) videos, count = get_60_videos(ucid, page, auto_generated, sort_by)
rescue ex rescue ex
error_message = {"error" => ex.message}.to_json error_message = {"error" => ex.message}.to_json
halt env, status_code: 500, response: error_message halt env, status_code: 500, response: error_message

View File

@ -163,7 +163,7 @@ def fetch_channel(ucid, client, db, pull_all_videos = true)
return channel return channel
end 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 if auto_generated
seed = Time.unix(1525757349) 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.size.to_u8.unsafe_chr
meta += page 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 = Base64.urlsafe_encode(meta)
meta = URI.escape(meta) meta = URI.escape(meta)
@ -254,14 +264,14 @@ def get_about_info(ucid)
return {author, ucid, auto_generated, sub_count} return {author, ucid, auto_generated, sub_count}
end end
def get_60_videos(ucid, page, auto_generated) def get_60_videos(ucid, page, auto_generated, sort_by = "newest")
count = 0 count = 0
videos = [] of SearchVideo videos = [] of SearchVideo
client = make_client(YT_URL) client = make_client(YT_URL)
2.times do |i| 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) response = client.get(url)
json = JSON.parse(response.body) json = JSON.parse(response.body)

View File

@ -40,9 +40,32 @@
<% end %> <% end %>
</div> </div>
<p class="h-box"> <div class="pure-g h-box">
<div class="pure-u-1-3">
<a href="https://www.youtube.com/channel/<%= ucid %>">View channel on YouTube</a> <a href="https://www.youtube.com/channel/<%= ucid %>">View channel on YouTube</a>
</p> </div>
<div class="pure-u-1-3">
</div>
<div class="pure-u-1-3">
<div class="pure-g" style="text-align:right;">
<% {"newest", "oldest", "popular"}.each do |sort| %>
<div class="pure-u-1-3">
<% if sort_by == sort %>
<b><%= sort %></b>
<% else %>
<a href="/channel/<%= ucid %>?page=<%= page %>&sort_by=<%= sort %>">
<%= sort %>
</a>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
<div class="h-box">
<hr>
</div>
<% videos.each_slice(4) do |slice| %> <% videos.each_slice(4) do |slice| %>
<div class="pure-g"> <div class="pure-g">
@ -55,13 +78,13 @@
<div class="pure-g h-box"> <div class="pure-g h-box">
<div class="pure-u-1 pure-u-md-1-5"> <div class="pure-u-1 pure-u-md-1-5">
<% if page >= 2 %> <% if page >= 2 %>
<a href="/channel/<%= ucid %>?page=<%= page - 1 %>">Previous page</a> <a href="/channel/<%= ucid %>?page=<%= page - 1 %><% if sort_by != "newest" %>&sort_by=<%= sort_by %><% end %>">Previous page</a>
<% end %> <% end %>
</div> </div>
<div class="pure-u-1 pure-u-md-3-5"></div> <div class="pure-u-1 pure-u-md-3-5"></div>
<div style="text-align:right;" class="pure-u-1 pure-u-md-1-5"> <div style="text-align:right;" class="pure-u-1 pure-u-md-1-5">
<% if count == 60 %> <% if count == 60 %>
<a href="/channel/<%= ucid %>?page=<%= page + 1 %>">Next page</a> <a href="/channel/<%= ucid %>?page=<%= page + 1 %><% if sort_by != "newest" %>&sort_by=<%= sort_by %><% end %>">Next page</a>
<% end %> <% end %>
</div> </div>
</div> </div>