Can fetch related channels using just the channel ucid]

This commit is contained in:
matthewmcgarvey 2022-01-10 17:07:52 -06:00
parent 4962c00ba8
commit dc62b6ffdb
2 changed files with 16 additions and 33 deletions

View file

@ -12,13 +12,8 @@ record AboutChannel,
joined : Time,
is_family_friendly : Bool,
allowed_regions : Array(String),
related_channels : RelatedChannels?,
tabs : Array(String)
record RelatedChannels,
browse_id : String,
params : String?
record AboutRelatedChannel,
ucid : String,
author : String,
@ -47,7 +42,6 @@ def get_about_info(ucid, locale) : AboutChannel
auto_generated = true
end
related_channels = nil
if auto_generated
author = initdata["header"]["interactiveTabbedHeaderRenderer"]["title"]["simpleText"].as_s
author_url = initdata["microformat"]["microformatDataRenderer"]["urlCanonical"].as_s
@ -82,15 +76,6 @@ def get_about_info(ucid, locale) : AboutChannel
is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool
allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s)
tabs = initdata.dig("contents", "twoColumnBrowseResultsRenderer", "tabs").as_a
if tab = tabs.find { |tab| tab.dig?("tabRenderer", "title").try(&.as_s?) == "Channels" }
browse_id = tab.dig?("tabRenderer", "endpoint", "browseEndpoint", "browseId").try(&.as_s?)
params = tab.dig?("tabRenderer", "endpoint", "browseEndpoint", "params").try(&.as_s?)
if browse_id
related_channels = RelatedChannels.new(browse_id: browse_id, params: params)
end
end
end
total_views = 0_i64
@ -142,13 +127,13 @@ def get_about_info(ucid, locale) : AboutChannel
joined: joined,
is_family_friendly: is_family_friendly,
allowed_regions: allowed_regions,
related_channels: related_channels,
tabs: tabs,
)
end
def fetch_related_channels(related_channels : RelatedChannels) : Array(AboutRelatedChannel)
channels = YoutubeAPI.browse(browse_id: related_channels.browse_id, params: related_channels.params || "")
def fetch_related_channels(about_channel : AboutChannel) : Array(AboutRelatedChannel)
# params is {"2:string":"channels"} encoded
channels = YoutubeAPI.browse(browse_id: about_channel.ucid, params: "EghjaGFubmVscw%3D%3D")
tabs = channels.dig?("contents", "twoColumnBrowseResultsRenderer", "tabs").try(&.as_a?) || [] of JSON::Any
tab = tabs.find { |tab| tab.dig?("tabRenderer", "title").try(&.as_s?) == "Channels" }

View file

@ -96,23 +96,21 @@ module Invidious::Routes::API::V1::Channels
json.field "relatedChannels" do
json.array do
if related_channels = channel.related_channels
fetch_related_channels(related_channels).each do |related_channel|
json.object do
json.field "author", related_channel.author
json.field "authorId", related_channel.ucid
json.field "authorUrl", related_channel.author_url
fetch_related_channels(channel).each do |related_channel|
json.object do
json.field "author", related_channel.author
json.field "authorId", related_channel.ucid
json.field "authorUrl", related_channel.author_url
json.field "authorThumbnails" do
json.array do
qualities = {32, 48, 76, 100, 176, 512}
json.field "authorThumbnails" do
json.array do
qualities = {32, 48, 76, 100, 176, 512}
qualities.each do |quality|
json.object do
json.field "url", related_channel.author_thumbnail.gsub(/=\d+/, "=s#{quality}")
json.field "width", quality
json.field "height", quality
end
qualities.each do |quality|
json.object do
json.field "url", related_channel.author_thumbnail.gsub(/=\d+/, "=s#{quality}")
json.field "width", quality
json.field "height", quality
end
end
end