From a19cdb5e7251cb5b51d08e0c844119d8267440b3 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Fri, 16 Aug 2019 15:46:37 -0500 Subject: [PATCH] Fix season playlists --- src/invidious.cr | 40 +++++++++++++++++++++++-- src/invidious/helpers/helpers.cr | 30 +++++++------------ src/invidious/search.cr | 14 ++++----- src/invidious/views/components/item.ecr | 4 +-- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 712a408f..21d6544d 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -3047,8 +3047,7 @@ get "/channel/:ucid" do |env| item.author end end - items.select! { |item| item.responds_to?(:thumbnail_id) && item.thumbnail_id } - items = items.map { |item| item.as(SearchPlaylist) } + items = items.select { |item| item.is_a?(SearchPlaylist) }.map { |item| item.as(SearchPlaylist) } items.each { |item| item.author = "" } else sort_options = {"newest", "oldest", "popular"} @@ -5086,6 +5085,43 @@ get "/sb/:id/:storyboard/:index" do |env| end end +get "/s_p/:id/:name" do |env| + id = env.params.url["id"] + name = env.params.url["name"] + + host = "https://i9.ytimg.com" + client = make_client(URI.parse(host)) + url = env.request.resource + + headers = HTTP::Headers.new + REQUEST_HEADERS_WHITELIST.each do |header| + if env.request.headers[header]? + headers[header] = env.request.headers[header] + end + end + + begin + client.get(url, headers) do |response| + env.response.status_code = response.status_code + response.headers.each do |key, value| + if !RESPONSE_HEADERS_BLACKLIST.includes? key + env.response.headers[key] = value + end + end + + env.response.headers["Access-Control-Allow-Origin"] = "*" + + if response.status_code >= 300 && response.status_code != 404 + env.response.headers.delete("Transfer-Encoding") + break + end + + proxy_file(response, env) + end + rescue ex + end +end + get "/vi/:id/:name" do |env| id = env.params.url["id"] name = env.params.url["name"] diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 03c1654c..e018e567 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -312,8 +312,7 @@ end def extract_videos(nodeset, ucid = nil, author_name = nil) videos = extract_items(nodeset, ucid, author_name) - videos.select! { |item| !item.is_a?(SearchChannel | SearchPlaylist) } - videos.map { |video| video.as(SearchVideo) } + videos.select { |item| item.is_a?(SearchVideo) }.map { |video| video.as(SearchVideo) } end def extract_items(nodeset, ucid = nil, author_name = nil) @@ -361,14 +360,14 @@ def extract_items(nodeset, ucid = nil, author_name = nil) anchor = node.xpath_node(%q(.//ul[@class="yt-lockup-meta-info"]/li/a)) end - video_count = node.xpath_node(%q(.//span[@class="formatted-video-count-label"]/b)) + video_count = node.xpath_node(%q(.//span[@class="formatted-video-count-label"]/b)) || + node.xpath_node(%q(.//span[@class="formatted-video-count-label"])) if video_count video_count = video_count.content if video_count == "50+" author = "YouTube" author_id = "UC-9-kyTW8ZkZNDHQJ6FgpwQ" - video_count = video_count.rchop("+") end video_count = video_count.gsub(/\D/, "").to_i? @@ -400,11 +399,6 @@ def extract_items(nodeset, ucid = nil, author_name = nil) playlist_thumbnail = node.xpath_node(%q(.//div/span/img)).try &.["data-thumb"]? playlist_thumbnail ||= node.xpath_node(%q(.//div/span/img)).try &.["src"] - if !playlist_thumbnail || playlist_thumbnail.empty? - thumbnail_id = videos[0]?.try &.id - else - thumbnail_id = playlist_thumbnail.match(/\/vi\/(?[a-zA-Z0-9_-]{11})\/\w+\.jpg/).try &.["video_id"] - end items << SearchPlaylist.new( title, @@ -413,7 +407,7 @@ def extract_items(nodeset, ucid = nil, author_name = nil) author_id, video_count, videos, - thumbnail_id + playlist_thumbnail ) when .includes? "yt-lockup-channel" author = title.strip @@ -586,15 +580,11 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil) playlist_thumbnail = child_node.xpath_node(%q(.//span/img)).try &.["data-thumb"]? playlist_thumbnail ||= child_node.xpath_node(%q(.//span/img)).try &.["src"] - if !playlist_thumbnail || playlist_thumbnail.empty? - thumbnail_id = videos[0]?.try &.id - else - thumbnail_id = playlist_thumbnail.match(/\/vi\/(?[a-zA-Z0-9_-]{11})\/\w+\.jpg/).try &.["video_id"] - end - video_count_label = child_node.xpath_node(%q(.//span[@class="formatted-video-count-label"])) - if video_count_label - video_count = video_count_label.content.gsub(/\D/, "").to_i? + video_count = child_node.xpath_node(%q(.//span[@class="formatted-video-count-label"]/b)) || + child_node.xpath_node(%q(.//span[@class="formatted-video-count-label"])) + if video_count + video_count = video_count.content.gsub(/\D/, "").to_i? end video_count ||= 50 @@ -605,7 +595,7 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil) ucid, video_count, Array(SearchPlaylistVideo).new, - thumbnail_id + playlist_thumbnail ) end end @@ -620,7 +610,7 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil) ucid, videos.size, videos, - videos[0].try &.id + "/vi/#{videos[0].id}/mqdefault.jpg" ) end end diff --git a/src/invidious/search.cr b/src/invidious/search.cr index 1d4805bf..784e3897 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -152,13 +152,13 @@ struct SearchPlaylist end db_mapping({ - title: String, - id: String, - author: String, - ucid: String, - video_count: Int32, - videos: Array(SearchPlaylistVideo), - thumbnail_id: String?, + title: String, + id: String, + author: String, + ucid: String, + video_count: Int32, + videos: Array(SearchPlaylistVideo), + thumbnail: String?, }) end diff --git a/src/invidious/views/components/item.ecr b/src/invidious/views/components/item.ecr index 28e70058..71ae70df 100644 --- a/src/invidious/views/components/item.ecr +++ b/src/invidious/views/components/item.ecr @@ -15,7 +15,7 @@
<%= item.description_html %>
<% when SearchPlaylist %> <% if item.id.starts_with? "RD" %> - <% url = "/mix?list=#{item.id}&continuation=#{item.thumbnail_id}" %> + <% url = "/mix?list=#{item.id}&continuation=#{URI.parse(item.thumbnail || "/vi/-----------").full_path.split("/")[2]}" %> <% else %> <% url = "/playlist?list=#{item.id}" %> <% end %> @@ -23,7 +23,7 @@ <% if !env.get("preferences").as(Preferences).thin_mode %>
- + "/>

<%= number_with_separator(item.video_count) %> videos

<% end %>