mirror of
https://gitea.invidious.io/iv-org/invidious.git
synced 2024-08-15 00:53:41 +00:00
channel: use extractor utils to parse tabs (+ code cleaning)
This commit is contained in:
parent
99bf519781
commit
fbcce57ce2
1 changed files with 30 additions and 24 deletions
|
@ -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"]?
|
|
||||||
.try &.["contents"]?.try &.[0]?.try &.["itemSectionRenderer"]?.try &.["contents"]?
|
|
||||||
.try &.[0]?.try &.["channelAboutFullMetadataRenderer"]?
|
|
||||||
|
|
||||||
if !channel_about_meta.nil?
|
# Get the currently active tab ("About")
|
||||||
total_views = channel_about_meta["viewCountText"]?.try &.["simpleText"]?.try &.as_s.gsub(/\D/, "").to_i64? || 0_i64
|
about_tab = extract_selected_tab(tabs_json)
|
||||||
|
|
||||||
# The joined text is split to several sub strings. The reduce joins those strings before parsing the date.
|
# Try to find the about metadata section
|
||||||
joined = channel_about_meta["joinedDateText"]?.try &.["runs"]?.try &.as_a.reduce("") { |acc, nd| acc + nd["text"].as_s }
|
channel_about_meta = about_tab.dig?(
|
||||||
.try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)
|
"content",
|
||||||
|
"sectionListRenderer", "contents", 0,
|
||||||
|
"itemSectionRenderer", "contents", 0,
|
||||||
|
"channelAboutFullMetadataRenderer"
|
||||||
|
)
|
||||||
|
|
||||||
# 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…
Reference in a new issue