summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/invidious.cr2
-rw-r--r--src/invidious/channels/community.cr72
-rw-r--r--src/invidious/config.cr2
-rw-r--r--src/invidious/routes/images.cr142
-rw-r--r--src/invidious/videos/parser.cr10
-rw-r--r--src/invidious/yt_backend/connection_pool.cr37
-rw-r--r--src/invidious/yt_backend/extractors.cr26
-rw-r--r--src/invidious/yt_backend/youtube_api.cr14
8 files changed, 89 insertions, 216 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index 84e1895d..e0bd0101 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -90,7 +90,7 @@ SOFTWARE = {
"branch" => "#{CURRENT_BRANCH}",
}
-YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size, use_quic: CONFIG.use_quic)
+YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
# CLI
Kemal.config.extra_options do |parser|
diff --git a/src/invidious/channels/community.cr b/src/invidious/channels/community.cr
index aac4bc8a..791f1641 100644
--- a/src/invidious/channels/community.cr
+++ b/src/invidious/channels/community.cr
@@ -1,49 +1,31 @@
private IMAGE_QUALITIES = {320, 560, 640, 1280, 2000}
# TODO: Add "sort_by"
-def fetch_channel_community(ucid, continuation, locale, format, thin_mode)
- response = YT_POOL.client &.get("/channel/#{ucid}/community?gl=US&hl=en")
- if response.status_code != 200
- response = YT_POOL.client &.get("/user/#{ucid}/community?gl=US&hl=en")
- end
-
- if response.status_code != 200
- raise NotFoundException.new("This channel does not exist.")
- end
-
- ucid = response.body.match(/https:\/\/www.youtube.com\/channel\/(?<ucid>UC[a-zA-Z0-9_-]{22})/).not_nil!["ucid"]
-
- if !continuation || continuation.empty?
- initial_data = extract_initial_data(response.body)
- body = extract_selected_tab(initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"])["content"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]
-
- if !body
- raise InfoException.new("Could not extract community tab.")
+def fetch_channel_community(ucid, cursor, locale, format, thin_mode)
+ if cursor.nil?
+ # Egljb21tdW5pdHk%3D is the protobuf object to load "community"
+ initial_data = YoutubeAPI.browse(ucid, params: "Egljb21tdW5pdHk%3D")
+
+ items = [] of JSON::Any
+ extract_items(initial_data) do |item|
+ items << item
end
else
- continuation = produce_channel_community_continuation(ucid, continuation)
+ continuation = produce_channel_community_continuation(ucid, cursor)
+ initial_data = YoutubeAPI.browse(continuation: continuation)
- headers = HTTP::Headers.new
- headers["cookie"] = response.cookies.add_request_headers(headers)["cookie"]
+ container = initial_data.dig?("continuationContents", "itemSectionContinuation", "contents")
- session_token = response.body.match(/"XSRF_TOKEN":"(?<session_token>[^"]+)"/).try &.["session_token"]? || ""
- post_req = {
- session_token: session_token,
- }
+ raise InfoException.new("Can't extract community data") if container.nil?
- body = YoutubeAPI.browse(continuation)
-
- body = body.dig?("continuationContents", "itemSectionContinuation") ||
- body.dig?("continuationContents", "backstageCommentsContinuation")
-
- if !body
- raise InfoException.new("Could not extract continuation.")
- end
+ items = container.as_a
end
- posts = body["contents"].as_a
+ return extract_channel_community(items, ucid: ucid, locale: locale, format: format, thin_mode: thin_mode)
+end
- if message = posts[0]["messageRenderer"]?
+def extract_channel_community(items, *, ucid, locale, format, thin_mode)
+ if message = items[0]["messageRenderer"]?
error_message = (message["text"]["simpleText"]? ||
message["text"]["runs"]?.try &.[0]?.try &.["text"]?)
.try &.as_s || ""
@@ -59,7 +41,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)
json.field "authorId", ucid
json.field "comments" do
json.array do
- posts.each do |post|
+ items.each do |post|
comments = post["backstagePostThreadRenderer"]?.try &.["comments"]? ||
post["backstageCommentsContinuation"]?
@@ -216,6 +198,22 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)
parse_item(attachment)
.as(SearchPlaylist)
.to_json(locale, json)
+ when .has_key?("quizRenderer")
+ json.object do
+ attachment = attachment["quizRenderer"]
+ json.field "type", "quiz"
+ json.field "totalVotes", short_text_to_number(attachment["totalVotes"]["simpleText"].as_s.split(" ")[0])
+ json.field "choices" do
+ json.array do
+ attachment["choices"].as_a.each do |choice|
+ json.object do
+ json.field "text", choice.dig("text", "runs", 0, "text").as_s
+ json.field "isCorrect", choice["isCorrect"].as_bool
+ end
+ end
+ end
+ end
+ end
else
json.object do
json.field "type", "unknown"
@@ -242,7 +240,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)
end
end
end
- if cont = posts.dig?(-1, "continuationItemRenderer", "continuationEndpoint", "continuationCommand", "token")
+ if cont = items.dig?(-1, "continuationItemRenderer", "continuationEndpoint", "continuationCommand", "token")
json.field "continuation", extract_channel_community_cursor(cont.as_s)
end
end
diff --git a/src/invidious/config.cr b/src/invidious/config.cr
index e5f1e822..cee33ce1 100644
--- a/src/invidious/config.cr
+++ b/src/invidious/config.cr
@@ -126,8 +126,6 @@ class Config
property host_binding : String = "0.0.0.0"
# Pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool of `pool_size`)
property pool_size : Int32 = 100
- # Use quic transport for youtube api
- property use_quic : Bool = false
# Saved cookies in "name1=value1; name2=value2..." format
@[YAML::Field(converter: Preferences::StringToCookies)]
diff --git a/src/invidious/routes/images.cr b/src/invidious/routes/images.cr
index 594a7869..b6a2e110 100644
--- a/src/invidious/routes/images.cr
+++ b/src/invidious/routes/images.cr
@@ -3,17 +3,7 @@ module Invidious::Routes::Images
def self.ggpht(env)
url = env.request.path.lchop("/ggpht")
- headers = (
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- HTTP::Headers{":authority" => "yt3.ggpht.com"}
- else
- HTTP::Headers.new
- end
- {% else %}
- HTTP::Headers.new
- {% end %}
- )
+ headers = HTTP::Headers.new
REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]?
@@ -42,22 +32,9 @@ module Invidious::Routes::Images
}
begin
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- YT_POOL.client &.get(url, headers) do |resp|
- return request_proc.call(resp)
- end
- else
- HTTP::Client.get("https://yt3.ggpht.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- end
- {% else %}
- # This can likely be optimized into a (small) pool sometime in the future.
- HTTP::Client.get("https://yt3.ggpht.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- {% end %}
+ HTTP::Client.get("https://yt3.ggpht.com#{url}") do |resp|
+ return request_proc.call(resp)
+ end
rescue ex
end
end
@@ -78,10 +55,6 @@ module Invidious::Routes::Images
headers = HTTP::Headers.new
- {% unless flag?(:disable_quic) %}
- headers[":authority"] = "#{authority}.ytimg.com"
- {% end %}
-
REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]?
headers[header] = env.request.headers[header]
@@ -107,22 +80,9 @@ module Invidious::Routes::Images
}
begin
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- YT_POOL.client &.get(url, headers) do |resp|
- return request_proc.call(resp)
- end
- else
- HTTP::Client.get("https://#{authority}.ytimg.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- end
- {% else %}
- # This can likely be optimized into a (small) pool sometime in the future.
- HTTP::Client.get("https://#{authority}.ytimg.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- {% end %}
+ HTTP::Client.get("https://#{authority}.ytimg.com#{url}") do |resp|
+ return request_proc.call(resp)
+ end
rescue ex
end
end
@@ -133,17 +93,7 @@ module Invidious::Routes::Images
name = env.params.url["name"]
url = env.request.resource
- headers = (
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- HTTP::Headers{":authority" => "i9.ytimg.com"}
- else
- HTTP::Headers.new
- end
- {% else %}
- HTTP::Headers.new
- {% end %}
- )
+ headers = HTTP::Headers.new
REQUEST_HEADERS_WHITELIST.each do |header|
if env.request.headers[header]?
@@ -169,22 +119,9 @@ module Invidious::Routes::Images
}
begin
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- YT_POOL.client &.get(url, headers) do |resp|
- return request_proc.call(resp)
- end
- else
- HTTP::Client.get("https://i9.ytimg.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- end
- {% else %}
- # This can likely be optimized into a (small) pool sometime in the future.
- HTTP::Client.get("https://i9.ytimg.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- {% end %}
+ HTTP::Client.get("https://i9.ytimg.com#{url}") do |resp|
+ return request_proc.call(resp)
+ end
rescue ex
end
end
@@ -223,41 +160,16 @@ module Invidious::Routes::Images
id = env.params.url["id"]
name = env.params.url["name"]
- headers = (
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- HTTP::Headers{":authority" => "i.ytimg.com"}
- else
- HTTP::Headers.new
- end
- {% else %}
- HTTP::Headers.new
- {% end %}
- )
+ headers = HTTP::Headers.new
if name == "maxres.jpg"
build_thumbnails(id).each do |thumb|
thumbnail_resource_path = "/vi/#{id}/#{thumb[:url]}.jpg"
- # Logic here is short enough that manually typing them out should be fine.
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- if YT_POOL.client &.head(thumbnail_resource_path, headers).status_code == 200
- name = thumb[:url] + ".jpg"
- break
- end
- else
- if HTTP::Client.head("https://i.ytimg.com#{thumbnail_resource_path}").status_code == 200
- name = thumb[:url] + ".jpg"
- break
- end
- end
- {% else %}
- # This can likely be optimized into a (small) pool sometime in the future.
- if HTTP::Client.head("https://i.ytimg.com#{thumbnail_resource_path}").status_code == 200
- name = thumb[:url] + ".jpg"
- break
- end
- {% end %}
+ # This can likely be optimized into a (small) pool sometime in the future.
+ if HTTP::Client.head("https://i.ytimg.com#{thumbnail_resource_path}").status_code == 200
+ name = thumb[:url] + ".jpg"
+ break
+ end
end
end
@@ -287,22 +199,10 @@ module Invidious::Routes::Images
}
begin
- {% unless flag?(:disable_quic) %}
- if CONFIG.use_quic
- YT_POOL.client &.get(url, headers) do |resp|
- return request_proc.call(resp)
- end
- else
- HTTP::Client.get("https://i.ytimg.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- end
- {% else %}
- # This can likely be optimized into a (small) pool sometime in the future.
- HTTP::Client.get("https://i.ytimg.com#{url}") do |resp|
- return request_proc.call(resp)
- end
- {% end %}
+ # This can likely be optimized into a (small) pool sometime in the future.
+ HTTP::Client.get("https://i.ytimg.com#{url}") do |resp|
+ return request_proc.call(resp)
+ end
rescue ex
end
end
diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr
index 9cc0ffdc..2a09d187 100644
--- a/src/invidious/videos/parser.cr
+++ b/src/invidious/videos/parser.cr
@@ -55,8 +55,9 @@ def extract_video_info(video_id : String, proxy_region : String? = nil)
client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region)
# Fetch data from the player endpoint
- # 8AEB param is used to fetch YouTube stories
- player_response = YoutubeAPI.player(video_id: video_id, params: "8AEB", client_config: client_config)
+ # CgIQBg is a workaround for streaming URLs that returns a 403.
+ # See https://github.com/iv-org/invidious/issues/4027#issuecomment-1666944520
+ player_response = YoutubeAPI.player(video_id: video_id, params: "CgIQBg", client_config: client_config)
playability_status = player_response.dig?("playabilityStatus", "status").try &.as_s
@@ -135,8 +136,9 @@ end
def try_fetch_streaming_data(id : String, client_config : YoutubeAPI::ClientConfig) : Hash(String, JSON::Any)?
LOGGER.debug("try_fetch_streaming_data: [#{id}] Using #{client_config.client_type} client.")
- # 8AEB param is used to fetch YouTube stories
- response = YoutubeAPI.player(video_id: id, params: "8AEB", client_config: client_config)
+ # CgIQBg is a workaround for streaming URLs that returns a 403.
+ # See https://github.com/iv-org/invidious/issues/4027#issuecomment-1666944520
+ response = YoutubeAPI.player(video_id: id, params: "CgIQBg", client_config: client_config)
playability_status = response["playabilityStatus"]["status"]
LOGGER.debug("try_fetch_streaming_data: [#{id}] Got playabilityStatus == #{playability_status}.")
diff --git a/src/invidious/yt_backend/connection_pool.cr b/src/invidious/yt_backend/connection_pool.cr
index 658731cf..e9eb726c 100644
--- a/src/invidious/yt_backend/connection_pool.cr
+++ b/src/invidious/yt_backend/connection_pool.cr
@@ -1,11 +1,3 @@
-{% unless flag?(:disable_quic) %}
- require "lsquic"
-
- alias HTTPClientType = QUIC::Client | HTTP::Client
-{% else %}
- alias HTTPClientType = HTTP::Client
-{% end %}
-
def add_yt_headers(request)
if request.headers["User-Agent"] == "Crystal"
request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
@@ -26,11 +18,11 @@ struct YoutubeConnectionPool
property! url : URI
property! capacity : Int32
property! timeout : Float64
- property pool : DB::Pool(HTTPClientType)
+ property pool : DB::Pool(HTTP::Client)
- def initialize(url : URI, @capacity = 5, @timeout = 5.0, use_quic = true)
+ def initialize(url : URI, @capacity = 5, @timeout = 5.0)
@url = url
- @pool = build_pool(use_quic)
+ @pool = build_pool()
end
def client(region = nil, &block)
@@ -43,11 +35,7 @@ struct YoutubeConnectionPool
response = yield conn
rescue ex
conn.close
- {% unless flag?(:disable_quic) %}
- conn = CONFIG.use_quic ? QUIC::Client.new(url) : HTTP::Client.new(url)
- {% else %}
- conn = HTTP::Client.new(url)
- {% end %}
+ conn = HTTP::Client.new(url)
conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
@@ -61,19 +49,9 @@ struct YoutubeConnectionPool
response
end
- private def build_pool(use_quic)
- DB::Pool(HTTPClientType).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
- conn = nil # Declare
- {% unless flag?(:disable_quic) %}
- if use_quic
- conn = QUIC::Client.new(url)
- else
- conn = HTTP::Client.new(url)
- end
- {% else %}
- conn = HTTP::Client.new(url)
- {% end %}
-
+ private def build_pool
+ DB::Pool(HTTP::Client).new(initial_pool_size: 0, max_pool_size: capacity, max_idle_pool_size: capacity, checkout_timeout: timeout) do
+ conn = HTTP::Client.new(url)
conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
@@ -83,7 +61,6 @@ struct YoutubeConnectionPool
end
def make_client(url : URI, region = nil)
- # TODO: Migrate any applicable endpoints to QUIC
client = HTTPClient.new(url, OpenSSL::SSL::Context::Client.insecure)
client.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::UNSPEC
client.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr
index 8456313b..aaf7772e 100644
--- a/src/invidious/yt_backend/extractors.cr
+++ b/src/invidious/yt_backend/extractors.cr
@@ -659,19 +659,25 @@ private module Extractors
private def self.unpack_section_list(contents)
raw_items = [] of JSON::Any
- contents.as_a.each do |renderer_container|
- renderer_container_contents = renderer_container["itemSectionRenderer"]["contents"][0]
-
- # Category extraction
- if items_container = renderer_container_contents["shelfRenderer"]?
- raw_items << renderer_container_contents
- next
- elsif items_container = renderer_container_contents["gridRenderer"]?
+ contents.as_a.each do |item|
+ if item_section_content = item.dig?("itemSectionRenderer", "contents")
+ raw_items += self.unpack_item_section(item_section_content)
else
- items_container = renderer_container_contents
+ raw_items << item
end
+ end
- items_container["items"]?.try &.as_a.each do |item|
+ return raw_items
+ end
+
+ private def self.unpack_item_section(contents)
+ raw_items = [] of JSON::Any
+
+ contents.as_a.each do |item|
+ # Category extraction
+ if container = item.dig?("gridRenderer", "items") || item.dig?("items")
+ raw_items += container.as_a
+ else
raw_items << item
end
end
diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr
index 3dd9e9d8..aef9ddd9 100644
--- a/src/invidious/yt_backend/youtube_api.cr
+++ b/src/invidious/yt_backend/youtube_api.cr
@@ -595,17 +595,9 @@ module YoutubeAPI
LOGGER.trace("YoutubeAPI: POST data: #{data}")
# Send the POST request
- if {{ !flag?(:disable_quic) }} && CONFIG.use_quic
- # Using QUIC client
- body = YT_POOL.client(client_config.proxy_region,
- &.post(url, headers: headers, body: data.to_json)
- ).body
- else
- # Using HTTP client
- body = YT_POOL.client(client_config.proxy_region) do |client|
- client.post(url, headers: headers, body: data.to_json) do |response|
- self._decompress(response.body_io, response.headers["Content-Encoding"]?)
- end
+ body = YT_POOL.client(client_config.proxy_region) do |client|
+ client.post(url, headers: headers, body: data.to_json) do |response|
+ self._decompress(response.body_io, response.headers["Content-Encoding"]?)
end
end