This commit is contained in:
ChunkyProgrammer 2024-10-11 12:45:29 -03:00 committed by GitHub
commit 36aed67671
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 84 additions and 39 deletions

View File

@ -1,42 +1,89 @@
module Invidious::Hashtag
extend self
def fetch(hashtag : String, page : Int, region : String? = nil) : Array(SearchItem)
struct HashtagPage
include DB::Serializable
property videos : Array(SearchItem) | Array(Video)
property header : SearchHashtag?
property has_next_continuation : Bool
def to_json(locale : String?, json : JSON::Builder)
json.object do
json.field "type", "hashtagPage"
if self.header != nil
json.field "header" do
self.header.try &.as(SearchHashtag).to_json(locale, json)
end
end
json.field "results" do
json.array do
self.videos.each do |item|
item.to_json(locale, json)
end
end
end
json.field "hasNextPage", self.has_next_continuation
end
end
end
def fetch(hashtag : String, page : Int, region : String? = nil) : HashtagPage
cursor = (page - 1) * 60
ctoken = generate_continuation(hashtag, cursor)
header = nil
client_config = YoutubeAPI::ClientConfig.new(region: region)
response = YoutubeAPI.browse(continuation: ctoken, client_config: client_config)
item = generate_continuation(hashtag, cursor)
# item is a ctoken
if cursor > 0
response = YoutubeAPI.browse(continuation: item, client_config: client_config)
else
# item browses the first page (including metadata)
response = YoutubeAPI.browse("FEhashtag", params: item, client_config: client_config)
if item_contents = response.dig?("header")
header = parse_item(item_contents).try &.as(SearchHashtag)
end
end
items, _ = extract_items(response)
return items
items, next_continuation = extract_items(response)
return HashtagPage.new({
videos: items,
header: header,
has_next_continuation: next_continuation != nil,
})
end
def generate_continuation(hashtag : String, cursor : Int)
object = {
"80226972:embedded" => {
"2:string" => "FEhashtag",
"3:base64" => {
"1:varint" => 60_i64, # result count
"15:base64" => {
"1:varint" => cursor.to_i64,
"2:varint" => 0_i64,
},
"93:2:embedded" => {
"1:string" => hashtag,
"2:varint" => 0_i64,
"3:varint" => 1_i64,
},
},
"35:string" => "browse-feedFEhashtag",
"93:2:embedded" => {
"1:string" => hashtag,
"2:varint" => 0_i64,
"3:varint" => 1_i64,
},
}
if cursor > 0
object = {
"80226972:embedded" => {
"2:string" => "FEhashtag",
"3:base64" => {
"1:varint" => 60_i64, # result count
"15:base64" => {
"1:varint" => cursor.to_i64,
"2:varint" => 0_i64,
},
"93:2:embedded" => {
"1:string" => hashtag,
"2:varint" => 0_i64,
"3:varint" => 1_i64,
},
},
"35:string" => "browse-feedFEhashtag",
},
}
end
continuation = object.try { |i| Protodec::Any.cast_json(i) }
return object.try { |i| Protodec::Any.cast_json(i) }
.try { |i| Protodec::Any.from_json(i) }
.try { |i| Base64.urlsafe_encode(i) }
.try { |i| URI.encode_www_form(i) }
return continuation
end
end

View File

@ -69,21 +69,13 @@ module Invidious::Routes::API::V1::Search
env.response.content_type = "application/json"
begin
results = Invidious::Hashtag.fetch(hashtag, page, region)
hashtag_page = Invidious::Hashtag.fetch(hashtag, page, region)
rescue ex
return error_json(400, ex)
end
JSON.build do |json|
json.object do
json.field "results" do
json.array do
results.each do |item|
item.to_json(locale, json)
end
end
end
end
hashtag_page.to_json(locale, json)
end
end
end

View File

@ -101,7 +101,8 @@ module Invidious::Routes::Search
end
begin
items = Invidious::Hashtag.fetch(hashtag, page)
hashtag_page = Invidious::Hashtag.fetch(hashtag, page)
items = hashtag_page.videos
rescue ex
return error_template(500, ex)
end
@ -111,7 +112,7 @@ module Invidious::Routes::Search
page_nav_html = Frontend::Pagination.nav_numeric(locale,
base_url: "/hashtag/#{hashtag_encoded}",
current_page: page,
show_next: (items.size >= 60)
show_next: hashtag_page.has_next_continuation
)
templated "hashtag"

View File

@ -225,15 +225,18 @@ private module Parsers
#
# A `hashtagTileRenderer` is a kind of search result.
# 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
def self.process(item : JSON::Any, author_fallback : AuthorFallback)
if item_contents = item["hashtagTileRenderer"]?
if item_contents = (item["hashtagTileRenderer"]? || item["hashtagHeaderRenderer"]? || item["pageHeaderRenderer"]?)
return self.parse(item_contents)
end
end
private def self.parse(item_contents)
title = extract_text(item_contents["hashtag"]).not_nil! # E.g "#hi"
title = item_contents.dig?("pageTitle").try &.as_s
title ||= extract_text(item_contents["hashtag"]).not_nil! # E.g "#hi"
# E.g "/hashtag/hi"
url = item_contents.dig?("onTapCommand", "commandMetadata", "webCommandMetadata", "url").try &.as_s
@ -244,8 +247,10 @@ private module Parsers
# Fallback for video/channel counts
if channel_count_txt.nil? || video_count_txt.nil?
info_text = (item_contents.dig?("content", "pageHeaderViewModel", "metadata", "contentMetadataViewModel", "metadataRows", 0, "metadataParts", 0, "text", "content").try &.as_s ||
extract_text(item_contents.dig?("hashtagInfoText"))).try &.split("")
# E.g: "203K videos • 81K channels"
info_text = extract_text(item_contents["hashtagInfoText"]?).try &.split("")
if info_text && info_text.size == 2
video_count_txt ||= info_text[0]