Compare commits

...

6 Commits

Author SHA1 Message Date
ChunkyProgrammer
0e0a8abe90
Merge f530817663 into 9892604758 2024-11-17 18:19:48 +00:00
ChunkyProgrammer
f530817663 Fix Ameba error 2024-11-17 13:19:38 -05:00
ChunkyProgrammer
767d40d0ff add support for new hashtag header format 2024-11-17 13:19:38 -05:00
ChunkyProgrammer
d93d19520e Use SearchHashtag for parsing the header of hashtag pages 2024-11-17 13:19:38 -05:00
ChunkyProgrammer
347412294a Hashtag: Show next page if a continuation exists 2024-11-17 13:19:38 -05:00
Chunky programmer
5be1d992a9 Parse hashtag header when getting the first hashtag page 2024-11-17 13:19:38 -05:00
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

@ -67,21 +67,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

@ -226,15 +226,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
@ -245,8 +248,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]