Add logic to parse external chan links and country

(cherry picked from commit 90bd19dc45)
This commit is contained in:
syeopite 2021-04-13 17:45:57 -07:00
parent 0ca032171e
commit 2ed4a24dbc
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82

View file

@ -9,6 +9,7 @@ struct AboutChannel
property author_thumbnail : String
property banner : String?
property description_html : String
property country : String
property total_views : Int64
property sub_count : Int32
property joined : Time
@ -16,6 +17,7 @@ struct AboutChannel
property allowed_regions : Array(String)
property related_channels : Array(AboutRelatedChannel)
property tabs : Array(String)
property links : Array(Tuple(String, String))
end
struct AboutRelatedChannel
@ -119,10 +121,11 @@ def get_about_info(ucid, locale)
related_channels ||= [] of AboutRelatedChannel
end
country = ""
total_views = 0_i64
joined = Time.unix(0)
tabs = [] of String
links = [] of {String, String}
tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?.try &.as_a?
if !tabs_json.nil?
@ -140,6 +143,26 @@ def get_about_info(ucid, locale)
joined = channel_about_meta["joinedDateText"]?.try &.["runs"]?.try &.as_a.reduce("") { |acc, node| acc + node["text"].as_s }
.try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)
channel_about_meta["primaryLinks"]?.try &.as_a.each do |link|
link_title = link["title"]["simpleText"].as_s
link_url = URI.parse(link["navigationEndpoint"]["urlEndpoint"]["url"].to_s)
if {"m.youtube.com", "www.youtube.com", "youtu.be"}.includes? link_url.host
if link_url.path == "/redirect"
link_url = HTTP::Params.parse(link_url.query.not_nil!)["q"]
else
link_url = link_url.request_target.to_s
end
else
link_url = link_url.to_s
end
links << {link_title, link_url}
end
country = channel_about_meta["country"]?.try &.["simpleText"].as_s || ""
# Normal Auto-generated channels
# https://support.google.com/youtube/answer/2579942
# For auto-generated channels, channel_about_meta only has ["description"]["simpleText"] and ["primaryLinks"][0]["title"]["simpleText"]
@ -163,6 +186,7 @@ def get_about_info(ucid, locale)
author_thumbnail: author_thumbnail,
banner: banner,
description_html: description_html,
country: country,
total_views: total_views,
sub_count: sub_count,
joined: joined,
@ -170,5 +194,6 @@ def get_about_info(ucid, locale)
allowed_regions: allowed_regions,
related_channels: related_channels,
tabs: tabs,
links: links
})
end