From 90bd19dc45078630b440177d260df7f54b9909c2 Mon Sep 17 00:00:00 2001 From: syeopite Date: Tue, 13 Apr 2021 17:45:57 -0700 Subject: [PATCH] Add logic to parse external chan links and country --- src/invidious/channels/about.cr | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/invidious/channels/about.cr b/src/invidious/channels/about.cr index 628d5b6f..ddddc214 100644 --- a/src/invidious/channels/about.cr +++ b/src/invidious/channels/about.cr @@ -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