Use SearchHashtag for parsing the header of hashtag pages

This commit is contained in:
ChunkyProgrammer 2023-09-07 00:54:21 -04:00
parent 347412294a
commit d93d19520e
2 changed files with 16 additions and 57 deletions

View File

@ -5,15 +5,15 @@ module Invidious::Hashtag
include DB::Serializable include DB::Serializable
property videos : Array(SearchItem) | Array(Video) property videos : Array(SearchItem) | Array(Video)
property header : HashtagHeader? property header : SearchHashtag?
property has_next_continuation : Bool property has_next_continuation : Bool
def to_json(locale : String?, json : JSON::Builder) def to_json(locale : String?, json : JSON::Builder)
json.object do json.object do
json.field "type", "hashtag" json.field "type", "hashtagPage"
if self.header != nil if self.header != nil
json.field "header" do json.field "header" do
self.header.to_json(json) self.header.try &.as(SearchHashtag).to_json(locale, json)
end end
end end
json.field "results" do json.field "results" do
@ -26,39 +26,6 @@ module Invidious::Hashtag
json.field "hasNextPage", self.has_next_continuation json.field "hasNextPage", self.has_next_continuation
end end
end end
# TODO: remove the locale and follow the crystal convention
def to_json(locale : String?, _json : Nil)
JSON.build do |json|
to_json(locale, json)
end
end
def to_json(json : JSON::Builder)
to_json(nil, json)
end
end
struct HashtagHeader
include DB::Serializable
property tag : String
property channel_count : Int64
property video_count : Int64
def to_json(json : JSON::Builder)
json.object do
json.field "hashtag", self.tag
json.field "channelCount", self.channel_count
json.field "videoCount", self.video_count
end
end
def to_json(_json : Nil)
JSON.build do |json|
to_json(json)
end
end
end end
def fetch(hashtag : String, page : Int, region : String? = nil) : HashtagPage def fetch(hashtag : String, page : Int, region : String? = nil) : HashtagPage
@ -72,8 +39,8 @@ module Invidious::Hashtag
else else
# item browses the first page (including metadata) # item browses the first page (including metadata)
response = YoutubeAPI.browse("FEhashtag", params: item, client_config: client_config) response = YoutubeAPI.browse("FEhashtag", params: item, client_config: client_config)
if item_contents = response.dig?("header", "hashtagHeaderRenderer") if item_contents = response.dig?("header")
header = parse_hashtag_renderer(item_contents) header = parse_item(item_contents).try &.as(SearchHashtag)
end end
end end
@ -119,20 +86,4 @@ module Invidious::Hashtag
.try { |i| Base64.urlsafe_encode(i) } .try { |i| Base64.urlsafe_encode(i) }
.try { |i| URI.encode_www_form(i) } .try { |i| URI.encode_www_form(i) }
end end
def parse_hashtag_renderer(item_contents)
info = extract_text(item_contents.dig?("hashtagInfoText")) || ""
regex_match = /(?<videos>\d+\S)\D+(?<channels>\d+\S)/.match(info)
hashtag = extract_text(item_contents.dig?("hashtag")) || ""
videos = short_text_to_number(regex_match.try &.["videos"]?.try &.to_s || "0")
channels = short_text_to_number(regex_match.try &.["channels"]?.try &.to_s || "0")
return HashtagHeader.new({
tag: hashtag,
channel_count: channels,
video_count: videos,
})
end
end end

View File

@ -226,9 +226,11 @@ private module Parsers
# #
# A `hashtagTileRenderer` is a kind of search result. # A `hashtagTileRenderer` is a kind of search result.
# It can be found when searching for any hashtag (e.g "#hi" or "#shorts") # It can be found when searching for any hashtag (e.g "#hi" or "#shorts")
#
# A `hashtagHeaderRenderer` is displayed on the first page of the hashtag page.
module HashtagRendererParser module HashtagRendererParser
def self.process(item : JSON::Any, author_fallback : AuthorFallback) def self.process(item : JSON::Any, author_fallback : AuthorFallback)
if item_contents = item["hashtagTileRenderer"]? if item_contents = (item["hashtagTileRenderer"]? || item["hashtagHeaderRenderer"]?)
return self.parse(item_contents) return self.parse(item_contents)
end end
end end
@ -240,8 +242,14 @@ private module Parsers
url = item_contents.dig?("onTapCommand", "commandMetadata", "webCommandMetadata", "url").try &.as_s url = item_contents.dig?("onTapCommand", "commandMetadata", "webCommandMetadata", "url").try &.as_s
url ||= URI.encode_path("/hashtag/#{title.lchop('#')}") url ||= URI.encode_path("/hashtag/#{title.lchop('#')}")
video_count_txt = extract_text(item_contents["hashtagVideoCount"]?) # E.g "203K videos" if info = extract_text(item_contents.dig?("hashtagInfoText"))
channel_count_txt = extract_text(item_contents["hashtagChannelCount"]?) # E.g "81K channels" regex_match = /(?<videos>\d+\S)\D+(?<channels>\d+\S)/.match(info)
videos = regex_match.try &.["videos"]?.try &.to_s
channels = regex_match.try &.["channels"]?.try &.to_s
else
video_count_txt = extract_text(item_contents["hashtagVideoCount"]?) # E.g "203K videos"
channel_count_txt = extract_text(item_contents["hashtagChannelCount"]?) # E.g "81K channels"
end
# Fallback for video/channel counts # Fallback for video/channel counts
if channel_count_txt.nil? || video_count_txt.nil? if channel_count_txt.nil? || video_count_txt.nil?