Use the youtubei API over the legacy one

This commit is contained in:
Svallinn 2021-03-24 05:33:45 +00:00
parent c481ca924b
commit cbdba66ef3
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
2 changed files with 18 additions and 14 deletions

View File

@ -1699,7 +1699,7 @@ get "/channel/:ucid" do |env|
sort_options = {"last", "oldest", "newest"} sort_options = {"last", "oldest", "newest"}
sort_by ||= "last" sort_by ||= "last"
items, continuation = fetch_channel_playlists(channel.ucid, channel.author, channel.auto_generated, continuation, sort_by) items, continuation = fetch_channel_playlists(channel.ucid, channel.author, continuation, sort_by)
items.uniq! do |item| items.uniq! do |item|
if item.responds_to?(:title) if item.responds_to?(:title)
item.title item.title
@ -1766,7 +1766,7 @@ get "/channel/:ucid/playlists" do |env|
next env.redirect "/channel/#{channel.ucid}" next env.redirect "/channel/#{channel.ucid}"
end end
items, continuation = fetch_channel_playlists(channel.ucid, channel.author, channel.auto_generated, continuation, sort_by) items, continuation = fetch_channel_playlists(channel.ucid, channel.author, continuation, sort_by)
items = items.select { |item| item.is_a?(SearchPlaylist) }.map { |item| item.as(SearchPlaylist) } items = items.select { |item| item.is_a?(SearchPlaylist) }.map { |item| item.as(SearchPlaylist) }
items.each { |item| item.author = "" } items.each { |item| item.author = "" }
@ -2467,7 +2467,7 @@ end
next error_json(500, ex) next error_json(500, ex)
end end
items, continuation = fetch_channel_playlists(channel.ucid, channel.author, channel.auto_generated, continuation, sort_by) items, continuation = fetch_channel_playlists(channel.ucid, channel.author, continuation, sort_by)
JSON.build do |json| JSON.build do |json|
json.object do json.object do

View File

@ -355,14 +355,19 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)
return channel return channel
end end
def fetch_channel_playlists(ucid, author, auto_generated, continuation, sort_by) def fetch_channel_playlists(ucid, author, continuation, sort_by)
if continuation || auto_generated if continuation
url = produce_channel_playlists_url(ucid, continuation, sort_by, auto_generated) response_json = request_youtube_api_browse(continuation)
result = JSON.parse(response_json.match(/"continuationItems": (?<items>\[.*\]),/m).try &.["items"] || "{}")
response = YT_POOL.client &.get(url) return [] of SearchItem, nil if result.size == 0
continuation = response.body.match(/"continuation":"(?<continuation>[^"]+)"/).try &.["continuation"]? items = [] of SearchItem
initial_data = JSON.parse(response.body).as_a.find(&.["response"]?).try &.as_h result.as_a.select(&.as_h.has_key?("gridPlaylistRenderer")).each { |item|
extract_item(item, author, ucid).try { |t| items << t }
}
continuation = result.as_a.last["continuationItemRenderer"]?.try &.["continuationEndpoint"]["continuationCommand"]["token"].as_s
else else
url = "/channel/#{ucid}/playlists?flow=list&view=1" url = "/channel/#{ucid}/playlists?flow=list&view=1"
@ -377,13 +382,12 @@ def fetch_channel_playlists(ucid, author, auto_generated, continuation, sort_by)
end end
response = YT_POOL.client &.get(url) response = YT_POOL.client &.get(url)
continuation = response.body.match(/"continuation":"(?<continuation>[^"]+)"/).try &.["continuation"]?
initial_data = extract_initial_data(response.body) initial_data = extract_initial_data(response.body)
end return [] of SearchItem, nil if !initial_data
return [] of SearchItem, nil if !initial_data items = extract_items(initial_data, author, ucid)
items = extract_items(initial_data) continuation = response.body.match(/"token":"(?<continuation>[^"]+)"/).try &.["continuation"]?
continuation = extract_channel_playlists_cursor(continuation, auto_generated) if continuation end
return items, continuation return items, continuation
end end