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, joined : Time,
is_family_friendly : Bool, is_family_friendly : Bool,
allowed_regions : Array(String), allowed_regions : Array(String),
related_channels : RelatedChannels?,
tabs : Array(String) tabs : Array(String)
record RelatedChannels,
browse_id : String,
params : String?
record AboutRelatedChannel, record AboutRelatedChannel,
ucid : String, ucid : String,
author : String, author : String,
@ -47,7 +42,6 @@ def get_about_info(ucid, locale) : AboutChannel
auto_generated = true auto_generated = true
end end
related_channels = nil
if auto_generated if auto_generated
author = initdata["header"]["interactiveTabbedHeaderRenderer"]["title"]["simpleText"].as_s author = initdata["header"]["interactiveTabbedHeaderRenderer"]["title"]["simpleText"].as_s
author_url = initdata["microformat"]["microformatDataRenderer"]["urlCanonical"].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 is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool
allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s) 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 end
total_views = 0_i64 total_views = 0_i64
@ -142,13 +127,13 @@ def get_about_info(ucid, locale) : AboutChannel
joined: joined, joined: joined,
is_family_friendly: is_family_friendly, is_family_friendly: is_family_friendly,
allowed_regions: allowed_regions, allowed_regions: allowed_regions,
related_channels: related_channels,
tabs: tabs, tabs: tabs,
) )
end end
def fetch_related_channels(related_channels : RelatedChannels) : Array(AboutRelatedChannel) def fetch_related_channels(about_channel : AboutChannel) : Array(AboutRelatedChannel)
channels = YoutubeAPI.browse(browse_id: related_channels.browse_id, params: related_channels.params || "") # 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 tabs = channels.dig?("contents", "twoColumnBrowseResultsRenderer", "tabs").try(&.as_a?) || [] of JSON::Any
tab = tabs.find { |tab| tab.dig?("tabRenderer", "title").try(&.as_s?) == "Channels" } tab = tabs.find { |tab| tab.dig?("tabRenderer", "title").try(&.as_s?) == "Channels" }

View File

@ -96,8 +96,7 @@ module Invidious::Routes::API::V1::Channels
json.field "relatedChannels" do json.field "relatedChannels" do
json.array do json.array do
if related_channels = channel.related_channels fetch_related_channels(channel).each do |related_channel|
fetch_related_channels(related_channels).each do |related_channel|
json.object do json.object do
json.field "author", related_channel.author json.field "author", related_channel.author
json.field "authorId", related_channel.ucid json.field "authorId", related_channel.ucid
@ -123,7 +122,6 @@ module Invidious::Routes::API::V1::Channels
end end
end end
end end
end
def self.latest(env) def self.latest(env)
locale = env.get("preferences").as(Preferences).locale locale = env.get("preferences").as(Preferences).locale