channel: use extractor utils to parse tabs (+ code cleaning)

pull/3419/head
Samantaz Fox 2 years ago
parent 99bf519781
commit fbcce57ce2
No known key found for this signature in database
GPG Key ID: F42821059186176E

@ -100,34 +100,40 @@ def get_about_info(ucid, locale) : AboutChannel
total_views = 0_i64 total_views = 0_i64
joined = Time.unix(0) joined = Time.unix(0)
tabs = [] of String tab_names = [] of String
tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?.try &.as_a? if tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?
if !tabs_json.nil? # Get the name of the tabs available on this channel
# Retrieve information from the tabs array. The index we are looking for varies between channels. tab_names = tabs_json.as_a
tabs_json.each do |node| .compact_map(&.dig?("tabRenderer", "title").try &.as_s.downcase)
# Try to find the about section which is located in only one of the tabs.
channel_about_meta = node["tabRenderer"]?.try &.["content"]?.try &.["sectionListRenderer"]? # Get the currently active tab ("About")
.try &.["contents"]?.try &.[0]?.try &.["itemSectionRenderer"]?.try &.["contents"]? about_tab = extract_selected_tab(tabs_json)
.try &.[0]?.try &.["channelAboutFullMetadataRenderer"]?
# 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? if !channel_about_meta.nil?
total_views = channel_about_meta["viewCountText"]?.try &.["simpleText"]?.try &.as_s.gsub(/\D/, "").to_i64? || 0_i64 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. # 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 } joined = extract_text(channel_about_meta["joinedDateText"]?)
.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)
# 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
if (channel_about_meta["primaryLinks"]?.try &.size || 0) == 1 && (channel_about_meta["primaryLinks"][0]?) && # ["description"]["simpleText"] and ["primaryLinks"][0]["title"]["simpleText"]
(channel_about_meta["primaryLinks"][0]["title"]?.try &.["simpleText"]?.try &.as_s? || "") == "Auto-generated by YouTube" auto_generated = (
auto_generated = true (channel_about_meta["primaryLinks"]?.try &.size) == 1 && \
end extract_text(channel_about_meta.dig?("primaryLinks", 0, "title")) == "Auto-generated by YouTube"
end )
end end
tabs = tabs_json.reject { |node| node["tabRenderer"]?.nil? }.map(&.["tabRenderer"]["title"].as_s.downcase)
end end
sub_count = initdata sub_count = initdata
@ -148,7 +154,7 @@ 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,
tabs: tabs, tabs: tab_names,
verified: author_verified || false, verified: author_verified || false,
) )
end end

Loading…
Cancel
Save