Invidious is an alternative front-end to YouTube
channel: use extractor utils to parse tabs (+ code cleaning)
Samantaz Foxfbcce57ce29b
diff --git a/src/invidious/channels/about.cr b/src/invidious/channels/about.crindex 4c442959..bb9bd8c7 100644--- a/src/invidious/channels/about.cr+++ b/src/invidious/channels/about.cr@@ -100,34 +100,40 @@ def get_about_info(ucid, locale) : AboutChanneltotal_views = 0_i64joined = Time.unix(0)- tabs = [] of String-- tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?.try &.as_a?- if !tabs_json.nil?- # Retrieve information from the tabs array. The index we are looking for varies between channels.- tabs_json.each do |node|- # Try to find the about section which is located in only one of the tabs.- channel_about_meta = node["tabRenderer"]?.try &.["content"]?.try &.["sectionListRenderer"]?- .try &.["contents"]?.try &.[0]?.try &.["itemSectionRenderer"]?.try &.["contents"]?- .try &.[0]?.try &.["channelAboutFullMetadataRenderer"]?-- if !channel_about_meta.nil?- total_views = channel_about_meta["viewCountText"]?.try &.["simpleText"]?.try &.as_s.gsub(/\D/, "").to_i64? || 0_i64-- # The joined text is split to several sub strings. The reduce joins those strings before parsing the date.- joined = channel_about_meta["joinedDateText"]?.try &.["runs"]?.try &.as_a.reduce("") { |acc, nd| acc + nd["text"].as_s }- .try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)-- # 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"]- if (channel_about_meta["primaryLinks"]?.try &.size || 0) == 1 && (channel_about_meta["primaryLinks"][0]?) &&- (channel_about_meta["primaryLinks"][0]["title"]?.try &.["simpleText"]?.try &.as_s? || "") == "Auto-generated by YouTube"- auto_generated = true- end- end+ tab_names = [] of String++ if tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?+ # Get the name of the tabs available on this channel+ tab_names = tabs_json.as_a+ .compact_map(&.dig?("tabRenderer", "title").try &.as_s.downcase)++ # Get the currently active tab ("About")+ about_tab = extract_selected_tab(tabs_json)++ # Try to find the about metadata section+ channel_about_meta = about_tab.dig?(+ "content",+ "sectionListRenderer", "contents", 0,+ "itemSectionRenderer", "contents", 0,+ "channelAboutFullMetadataRenderer"+ )++ if !channel_about_meta.nil?+ total_views = channel_about_meta.dig?("viewCountText", "simpleText").try &.as_s.gsub(/\D/, "").to_i64? || 0_i64++ # The joined text is split to several sub strings. The reduce joins those strings before parsing the date.+ joined = extract_text(channel_about_meta["joinedDateText"]?)+ .try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)++ # 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"]+ auto_generated = (+ (channel_about_meta["primaryLinks"]?.try &.size) == 1 && \+ extract_text(channel_about_meta.dig?("primaryLinks", 0, "title")) == "Auto-generated by YouTube"+ )end- tabs = tabs_json.reject { |node| node["tabRenderer"]?.nil? }.map(&.["tabRenderer"]["title"].as_s.downcase)endsub_count = initdata@@ -148,7 +154,7 @@ def get_about_info(ucid, locale) : AboutChanneljoined: joined,is_family_friendly: is_family_friendly,allowed_regions: allowed_regions,- tabs: tabs,+ tabs: tab_names,verified: author_verified || false,)end