mirror of
https://github.com/iv-org/invidious.git
synced 2024-10-01 01:25:56 -04:00
Merge 58d294fe3e
into 53e8a5d62d
This commit is contained in:
commit
95414d269a
@ -1,42 +1,89 @@
|
|||||||
module Invidious::Hashtag
|
module Invidious::Hashtag
|
||||||
extend self
|
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
|
cursor = (page - 1) * 60
|
||||||
ctoken = generate_continuation(hashtag, cursor)
|
header = nil
|
||||||
|
|
||||||
client_config = YoutubeAPI::ClientConfig.new(region: region)
|
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)
|
items, next_continuation = extract_items(response)
|
||||||
return items
|
return HashtagPage.new({
|
||||||
|
videos: items,
|
||||||
|
header: header,
|
||||||
|
has_next_continuation: next_continuation != nil,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_continuation(hashtag : String, cursor : Int)
|
def generate_continuation(hashtag : String, cursor : Int)
|
||||||
object = {
|
object = {
|
||||||
"80226972:embedded" => {
|
"93:2:embedded" => {
|
||||||
"2:string" => "FEhashtag",
|
"1:string" => hashtag,
|
||||||
"3:base64" => {
|
"2:varint" => 0_i64,
|
||||||
"1:varint" => 60_i64, # result count
|
"3:varint" => 1_i64,
|
||||||
"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",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
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| Protodec::Any.from_json(i) }
|
||||||
.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) }
|
||||||
|
|
||||||
return continuation
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -69,21 +69,13 @@ module Invidious::Routes::API::V1::Search
|
|||||||
env.response.content_type = "application/json"
|
env.response.content_type = "application/json"
|
||||||
|
|
||||||
begin
|
begin
|
||||||
results = Invidious::Hashtag.fetch(hashtag, page, region)
|
hashtag_page = Invidious::Hashtag.fetch(hashtag, page, region)
|
||||||
rescue ex
|
rescue ex
|
||||||
return error_json(400, ex)
|
return error_json(400, ex)
|
||||||
end
|
end
|
||||||
|
|
||||||
JSON.build do |json|
|
JSON.build do |json|
|
||||||
json.object do
|
hashtag_page.to_json(locale, json)
|
||||||
json.field "results" do
|
|
||||||
json.array do
|
|
||||||
results.each do |item|
|
|
||||||
item.to_json(locale, json)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -101,7 +101,8 @@ module Invidious::Routes::Search
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
items = Invidious::Hashtag.fetch(hashtag, page)
|
hashtag_page = Invidious::Hashtag.fetch(hashtag, page)
|
||||||
|
items = hashtag_page.videos
|
||||||
rescue ex
|
rescue ex
|
||||||
return error_template(500, ex)
|
return error_template(500, ex)
|
||||||
end
|
end
|
||||||
@ -111,7 +112,7 @@ module Invidious::Routes::Search
|
|||||||
page_nav_html = Frontend::Pagination.nav_numeric(locale,
|
page_nav_html = Frontend::Pagination.nav_numeric(locale,
|
||||||
base_url: "/hashtag/#{hashtag_encoded}",
|
base_url: "/hashtag/#{hashtag_encoded}",
|
||||||
current_page: page,
|
current_page: page,
|
||||||
show_next: (items.size >= 60)
|
show_next: hashtag_page.has_next_continuation
|
||||||
)
|
)
|
||||||
|
|
||||||
templated "hashtag"
|
templated "hashtag"
|
||||||
|
@ -217,15 +217,18 @@ 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"]? || item["pageHeaderRenderer"]?)
|
||||||
return self.parse(item_contents)
|
return self.parse(item_contents)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def self.parse(item_contents)
|
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"
|
# E.g "/hashtag/hi"
|
||||||
url = item_contents.dig?("onTapCommand", "commandMetadata", "webCommandMetadata", "url").try &.as_s
|
url = item_contents.dig?("onTapCommand", "commandMetadata", "webCommandMetadata", "url").try &.as_s
|
||||||
@ -236,8 +239,10 @@ private module Parsers
|
|||||||
|
|
||||||
# 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?
|
||||||
|
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"
|
# E.g: "203K videos • 81K channels"
|
||||||
info_text = extract_text(item_contents["hashtagInfoText"]?).try &.split(" • ")
|
|
||||||
|
|
||||||
if info_text && info_text.size == 2
|
if info_text && info_text.size == 2
|
||||||
video_count_txt ||= info_text[0]
|
video_count_txt ||= info_text[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user