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
if !channel_about_meta.nil? channel_about_meta = about_tab.dig?(
total_views = channel_about_meta["viewCountText"]?.try &.["simpleText"]?.try &.as_s.gsub(/\D/, "").to_i64? || 0_i64 "content",
"sectionListRenderer", "contents", 0,
# The joined text is split to several sub strings. The reduce joins those strings before parsing the date. "itemSectionRenderer", "contents", 0,
joined = channel_about_meta["joinedDateText"]?.try &.["runs"]?.try &.as_a.reduce("") { |acc, nd| acc + nd["text"].as_s } "channelAboutFullMetadataRenderer"
.try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0) )
# Normal Auto-generated channels if !channel_about_meta.nil?
# https://support.google.com/youtube/answer/2579942 total_views = channel_about_meta.dig?("viewCountText", "simpleText").try &.as_s.gsub(/\D/, "").to_i64? || 0_i64
# 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]?) && # The joined text is split to several sub strings. The reduce joins those strings before parsing the date.
(channel_about_meta["primaryLinks"][0]["title"]?.try &.["simpleText"]?.try &.as_s? || "") == "Auto-generated by YouTube" joined = extract_text(channel_about_meta["joinedDateText"]?)
auto_generated = true .try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)
end
end # 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 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