Add logic to parse external chan links and country

This commit is contained in:
syeopite 2021-04-13 17:45:57 -07:00
parent d487ee1e15
commit 1feccf7e53
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82

View file

@ -128,6 +128,7 @@ struct AboutChannel
property banner : String? property banner : String?
property description_html : String property description_html : String
property paid : Bool property paid : Bool
property country : String
property total_views : Int64 property total_views : Int64
property sub_count : Int32 property sub_count : Int32
property joined : Time property joined : Time
@ -135,6 +136,7 @@ struct AboutChannel
property allowed_regions : Array(String) property allowed_regions : Array(String)
property related_channels : Array(AboutRelatedChannel) property related_channels : Array(AboutRelatedChannel)
property tabs : Array(String) property tabs : Array(String)
property links : Array(Tuple(String, String))
end end
class ChannelRedirect < Exception class ChannelRedirect < Exception
@ -882,9 +884,11 @@ def get_about_info(ucid, locale)
related_channels ||= [] of AboutRelatedChannel related_channels ||= [] of AboutRelatedChannel
end end
country = ""
total_views = 0_i64 total_views = 0_i64
joined = Time.unix(0) joined = Time.unix(0)
tabs = [] of String tabs = [] of String
links = [] of {String, String}
tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?.try &.as_a? tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?.try &.as_a?
if !tabs_json.nil? if !tabs_json.nil?
@ -902,6 +906,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 } 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) .try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)
# External link parsing
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 # Normal Auto-generated channels
# https://support.google.com/youtube/answer/2579942 # https://support.google.com/youtube/answer/2579942
# For auto-generated channels, channel_about_meta only has ["description"]["simpleText"] and ["primaryLinks"][0]["title"]["simpleText"] # For auto-generated channels, channel_about_meta only has ["description"]["simpleText"] and ["primaryLinks"][0]["title"]["simpleText"]
@ -926,6 +950,7 @@ def get_about_info(ucid, locale)
banner: banner, banner: banner,
description_html: description_html, description_html: description_html,
paid: paid, paid: paid,
country: country,
total_views: total_views, total_views: total_views,
sub_count: sub_count, sub_count: sub_count,
joined: joined, joined: joined,
@ -933,6 +958,7 @@ def get_about_info(ucid, locale)
allowed_regions: allowed_regions, allowed_regions: allowed_regions,
related_channels: related_channels, related_channels: related_channels,
tabs: tabs, tabs: tabs,
links: links
}) })
end end