mirror of
https://gitea.invidious.io/iv-org/invidious-copy-2023-06-08.git
synced 2024-08-15 00:53:38 +00:00
Parse hashtag header when getting the first hashtag page
This commit is contained in:
parent
545a5937d8
commit
6c8f9a6991
5 changed files with 129 additions and 16 deletions
|
@ -1,15 +1,42 @@
|
||||||
module Invidious::Hashtag
|
module Invidious::Hashtag
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
def fetch(hashtag : String, page : Int, region : String? = nil) : Array(SearchItem)
|
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)
|
||||||
|
# for any page besides the first page, get the list of videos
|
||||||
|
if cursor > 0
|
||||||
|
ctoken = generate_continuation(hashtag, cursor)
|
||||||
response = YoutubeAPI.browse(continuation: ctoken, client_config: client_config)
|
response = YoutubeAPI.browse(continuation: ctoken, client_config: client_config)
|
||||||
|
else
|
||||||
|
# get first page + header info
|
||||||
|
response = YoutubeAPI.browse("FEhashtag", params: get_hashtag_first_page(hashtag), client_config: client_config)
|
||||||
|
if header = response.dig?("header")
|
||||||
|
header = parse_item(header).as(HashtagHeader)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
items, _ = extract_items(response)
|
items, _ = extract_items(response)
|
||||||
return items
|
return HashtagPage.new({
|
||||||
|
videos: items,
|
||||||
|
header: header,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_hashtag_first_page(hashtag : String)
|
||||||
|
object = {
|
||||||
|
"93:embedded" => {
|
||||||
|
"1:string" => hashtag,
|
||||||
|
"2:varint" => 0_i64,
|
||||||
|
"3:varint" => 1_i64,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_continuation(hashtag : String, cursor : Int)
|
def generate_continuation(hashtag : String, cursor : Int)
|
||||||
|
|
|
@ -274,4 +274,63 @@ struct Continuation
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct HashtagPage
|
||||||
|
include DB::Serializable
|
||||||
|
|
||||||
|
property videos : Array(SearchItem) | Array(Video)
|
||||||
|
property header : HashtagHeader?
|
||||||
|
|
||||||
|
def to_json(locale : String?, json : JSON::Builder)
|
||||||
|
json.object do
|
||||||
|
json.field "type", "hashtag"
|
||||||
|
if self.header != nil
|
||||||
|
json.field "header" do
|
||||||
|
self.header.to_json(json)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
json.field "results" do
|
||||||
|
json.array do
|
||||||
|
self.videos.each do |item|
|
||||||
|
item.to_json(locale, json)
|
||||||
|
end
|
||||||
|
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 "type", "hashtagHeader"
|
||||||
|
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
|
||||||
|
|
||||||
alias SearchItem = SearchVideo | SearchChannel | SearchPlaylist | Category
|
alias SearchItem = SearchVideo | SearchChannel | SearchPlaylist | Category
|
||||||
|
|
|
@ -66,21 +66,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)
|
hashtagPage = 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
|
hashtagPage.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
|
||||||
|
|
|
@ -91,7 +91,8 @@ module Invidious::Routes::Search
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
videos = Invidious::Hashtag.fetch(hashtag, page)
|
hashtagPage = Invidious::Hashtag.fetch(hashtag, page)
|
||||||
|
videos = hashtagPage.videos
|
||||||
rescue ex
|
rescue ex
|
||||||
return error_template(500, ex)
|
return error_template(500, ex)
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,6 +20,7 @@ private ITEM_PARSERS = {
|
||||||
Parsers::ReelItemRendererParser,
|
Parsers::ReelItemRendererParser,
|
||||||
Parsers::ItemSectionRendererParser,
|
Parsers::ItemSectionRendererParser,
|
||||||
Parsers::ContinuationItemRendererParser,
|
Parsers::ContinuationItemRendererParser,
|
||||||
|
Parsers::HashtagHeaderRenderer,
|
||||||
}
|
}
|
||||||
|
|
||||||
private alias InitialData = Hash(String, JSON::Any)
|
private alias InitialData = Hash(String, JSON::Any)
|
||||||
|
@ -550,6 +551,39 @@ private module Parsers
|
||||||
return {{@type.name}}
|
return {{@type.name}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Parses an InnerTube hashtagHeaderRender into a HashtagHeaderRender.
|
||||||
|
# Returns nil when the given object isn't a hashtagHeaderRender.
|
||||||
|
#
|
||||||
|
# hashtagHeaderRender contains metadate of the hashtag page such as video count and channel count
|
||||||
|
#
|
||||||
|
module HashtagHeaderRenderer
|
||||||
|
def self.process(item : JSON::Any, author_fallback : AuthorFallback)
|
||||||
|
if item_contents = item["hashtagHeaderRenderer"]?
|
||||||
|
return self.parse(item_contents)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def self.parse(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
|
||||||
|
|
||||||
|
def self.parser_name
|
||||||
|
return {{@type.name}}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# The following are the extractors for extracting an array of items from
|
# The following are the extractors for extracting an array of items from
|
||||||
|
|
Loading…
Reference in a new issue