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 ||= 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

View file

@ -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)

View file

@ -40,9 +40,32 @@
<% end %>
</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>
</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| %>
<div class="pure-g">
@ -55,13 +78,13 @@
<div class="pure-g h-box">
<div class="pure-u-1 pure-u-md-1-5">
<% 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 %>
</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">
<% 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 %>
</div>
</div>