mirror of
https://github.com/iv-org/invidious.git
synced 2024-09-17 01:05:46 +05:30
Compare commits
73 Commits
a98debfe88
...
61d75050e4
Author | SHA1 | Date | |
---|---|---|---|
|
61d75050e4 | ||
|
10e5788c21 | ||
|
b509aa91d5 | ||
|
ec8b7916fa | ||
|
56a7488161 | ||
|
a845752fff | ||
|
63a729998b | ||
|
325561e755 | ||
|
09bf09befe | ||
|
7fdbda612f | ||
|
4f60feee17 | ||
|
733bd27a5c | ||
|
1ff0775f4b | ||
|
e62d4db752 | ||
|
8b1da2001e | ||
|
5a12005b48 | ||
|
bad92093bf | ||
|
436a61e3bb | ||
|
5e0f55333a | ||
|
de61b163a3 | ||
|
99c7e9e800 | ||
|
e9bab06e90 | ||
|
a56a724a55 | ||
|
0a54e26536 | ||
|
d135e5b7f7 | ||
|
911dad6935 | ||
|
220cc9bd2f | ||
|
aace30b2b4 | ||
|
64d1f26ece | ||
|
8f5c6a602b | ||
|
dd38eef41a | ||
|
848ab1e9c8 | ||
|
933802b897 | ||
|
3bac467a8c | ||
|
248df785d7 | ||
|
6b429575bf | ||
|
e0ed094cc4 | ||
|
a644d76497 | ||
|
45fd4a1968 | ||
|
e82c965e89 | ||
|
f466116cd7 | ||
|
5b519123a7 | ||
|
0224162ad2 | ||
|
04ca64691b | ||
|
5957523624 | ||
|
629599f940 | ||
|
31ad708206 | ||
|
3b773c4f77 | ||
|
57e606cb43 | ||
|
f57aac5815 | ||
|
71a821a7e6 | ||
|
e0d0dbde3c | ||
|
90fcf80a8d | ||
|
9d66676f2d | ||
|
2fdb6dd644 | ||
|
470245de54 | ||
|
b0ec359028 | ||
|
b90cf286fc | ||
|
a9e8aabe1f | ||
|
b0c6bdf44c | ||
|
c5eb10b21f | ||
|
72fe8af850 | ||
|
499aed37dd | ||
|
4adb4c00d2 | ||
|
cf61af67ab | ||
|
1363fb8094 | ||
|
5f2b43d653 | ||
|
6251d8d43f | ||
|
162b89d942 | ||
|
0d63ad5a7f | ||
|
63e5d72466 | ||
|
c251c66748 | ||
|
b0df3774db |
58
.ameba.yml
58
.ameba.yml
@ -20,6 +20,9 @@ Lint/ShadowingOuterLocalVar:
|
||||
Excluded:
|
||||
- src/invidious/helpers/tokens.cr
|
||||
|
||||
Lint/NotNil:
|
||||
Enabled: false
|
||||
|
||||
|
||||
#
|
||||
# Style
|
||||
@ -31,6 +34,13 @@ Style/RedundantBegin:
|
||||
Style/RedundantReturn:
|
||||
Enabled: false
|
||||
|
||||
Style/ParenthesesAroundCondition:
|
||||
Enabled: false
|
||||
|
||||
# This requires a rewrite of most data structs (and their usage) in Invidious.
|
||||
Style/QueryBoolMethods:
|
||||
Enabled: false
|
||||
|
||||
|
||||
#
|
||||
# Metrics
|
||||
@ -39,50 +49,4 @@ Style/RedundantReturn:
|
||||
# Ignore function complexity (number of if/else & case/when branches)
|
||||
# For some functions that can hardly be simplified for now
|
||||
Metrics/CyclomaticComplexity:
|
||||
Excluded:
|
||||
# get_about_info(ucid, locale) => [17/10]
|
||||
- src/invidious/channels/about.cr
|
||||
|
||||
# fetch_channel_community(ucid, continuation, ...) => [34/10]
|
||||
- src/invidious/channels/community.cr
|
||||
|
||||
# create_notification_stream(env, topics, connection_channel) => [14/10]
|
||||
- src/invidious/helpers/helpers.cr:84:5
|
||||
|
||||
# get_index(plural_form, count) => [25/10]
|
||||
- src/invidious/helpers/i18next.cr
|
||||
|
||||
# call(context) => [18/10]
|
||||
- src/invidious/helpers/static_file_handler.cr
|
||||
|
||||
# show(env) => [38/10]
|
||||
- src/invidious/routes/embed.cr
|
||||
|
||||
# get_video_playback(env) => [45/10]
|
||||
- src/invidious/routes/video_playback.cr
|
||||
|
||||
# handle(env) => [40/10]
|
||||
- src/invidious/routes/watch.cr
|
||||
|
||||
# playlist_ajax(env) => [24/10]
|
||||
- src/invidious/routes/playlists.cr
|
||||
|
||||
# fetch_youtube_comments(id, cursor, ....) => [40/10]
|
||||
# template_youtube_comments(comments, locale, ...) => [16/10]
|
||||
# content_to_comment_html(content) => [14/10]
|
||||
- src/invidious/comments.cr
|
||||
|
||||
# to_json(locale, json) => [21/10]
|
||||
# extract_video_info(video_id, ...) => [44/10]
|
||||
# process_video_params(query, preferences) => [20/10]
|
||||
- src/invidious/videos.cr
|
||||
|
||||
|
||||
|
||||
#src/invidious/playlists.cr:327:5
|
||||
#[C] Metrics/CyclomaticComplexity: Cyclomatic complexity too high [19/10]
|
||||
# fetch_playlist(plid : String)
|
||||
|
||||
#src/invidious/playlists.cr:436:5
|
||||
#[C] Metrics/CyclomaticComplexity: Cyclomatic complexity too high [11/10]
|
||||
# extract_playlist_videos(initial_data : Hash(String, JSON::Any))
|
||||
Enabled: false
|
||||
|
@ -1,4 +1,4 @@
|
||||
name: Build and release container
|
||||
name: Build and release container directly from master
|
||||
|
||||
on:
|
||||
push:
|
||||
@ -24,9 +24,9 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Crystal
|
||||
uses: crystal-lang/install-crystal@v1.8.0
|
||||
uses: crystal-lang/install-crystal@v1.8.2
|
||||
with:
|
||||
crystal: 1.9.2
|
||||
crystal: 1.12.2
|
||||
|
||||
- name: Run lint
|
||||
run: |
|
||||
@ -58,7 +58,7 @@ jobs:
|
||||
images: quay.io/invidious/invidious
|
||||
tags: |
|
||||
type=sha,format=short,prefix={{date 'YYYY.MM.DD'}}-,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
type=raw,value=master,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
labels: |
|
||||
quay.expires-after=12w
|
||||
|
||||
@ -83,7 +83,7 @@ jobs:
|
||||
suffix=-arm64
|
||||
tags: |
|
||||
type=sha,format=short,prefix={{date 'YYYY.MM.DD'}}-,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
type=raw,value=master,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
labels: |
|
||||
quay.expires-after=12w
|
||||
|
90
.github/workflows/build-stable-container.yml
vendored
Normal file
90
.github/workflows/build-stable-container.yml
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
name: Build and release container
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Crystal
|
||||
uses: crystal-lang/install-crystal@v1.8.2
|
||||
with:
|
||||
crystal: 1.12.2
|
||||
|
||||
- name: Run lint
|
||||
run: |
|
||||
if ! crystal tool format --check; then
|
||||
crystal tool format
|
||||
git diff
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
with:
|
||||
platforms: arm64
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: quay.io
|
||||
username: ${{ secrets.QUAY_USERNAME }}
|
||||
password: ${{ secrets.QUAY_PASSWORD }}
|
||||
|
||||
- name: Docker meta
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: quay.io/invidious/invidious
|
||||
tags: |
|
||||
type=semver,pattern={{version}}
|
||||
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
labels: |
|
||||
quay.expires-after=12w
|
||||
|
||||
- name: Build and push Docker AMD64 image for Push Event
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile
|
||||
platforms: linux/amd64
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
build-args: |
|
||||
"release=1"
|
||||
|
||||
- name: Docker meta
|
||||
id: meta-arm64
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: quay.io/invidious/invidious
|
||||
flavor: |
|
||||
suffix=-arm64
|
||||
tags: |
|
||||
type=semver,pattern={{version}}
|
||||
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
|
||||
labels: |
|
||||
quay.expires-after=12w
|
||||
|
||||
- name: Build and push Docker ARM64 image for Push Event
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: docker/Dockerfile.arm64
|
||||
platforms: linux/arm64/v8
|
||||
labels: ${{ steps.meta-arm64.outputs.labels }}
|
||||
push: true
|
||||
tags: ${{ steps.meta-arm64.outputs.tags }}
|
||||
build-args: |
|
||||
"release=1"
|
28
.github/workflows/ci.yml
vendored
28
.github/workflows/ci.yml
vendored
@ -38,10 +38,10 @@ jobs:
|
||||
matrix:
|
||||
stable: [true]
|
||||
crystal:
|
||||
- 1.7.3
|
||||
- 1.8.2
|
||||
- 1.9.2
|
||||
- 1.10.1
|
||||
- 1.11.2
|
||||
- 1.12.1
|
||||
include:
|
||||
- crystal: nightly
|
||||
stable: false
|
||||
@ -124,4 +124,28 @@ jobs:
|
||||
- name: Test Docker
|
||||
run: while curl -Isf http://localhost:3000; do sleep 1; done
|
||||
|
||||
ameba_lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Install Crystal
|
||||
uses: crystal-lang/install-crystal@v1.8.0
|
||||
with:
|
||||
crystal: latest
|
||||
|
||||
- name: Cache Shards
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: |
|
||||
./lib
|
||||
./bin
|
||||
key: shards-${{ hashFiles('shard.lock') }}
|
||||
|
||||
- name: Install Shards
|
||||
run: shards install
|
||||
|
||||
- name: Run Ameba linter
|
||||
run: bin/ameba
|
||||
|
@ -351,7 +351,12 @@ if (video_data.params.save_player_pos) {
|
||||
const rememberedTime = get_video_time();
|
||||
let lastUpdated = 0;
|
||||
|
||||
if(!hasTimeParam) set_seconds_after_start(rememberedTime);
|
||||
if(!hasTimeParam) {
|
||||
if (rememberedTime >= video_data.length_seconds - 20)
|
||||
set_seconds_after_start(0);
|
||||
else
|
||||
set_seconds_after_start(rememberedTime);
|
||||
}
|
||||
|
||||
player.on('timeupdate', function () {
|
||||
const raw = player.currentTime();
|
||||
|
@ -343,21 +343,6 @@ full_refresh: false
|
||||
##
|
||||
feed_threads: 1
|
||||
|
||||
##
|
||||
## Enable/Disable the polling job that keeps the decryption
|
||||
## function (for "secured" videos) up to date.
|
||||
##
|
||||
## Note: This part of the code generate a small amount of data every minute.
|
||||
## This may not be desired if you have bandwidth limits set by your ISP.
|
||||
##
|
||||
## Note 2: This part of the code is currently broken, so changing
|
||||
## this setting has no impact.
|
||||
##
|
||||
## Accepted values: true, false
|
||||
## Default: false
|
||||
##
|
||||
#decrypt_polling: false
|
||||
|
||||
|
||||
jobs:
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM crystallang/crystal:1.8.2-alpine AS builder
|
||||
FROM crystallang/crystal:1.12.1-alpine AS builder
|
||||
|
||||
RUN apk add --no-cache sqlite-static yaml-static
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
FROM alpine:3.18 AS builder
|
||||
RUN apk add --no-cache 'crystal=1.8.2-r0' shards sqlite-static yaml-static yaml-dev libxml2-static zlib-static openssl-libs-static openssl-dev musl-dev xz-static
|
||||
FROM alpine:3.19 AS builder
|
||||
RUN apk add --no-cache 'crystal=1.10.1-r0' shards sqlite-static yaml-static yaml-dev libxml2-static zlib-static openssl-libs-static openssl-dev musl-dev xz-static
|
||||
|
||||
ARG release
|
||||
|
||||
|
@ -2,7 +2,7 @@ version: 2.0
|
||||
shards:
|
||||
ameba:
|
||||
git: https://github.com/crystal-ameba/ameba.git
|
||||
version: 1.5.0
|
||||
version: 1.6.1
|
||||
|
||||
athena-negotiation:
|
||||
git: https://github.com/athena-framework/negotiation.git
|
||||
|
@ -35,7 +35,7 @@ development_dependencies:
|
||||
version: ~> 0.10.4
|
||||
ameba:
|
||||
github: crystal-ameba/ameba
|
||||
version: ~> 1.5.0
|
||||
version: ~> 1.6.1
|
||||
|
||||
crystal: ">= 1.0.0, < 2.0.0"
|
||||
|
||||
|
@ -67,7 +67,7 @@ Spectator.describe "parse_video_info" do
|
||||
# Video metadata
|
||||
|
||||
expect(info["genre"].as_s).to eq("Entertainment")
|
||||
expect(info["genreUcid"].as_s).to be_empty
|
||||
expect(info["genreUcid"].as_s?).to be_nil
|
||||
expect(info["license"].as_s).to be_empty
|
||||
|
||||
# Author infos
|
||||
@ -151,7 +151,7 @@ Spectator.describe "parse_video_info" do
|
||||
# Video metadata
|
||||
|
||||
expect(info["genre"].as_s).to eq("Music")
|
||||
expect(info["genreUcid"].as_s).to be_empty
|
||||
expect(info["genreUcid"].as_s?).to be_nil
|
||||
expect(info["license"].as_s).to be_empty
|
||||
|
||||
# Author infos
|
||||
|
@ -94,7 +94,7 @@ Spectator.describe "parse_video_info" do
|
||||
# Video metadata
|
||||
|
||||
expect(info["genre"].as_s).to eq("Entertainment")
|
||||
expect(info["genreUcid"].as_s).to be_empty
|
||||
expect(info["genreUcid"].as_s?).to be_nil
|
||||
expect(info["license"].as_s).to be_empty
|
||||
|
||||
# Author infos
|
||||
|
@ -163,11 +163,6 @@ if CONFIG.feed_threads > 0
|
||||
Invidious::Jobs.register Invidious::Jobs::RefreshFeedsJob.new(PG_DB)
|
||||
end
|
||||
|
||||
DECRYPT_FUNCTION = DecryptFunction.new(CONFIG.decrypt_polling)
|
||||
if CONFIG.decrypt_polling
|
||||
Invidious::Jobs.register Invidious::Jobs::UpdateDecryptFunctionJob.new
|
||||
end
|
||||
|
||||
if CONFIG.statistics_enabled
|
||||
Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
|
||||
end
|
||||
|
@ -72,6 +72,7 @@ def get_about_info(ucid, locale) : AboutChannel
|
||||
|
||||
# Raises a KeyError on failure.
|
||||
banners = initdata["header"]["c4TabbedHeaderRenderer"]?.try &.["banner"]?.try &.["thumbnails"]?
|
||||
banners ||= initdata.dig?("header", "pageHeaderRenderer", "content", "pageHeaderViewModel", "banner", "imageBannerViewModel", "image", "sources")
|
||||
banner = banners.try &.[-1]?.try &.["url"].as_s?
|
||||
|
||||
# if banner.includes? "channels/c4/default_banner"
|
||||
@ -147,9 +148,17 @@ def get_about_info(ucid, locale) : AboutChannel
|
||||
end
|
||||
end
|
||||
|
||||
sub_count = initdata
|
||||
.dig?("header", "c4TabbedHeaderRenderer", "subscriberCountText", "simpleText").try &.as_s?
|
||||
.try { |text| short_text_to_number(text.split(" ")[0]).to_i32 } || 0
|
||||
sub_count = 0
|
||||
|
||||
if (metadata_rows = initdata.dig?("header", "pageHeaderRenderer", "content", "pageHeaderViewModel", "metadata", "contentMetadataViewModel", "metadataRows").try &.as_a)
|
||||
metadata_rows.each do |row|
|
||||
metadata_part = row.dig?("metadataParts").try &.as_a.find { |i| i.dig?("text", "content").try &.as_s.includes?("subscribers") }
|
||||
if !metadata_part.nil?
|
||||
sub_count = short_text_to_number(metadata_part.dig("text", "content").as_s.split(" ")[0]).to_i32
|
||||
end
|
||||
break if sub_count != 0
|
||||
end
|
||||
end
|
||||
|
||||
AboutChannel.new(
|
||||
ucid: ucid,
|
||||
|
@ -1,4 +1,4 @@
|
||||
def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, sort_by = "newest", v2 = false)
|
||||
def produce_channel_content_continuation(ucid, content_type, page = 1, auto_generated = nil, sort_by = "newest", v2 = false)
|
||||
object_inner_2 = {
|
||||
"2:0:embedded" => {
|
||||
"1:0:varint" => 0_i64,
|
||||
@ -16,6 +16,13 @@ def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, so
|
||||
.try { |i| Base64.urlsafe_encode(i) }
|
||||
.try { |i| URI.encode_www_form(i) }
|
||||
|
||||
content_type_numerical =
|
||||
case content_type
|
||||
when "videos" then 15
|
||||
when "livestreams" then 14
|
||||
else 15 # Fallback to "videos"
|
||||
end
|
||||
|
||||
sort_by_numerical =
|
||||
case sort_by
|
||||
when "newest" then 1_i64
|
||||
@ -27,7 +34,7 @@ def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, so
|
||||
object_inner_1 = {
|
||||
"110:embedded" => {
|
||||
"3:embedded" => {
|
||||
"15:embedded" => {
|
||||
"#{content_type_numerical}:embedded" => {
|
||||
"1:embedded" => {
|
||||
"1:string" => object_inner_2_encoded,
|
||||
},
|
||||
@ -62,6 +69,10 @@ def produce_channel_videos_continuation(ucid, page = 1, auto_generated = nil, so
|
||||
return continuation
|
||||
end
|
||||
|
||||
def make_initial_content_ctoken(ucid, content_type, sort_by) : String
|
||||
return produce_channel_content_continuation(ucid, content_type, sort_by: sort_by)
|
||||
end
|
||||
|
||||
module Invidious::Channel::Tabs
|
||||
extend self
|
||||
|
||||
@ -69,10 +80,6 @@ module Invidious::Channel::Tabs
|
||||
# Regular videos
|
||||
# -------------------
|
||||
|
||||
def make_initial_video_ctoken(ucid, sort_by) : String
|
||||
return produce_channel_videos_continuation(ucid, sort_by: sort_by)
|
||||
end
|
||||
|
||||
# Wrapper for AboutChannel, as we still need to call get_videos with
|
||||
# an author name and ucid directly (e.g in RSS feeds).
|
||||
# TODO: figure out how to get rid of that
|
||||
@ -94,7 +101,7 @@ module Invidious::Channel::Tabs
|
||||
end
|
||||
|
||||
def get_videos(author : String, ucid : String, *, continuation : String? = nil, sort_by = "newest")
|
||||
continuation ||= make_initial_video_ctoken(ucid, sort_by)
|
||||
continuation ||= make_initial_content_ctoken(ucid, "videos", sort_by)
|
||||
initial_data = YoutubeAPI.browse(continuation: continuation)
|
||||
|
||||
return extract_items(initial_data, author, ucid)
|
||||
@ -138,21 +145,18 @@ module Invidious::Channel::Tabs
|
||||
# Livestreams
|
||||
# -------------------
|
||||
|
||||
def get_livestreams(channel : AboutChannel, continuation : String? = nil)
|
||||
if continuation.nil?
|
||||
# EgdzdHJlYW1z8gYECgJ6AA%3D%3D is the protobuf object to load "streams"
|
||||
initial_data = YoutubeAPI.browse(channel.ucid, params: "EgdzdHJlYW1z8gYECgJ6AA%3D%3D")
|
||||
else
|
||||
def get_livestreams(channel : AboutChannel, continuation : String? = nil, sort_by = "newest")
|
||||
continuation ||= make_initial_content_ctoken(channel.ucid, "livestreams", sort_by)
|
||||
|
||||
initial_data = YoutubeAPI.browse(continuation: continuation)
|
||||
end
|
||||
|
||||
return extract_items(initial_data, channel.author, channel.ucid)
|
||||
end
|
||||
|
||||
def get_60_livestreams(channel : AboutChannel, continuation : String? = nil)
|
||||
def get_60_livestreams(channel : AboutChannel, *, continuation : String? = nil, sort_by = "newest")
|
||||
if continuation.nil?
|
||||
# Fetch the first "page" of streams
|
||||
items, next_continuation = get_livestreams(channel)
|
||||
# Fetch the first "page" of stream
|
||||
items, next_continuation = get_livestreams(channel, sort_by: sort_by)
|
||||
else
|
||||
# Fetch a "page" of streams using the given continuation token
|
||||
items, next_continuation = get_livestreams(channel, continuation: continuation)
|
||||
|
@ -74,8 +74,6 @@ class Config
|
||||
# Database configuration using 12-Factor "Database URL" syntax
|
||||
@[YAML::Field(converter: Preferences::URIConverter)]
|
||||
property database_url : URI = URI.parse("")
|
||||
# Use polling to keep decryption function up to date
|
||||
property decrypt_polling : Bool = false
|
||||
# Used for crawling channels: threads should check all videos uploaded by a channel
|
||||
property full_refresh : Bool = false
|
||||
|
||||
|
@ -3,9 +3,9 @@
|
||||
# IPv6 addresses.
|
||||
#
|
||||
class TCPSocket
|
||||
def initialize(host : String, port, dns_timeout = nil, connect_timeout = nil, family = Socket::Family::UNSPEC)
|
||||
def initialize(host, port, dns_timeout = nil, connect_timeout = nil, blocking = false, family = Socket::Family::UNSPEC)
|
||||
Addrinfo.tcp(host, port, timeout: dns_timeout, family: family) do |addrinfo|
|
||||
super(addrinfo.family, addrinfo.type, addrinfo.protocol)
|
||||
super(addrinfo.family, addrinfo.type, addrinfo.protocol, blocking)
|
||||
connect(addrinfo, timeout: connect_timeout) do |error|
|
||||
close
|
||||
error
|
||||
@ -26,7 +26,7 @@ class HTTP::Client
|
||||
end
|
||||
|
||||
hostname = @host.starts_with?('[') && @host.ends_with?(']') ? @host[1..-2] : @host
|
||||
io = TCPSocket.new hostname, @port, @dns_timeout, @connect_timeout, @family
|
||||
io = TCPSocket.new hostname, @port, @dns_timeout, @connect_timeout, family: @family
|
||||
io.read_timeout = @read_timeout if @read_timeout
|
||||
io.write_timeout = @write_timeout if @write_timeout
|
||||
io.sync = false
|
||||
@ -35,7 +35,7 @@ class HTTP::Client
|
||||
if tls = @tls
|
||||
tcp_socket = io
|
||||
begin
|
||||
io = OpenSSL::SSL::Socket::Client.new(tcp_socket, context: tls, sync_close: true, hostname: @host)
|
||||
io = OpenSSL::SSL::Socket::Client.new(tcp_socket, context: tls, sync_close: true, hostname: @host.rchop('.'))
|
||||
rescue exc
|
||||
# don't leak the TCP socket when the SSL connection failed
|
||||
tcp_socket.close
|
||||
|
325
src/invidious/helpers/sig_helper.cr
Normal file
325
src/invidious/helpers/sig_helper.cr
Normal file
@ -0,0 +1,325 @@
|
||||
require "uri"
|
||||
require "socket"
|
||||
require "socket/tcp_socket"
|
||||
require "socket/unix_socket"
|
||||
|
||||
{% if flag?(:advanced_debug) %}
|
||||
require "io/hexdump"
|
||||
{% end %}
|
||||
|
||||
private alias NetworkEndian = IO::ByteFormat::NetworkEndian
|
||||
|
||||
class Invidious::SigHelper
|
||||
enum UpdateStatus
|
||||
Updated
|
||||
UpdateNotRequired
|
||||
Error
|
||||
end
|
||||
|
||||
# -------------------
|
||||
# Payload types
|
||||
# -------------------
|
||||
|
||||
abstract struct Payload
|
||||
end
|
||||
|
||||
struct StringPayload < Payload
|
||||
getter string : String
|
||||
|
||||
def initialize(str : String)
|
||||
raise Exception.new("SigHelper: String can't be empty") if str.empty?
|
||||
@string = str
|
||||
end
|
||||
|
||||
def self.from_bytes(slice : Bytes)
|
||||
size = IO::ByteFormat::NetworkEndian.decode(UInt16, slice)
|
||||
if size == 0 # Error code
|
||||
raise Exception.new("SigHelper: Server encountered an error")
|
||||
end
|
||||
|
||||
if (slice.bytesize - 2) != size
|
||||
raise Exception.new("SigHelper: String size mismatch")
|
||||
end
|
||||
|
||||
if str = String.new(slice[2..])
|
||||
return self.new(str)
|
||||
else
|
||||
raise Exception.new("SigHelper: Can't read string from socket")
|
||||
end
|
||||
end
|
||||
|
||||
def to_io(io)
|
||||
# `.to_u16` raises if there is an overflow during the conversion
|
||||
io.write_bytes(@string.bytesize.to_u16, NetworkEndian)
|
||||
io.write(@string.to_slice)
|
||||
end
|
||||
end
|
||||
|
||||
private enum Opcode
|
||||
FORCE_UPDATE = 0
|
||||
DECRYPT_N_SIGNATURE = 1
|
||||
DECRYPT_SIGNATURE = 2
|
||||
GET_SIGNATURE_TIMESTAMP = 3
|
||||
GET_PLAYER_STATUS = 4
|
||||
end
|
||||
|
||||
private record Request,
|
||||
opcode : Opcode,
|
||||
payload : Payload?
|
||||
|
||||
# ----------------------
|
||||
# High-level functions
|
||||
# ----------------------
|
||||
|
||||
module Client
|
||||
extend self
|
||||
|
||||
# Forces the server to re-fetch the YouTube player, and extract the necessary
|
||||
# components from it (nsig function code, sig function code, signature timestamp).
|
||||
def force_update : UpdateStatus
|
||||
request = Request.new(Opcode::FORCE_UPDATE, nil)
|
||||
|
||||
value = send_request(request) do |bytes|
|
||||
IO::ByteFormat::NetworkEndian.decode(UInt16, bytes)
|
||||
end
|
||||
|
||||
case value
|
||||
when 0x0000 then return UpdateStatus::Error
|
||||
when 0xFFFF then return UpdateStatus::UpdateNotRequired
|
||||
when 0xF44F then return UpdateStatus::Updated
|
||||
else
|
||||
code = value.nil? ? "nil" : value.to_s(base: 16)
|
||||
raise Exception.new("SigHelper: Invalid status code received #{code}")
|
||||
end
|
||||
end
|
||||
|
||||
# Decrypt a provided n signature using the server's current nsig function
|
||||
# code, and return the result (or an error).
|
||||
def decrypt_n_param(n : String) : String?
|
||||
request = Request.new(Opcode::DECRYPT_N_SIGNATURE, StringPayload.new(n))
|
||||
|
||||
n_dec = send_request(request) do |bytes|
|
||||
StringPayload.from_bytes(bytes).string
|
||||
end
|
||||
|
||||
return n_dec
|
||||
end
|
||||
|
||||
# Decrypt a provided s signature using the server's current sig function
|
||||
# code, and return the result (or an error).
|
||||
def decrypt_sig(sig : String) : String?
|
||||
request = Request.new(Opcode::DECRYPT_SIGNATURE, StringPayload.new(sig))
|
||||
|
||||
sig_dec = send_request(request) do |bytes|
|
||||
StringPayload.from_bytes(bytes).string
|
||||
end
|
||||
|
||||
return sig_dec
|
||||
end
|
||||
|
||||
# Return the signature timestamp from the server's current player
|
||||
def get_sts : UInt64?
|
||||
request = Request.new(Opcode::GET_SIGNATURE_TIMESTAMP, nil)
|
||||
|
||||
return send_request(request) do |bytes|
|
||||
IO::ByteFormat::NetworkEndian.decode(UInt64, bytes)
|
||||
end
|
||||
end
|
||||
|
||||
# Return the current player's version
|
||||
def get_player : UInt32?
|
||||
request = Request.new(Opcode::GET_PLAYER_STATUS, nil)
|
||||
|
||||
send_request(request) do |bytes|
|
||||
has_player = (bytes[0] == 0xFF)
|
||||
player_version = IO::ByteFormat::NetworkEndian.decode(UInt32, bytes[1..4])
|
||||
end
|
||||
|
||||
return has_player ? player_version : nil
|
||||
end
|
||||
|
||||
private def send_request(request : Request, &)
|
||||
channel = Multiplexor::INSTANCE.send(request)
|
||||
slice = channel.receive
|
||||
return yield slice
|
||||
rescue ex
|
||||
LOGGER.debug("SigHelper: Error when sending a request")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
# ---------------------
|
||||
# Low level functions
|
||||
# ---------------------
|
||||
|
||||
class Multiplexor
|
||||
alias TransactionID = UInt32
|
||||
record Transaction, channel = ::Channel(Bytes).new
|
||||
|
||||
@prng = Random.new
|
||||
@mutex = Mutex.new
|
||||
@queue = {} of TransactionID => Transaction
|
||||
|
||||
@conn : Connection
|
||||
|
||||
INSTANCE = new("")
|
||||
|
||||
def initialize(url : String)
|
||||
@conn = Connection.new(url)
|
||||
listen
|
||||
end
|
||||
|
||||
def listen : Nil
|
||||
raise "Socket is closed" if @conn.closed?
|
||||
|
||||
LOGGER.debug("SigHelper: Multiplexor listening")
|
||||
|
||||
# TODO: reopen socket if unexpectedly closed
|
||||
spawn do
|
||||
loop do
|
||||
receive_data
|
||||
Fiber.yield
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def send(request : Request)
|
||||
transaction = Transaction.new
|
||||
transaction_id = @prng.rand(TransactionID)
|
||||
|
||||
# Add transaction to queue
|
||||
@mutex.synchronize do
|
||||
# On a 32-bits random integer, this should never happen. Though, just in case, ...
|
||||
if @queue[transaction_id]?
|
||||
raise Exception.new("SigHelper: Duplicate transaction ID! You got a shiny pokemon!")
|
||||
end
|
||||
|
||||
@queue[transaction_id] = transaction
|
||||
end
|
||||
|
||||
write_packet(transaction_id, request)
|
||||
|
||||
return transaction.channel
|
||||
end
|
||||
|
||||
def receive_data
|
||||
transaction_id, slice = read_packet
|
||||
|
||||
@mutex.synchronize do
|
||||
if transaction = @queue.delete(transaction_id)
|
||||
# Remove transaction from queue and send data to the channel
|
||||
transaction.channel.send(slice)
|
||||
LOGGER.trace("SigHelper: Transaction unqueued and data sent to channel")
|
||||
else
|
||||
raise Exception.new("SigHelper: Received transaction was not in queue")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Read a single packet from the socket
|
||||
private def read_packet : {TransactionID, Bytes}
|
||||
# Header
|
||||
transaction_id = @conn.read_bytes(UInt32, NetworkEndian)
|
||||
length = @conn.read_bytes(UInt32, NetworkEndian)
|
||||
|
||||
LOGGER.trace("SigHelper: Recv transaction 0x#{transaction_id.to_s(base: 16)} / length #{length}")
|
||||
|
||||
if length > 67_000
|
||||
raise Exception.new("SigHelper: Packet longer than expected (#{length})")
|
||||
end
|
||||
|
||||
# Payload
|
||||
slice = Bytes.new(length)
|
||||
@conn.read(slice) if length > 0
|
||||
|
||||
LOGGER.trace("SigHelper: payload = #{slice}")
|
||||
LOGGER.trace("SigHelper: Recv transaction 0x#{transaction_id.to_s(base: 16)} - Done")
|
||||
|
||||
return transaction_id, slice
|
||||
end
|
||||
|
||||
# Write a single packet to the socket
|
||||
private def write_packet(transaction_id : TransactionID, request : Request)
|
||||
LOGGER.trace("SigHelper: Send transaction 0x#{transaction_id.to_s(base: 16)} / opcode #{request.opcode}")
|
||||
|
||||
io = IO::Memory.new(1024)
|
||||
io.write_bytes(request.opcode.to_u8, NetworkEndian)
|
||||
io.write_bytes(transaction_id, NetworkEndian)
|
||||
|
||||
if payload = request.payload
|
||||
payload.to_io(io)
|
||||
end
|
||||
|
||||
@conn.send(io)
|
||||
@conn.flush
|
||||
|
||||
LOGGER.trace("SigHelper: Send transaction 0x#{transaction_id.to_s(base: 16)} - Done")
|
||||
end
|
||||
end
|
||||
|
||||
class Connection
|
||||
@socket : UNIXSocket | TCPSocket
|
||||
|
||||
{% if flag?(:advanced_debug) %}
|
||||
@io : IO::Hexdump
|
||||
{% end %}
|
||||
|
||||
def initialize(host_or_path : String)
|
||||
if host_or_path.empty?
|
||||
host_or_path = "/tmp/inv_sig_helper.sock"
|
||||
end
|
||||
|
||||
case host_or_path
|
||||
when .starts_with?('/')
|
||||
@socket = UNIXSocket.new(host_or_path)
|
||||
when .starts_with?("tcp://")
|
||||
uri = URI.parse(host_or_path)
|
||||
@socket = TCPSocket.new(uri.host.not_nil!, uri.port.not_nil!)
|
||||
else
|
||||
uri = URI.parse("tcp://#{host_or_path}")
|
||||
@socket = TCPSocket.new(uri.host.not_nil!, uri.port.not_nil!)
|
||||
end
|
||||
|
||||
LOGGER.debug("SigHelper: Listening on '#{host_or_path}'")
|
||||
|
||||
{% if flag?(:advanced_debug) %}
|
||||
@io = IO::Hexdump.new(@socket, output: STDERR, read: true, write: true)
|
||||
{% end %}
|
||||
|
||||
@socket.sync = false
|
||||
@socket.blocking = false
|
||||
end
|
||||
|
||||
def closed? : Bool
|
||||
return @socket.closed?
|
||||
end
|
||||
|
||||
def close : Nil
|
||||
if @socket.closed?
|
||||
raise Exception.new("SigHelper: Can't close socket, it's already closed")
|
||||
else
|
||||
@socket.close
|
||||
end
|
||||
end
|
||||
|
||||
def flush(*args, **options)
|
||||
@socket.flush(*args, **options)
|
||||
end
|
||||
|
||||
def send(*args, **options)
|
||||
@socket.send(*args, **options)
|
||||
end
|
||||
|
||||
# Wrap IO functions, with added debug tooling if needed
|
||||
{% for function in %w(read read_bytes write write_bytes) %}
|
||||
def {{function.id}}(*args, **options)
|
||||
{% if flag?(:advanced_debug) %}
|
||||
@io.{{function.id}}(*args, **options)
|
||||
{% else %}
|
||||
@socket.{{function.id}}(*args, **options)
|
||||
{% end %}
|
||||
end
|
||||
{% end %}
|
||||
end
|
||||
end
|
@ -1,73 +1,46 @@
|
||||
alias SigProc = Proc(Array(String), Int32, Array(String))
|
||||
require "http/params"
|
||||
require "./sig_helper"
|
||||
|
||||
struct DecryptFunction
|
||||
@decrypt_function = [] of {SigProc, Int32}
|
||||
@decrypt_time = Time.monotonic
|
||||
struct Invidious::DecryptFunction
|
||||
@last_update = Time.monotonic - 42.days
|
||||
|
||||
def initialize(@use_polling = true)
|
||||
def initialize
|
||||
self.check_update
|
||||
end
|
||||
|
||||
def update_decrypt_function
|
||||
@decrypt_function = fetch_decrypt_function
|
||||
end
|
||||
|
||||
private def fetch_decrypt_function(id = "CvFH_6DNRCY")
|
||||
document = YT_POOL.client &.get("/watch?v=#{id}&gl=US&hl=en").body
|
||||
url = document.match(/src="(?<url>\/s\/player\/[^\/]+\/player_ias[^\/]+\/en_US\/base.js)"/).not_nil!["url"]
|
||||
player = YT_POOL.client &.get(url).body
|
||||
|
||||
function_name = player.match(/^(?<name>[^=]+)=function\(\w\){\w=\w\.split\(""\);[^\. ]+\.[^( ]+/m).not_nil!["name"]
|
||||
function_body = player.match(/^#{Regex.escape(function_name)}=function\(\w\){(?<body>[^}]+)}/m).not_nil!["body"]
|
||||
function_body = function_body.split(";")[1..-2]
|
||||
|
||||
var_name = function_body[0][0, 2]
|
||||
var_body = player.delete("\n").match(/var #{Regex.escape(var_name)}={(?<body>(.*?))};/).not_nil!["body"]
|
||||
|
||||
operations = {} of String => SigProc
|
||||
var_body.split("},").each do |operation|
|
||||
op_name = operation.match(/^[^:]+/).not_nil![0]
|
||||
op_body = operation.match(/\{[^}]+/).not_nil![0]
|
||||
|
||||
case op_body
|
||||
when "{a.reverse()"
|
||||
operations[op_name] = ->(a : Array(String), _b : Int32) { a.reverse }
|
||||
when "{a.splice(0,b)"
|
||||
operations[op_name] = ->(a : Array(String), b : Int32) { a.delete_at(0..(b - 1)); a }
|
||||
else
|
||||
operations[op_name] = ->(a : Array(String), b : Int32) { c = a[0]; a[0] = a[b % a.size]; a[b % a.size] = c; a }
|
||||
end
|
||||
end
|
||||
|
||||
decrypt_function = [] of {SigProc, Int32}
|
||||
function_body.each do |function|
|
||||
function = function.lchop(var_name).delete("[].")
|
||||
|
||||
op_name = function.match(/[^\(]+/).not_nil![0]
|
||||
value = function.match(/\(\w,(?<value>[\d]+)\)/).not_nil!["value"].to_i
|
||||
|
||||
decrypt_function << {operations[op_name], value}
|
||||
end
|
||||
|
||||
return decrypt_function
|
||||
end
|
||||
|
||||
def decrypt_signature(fmt : Hash(String, JSON::Any))
|
||||
return "" if !fmt["s"]? || !fmt["sp"]?
|
||||
|
||||
sp = fmt["sp"].as_s
|
||||
sig = fmt["s"].as_s.split("")
|
||||
if !@use_polling
|
||||
def check_update
|
||||
now = Time.monotonic
|
||||
if now - @decrypt_time > 60.seconds || @decrypt_function.size == 0
|
||||
@decrypt_function = fetch_decrypt_function
|
||||
@decrypt_time = Time.monotonic
|
||||
if (now - @last_update) > 60.seconds
|
||||
LOGGER.debug("Signature: Player might be outdated, updating")
|
||||
Invidious::SigHelper::Client.force_update
|
||||
@last_update = Time.monotonic
|
||||
end
|
||||
end
|
||||
|
||||
@decrypt_function.each do |proc, value|
|
||||
sig = proc.call(sig, value)
|
||||
def decrypt_nsig(n : String) : String?
|
||||
self.check_update
|
||||
return SigHelper::Client.decrypt_n_param(n)
|
||||
rescue ex
|
||||
LOGGER.debug(ex.message || "Signature: Unknown error")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return nil
|
||||
end
|
||||
|
||||
return "&#{sp}=#{sig.join("")}"
|
||||
def decrypt_signature(str : String) : String?
|
||||
self.check_update
|
||||
return SigHelper::Client.decrypt_sig(str)
|
||||
rescue ex
|
||||
LOGGER.debug(ex.message || "Signature: Unknown error")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return nil
|
||||
end
|
||||
|
||||
def get_sts : UInt64?
|
||||
self.check_update
|
||||
return SigHelper::Client.get_sts
|
||||
rescue ex
|
||||
LOGGER.debug(ex.message || "Signature: Unknown error")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
@ -11,11 +11,12 @@ module Invidious::HttpServer
|
||||
params = url.query_params
|
||||
params["host"] = url.host.not_nil! # Should never be nil, in theory
|
||||
params["region"] = region if !region.nil?
|
||||
url.query_params = params
|
||||
|
||||
if absolute
|
||||
return "#{HOST_URL}#{url.request_target}?#{params}"
|
||||
return "#{HOST_URL}#{url.request_target}"
|
||||
else
|
||||
return "#{url.request_target}?#{params}"
|
||||
return url.request_target
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
class Invidious::Jobs::UpdateDecryptFunctionJob < Invidious::Jobs::BaseJob
|
||||
def begin
|
||||
loop do
|
||||
begin
|
||||
DECRYPT_FUNCTION.update_decrypt_function
|
||||
rescue ex
|
||||
LOGGER.error("UpdateDecryptFunctionJob : #{ex.message}")
|
||||
ensure
|
||||
sleep 1.minute
|
||||
Fiber.yield
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -114,25 +114,31 @@ module Invidious::JSONify::APIv1
|
||||
|
||||
json.field "projectionType", fmt["projectionType"]
|
||||
|
||||
if fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt["itag"])
|
||||
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.as_i || 30
|
||||
height = fmt["height"]?.try &.as_i
|
||||
width = fmt["width"]?.try &.as_i
|
||||
|
||||
fps = fmt["fps"]?.try &.as_i
|
||||
|
||||
if fps
|
||||
json.field "fps", fps
|
||||
end
|
||||
|
||||
if height && width
|
||||
json.field "size", "#{width}x#{height}"
|
||||
json.field "resolution", "#{height}p"
|
||||
|
||||
quality_label = "#{width > height ? height : width}p"
|
||||
|
||||
if fps && fps > 30
|
||||
quality_label += fps.to_s
|
||||
end
|
||||
|
||||
json.field "qualityLabel", quality_label
|
||||
end
|
||||
|
||||
if fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt["itag"])
|
||||
json.field "container", fmt_info["ext"]
|
||||
json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"]
|
||||
|
||||
if fmt_info["height"]?
|
||||
json.field "resolution", "#{fmt_info["height"]}p"
|
||||
|
||||
quality_label = "#{fmt_info["height"]}p"
|
||||
if fps > 30
|
||||
quality_label += "60"
|
||||
end
|
||||
json.field "qualityLabel", quality_label
|
||||
|
||||
if fmt_info["width"]?
|
||||
json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Livestream chunk infos
|
||||
@ -163,26 +169,31 @@ module Invidious::JSONify::APIv1
|
||||
|
||||
json.field "bitrate", fmt["bitrate"].as_i.to_s if fmt["bitrate"]?
|
||||
|
||||
fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt["itag"])
|
||||
if fmt_info
|
||||
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.as_i || 30
|
||||
height = fmt["height"]?.try &.as_i
|
||||
width = fmt["width"]?.try &.as_i
|
||||
|
||||
fps = fmt["fps"]?.try &.as_i
|
||||
|
||||
if fps
|
||||
json.field "fps", fps
|
||||
end
|
||||
|
||||
if height && width
|
||||
json.field "size", "#{width}x#{height}"
|
||||
json.field "resolution", "#{height}p"
|
||||
|
||||
quality_label = "#{width > height ? height : width}p"
|
||||
|
||||
if fps && fps > 30
|
||||
quality_label += fps.to_s
|
||||
end
|
||||
|
||||
json.field "qualityLabel", quality_label
|
||||
end
|
||||
|
||||
if fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt["itag"])
|
||||
json.field "container", fmt_info["ext"]
|
||||
json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"]
|
||||
|
||||
if fmt_info["height"]?
|
||||
json.field "resolution", "#{fmt_info["height"]}p"
|
||||
|
||||
quality_label = "#{fmt_info["height"]}p"
|
||||
if fps > 30
|
||||
quality_label += "60"
|
||||
end
|
||||
json.field "qualityLabel", quality_label
|
||||
|
||||
if fmt_info["width"]?
|
||||
json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -366,6 +366,8 @@ def fetch_playlist(plid : String)
|
||||
|
||||
if text.includes? "video"
|
||||
video_count = text.gsub(/\D/, "").to_i? || 0
|
||||
elsif text.includes? "episode"
|
||||
video_count = text.gsub(/\D/, "").to_i? || 0
|
||||
elsif text.includes? "view"
|
||||
views = text.gsub(/\D/, "").to_i64? || 0_i64
|
||||
else
|
||||
|
@ -208,11 +208,12 @@ module Invidious::Routes::API::V1::Channels
|
||||
get_channel()
|
||||
|
||||
# Retrieve continuation from URL parameters
|
||||
sort_by = env.params.query["sort_by"]?.try &.downcase || "newest"
|
||||
continuation = env.params.query["continuation"]?
|
||||
|
||||
begin
|
||||
videos, next_continuation = Channel::Tabs.get_60_livestreams(
|
||||
channel, continuation: continuation
|
||||
channel, continuation: continuation, sort_by: sort_by
|
||||
)
|
||||
rescue ex
|
||||
return error_json(500, ex)
|
||||
|
@ -31,7 +31,7 @@ module Invidious::Routes::API::V1::Feeds
|
||||
|
||||
if !CONFIG.popular_enabled
|
||||
error_message = {"error" => "Administrator has disabled this endpoint."}.to_json
|
||||
haltf env, 400, error_message
|
||||
haltf env, 403, error_message
|
||||
end
|
||||
|
||||
JSON.build do |json|
|
||||
|
@ -74,7 +74,9 @@ module Invidious::Routes::API::V1::Misc
|
||||
response = playlist.to_json(offset, video_id: video_id)
|
||||
json_response = JSON.parse(response)
|
||||
|
||||
if json_response["videos"].as_a[0]["index"] != offset
|
||||
if json_response["videos"].as_a.empty?
|
||||
json_response = JSON.parse(response)
|
||||
elsif json_response["videos"].as_a[0]["index"] != offset
|
||||
offset = json_response["videos"].as_a[0]["index"].as_i
|
||||
lookback = offset < 50 ? offset : 50
|
||||
response = playlist.to_json(offset - lookback)
|
||||
|
@ -89,9 +89,14 @@ module Invidious::Routes::API::V1::Videos
|
||||
|
||||
if CONFIG.use_innertube_for_captions
|
||||
params = Invidious::Videos::Transcript.generate_param(id, caption.language_code, caption.auto_generated)
|
||||
initial_data = YoutubeAPI.get_transcript(params)
|
||||
|
||||
webvtt = Invidious::Videos::Transcript.convert_transcripts_to_vtt(initial_data, caption.language_code)
|
||||
transcript = Invidious::Videos::Transcript.from_raw(
|
||||
YoutubeAPI.get_transcript(params),
|
||||
caption.language_code,
|
||||
caption.auto_generated
|
||||
)
|
||||
|
||||
webvtt = transcript.to_vtt
|
||||
else
|
||||
# Timedtext API handling
|
||||
url = URI.parse("#{caption.base_url}&tlang=#{tlang}").request_target
|
||||
@ -136,7 +141,11 @@ module Invidious::Routes::API::V1::Videos
|
||||
end
|
||||
end
|
||||
else
|
||||
webvtt = YT_POOL.client &.get("#{url}&fmt=vtt").body
|
||||
uri = URI.parse(url)
|
||||
query_params = uri.query_params
|
||||
query_params["fmt"] = "vtt"
|
||||
uri.query_params = query_params
|
||||
webvtt = YT_POOL.client &.get(uri.request_target).body
|
||||
|
||||
if webvtt.starts_with?("<?xml")
|
||||
webvtt = caption.timedtext_to_vtt(webvtt)
|
||||
|
@ -30,7 +30,7 @@ module Invidious::Routes::BeforeAll
|
||||
|
||||
# Only allow the pages at /embed/* to be embedded
|
||||
if env.request.resource.starts_with?("/embed")
|
||||
frame_ancestors = "'self' http: https:"
|
||||
frame_ancestors = "'self' file: http: https:"
|
||||
else
|
||||
frame_ancestors = "'none'"
|
||||
end
|
||||
|
@ -81,13 +81,12 @@ module Invidious::Routes::Channels
|
||||
return env.redirect "/channel/#{channel.ucid}"
|
||||
end
|
||||
|
||||
# TODO: support sort option for livestreams
|
||||
sort_by = ""
|
||||
sort_options = [] of String
|
||||
sort_by = env.params.query["sort_by"]?.try &.downcase || "newest"
|
||||
sort_options = {"newest", "oldest", "popular"}
|
||||
|
||||
# Fetch items and continuation token
|
||||
items, next_continuation = Channel::Tabs.get_60_livestreams(
|
||||
channel, continuation: continuation
|
||||
channel, continuation: continuation, sort_by: sort_by
|
||||
)
|
||||
|
||||
selected_tab = Frontend::ChannelPage::TabsAvailable::Streams
|
||||
|
@ -214,7 +214,7 @@ module Invidious::Routes::PreferencesRoute
|
||||
statistics_enabled ||= "off"
|
||||
CONFIG.statistics_enabled = statistics_enabled == "on"
|
||||
|
||||
CONFIG.modified_source_code_url = env.params.body["modified_source_code_url"]?.try &.as(String)
|
||||
CONFIG.modified_source_code_url = env.params.body["modified_source_code_url"]?.presence
|
||||
|
||||
File.write("config/config.yml", CONFIG.to_yaml)
|
||||
end
|
||||
|
@ -1,3 +1,5 @@
|
||||
private DECRYPT_FUNCTION = IV::DecryptFunction.new
|
||||
|
||||
enum VideoType
|
||||
Video
|
||||
Livestream
|
||||
@ -98,20 +100,47 @@ struct Video
|
||||
|
||||
# Methods for parsing streaming data
|
||||
|
||||
def convert_url(fmt)
|
||||
if cfr = fmt["signatureCipher"]?.try { |h| HTTP::Params.parse(h.as_s) }
|
||||
sp = cfr["sp"]
|
||||
url = URI.parse(cfr["url"])
|
||||
params = url.query_params
|
||||
|
||||
LOGGER.debug("Videos: Decoding '#{cfr}'")
|
||||
|
||||
unsig = DECRYPT_FUNCTION.decrypt_signature(cfr["s"])
|
||||
params[sp] = unsig if unsig
|
||||
else
|
||||
url = URI.parse(fmt["url"].as_s)
|
||||
params = url.query_params
|
||||
end
|
||||
|
||||
n = DECRYPT_FUNCTION.decrypt_nsig(params["n"])
|
||||
params["n"] = n if n
|
||||
|
||||
params["host"] = url.host.not_nil!
|
||||
if region = self.info["region"]?.try &.as_s
|
||||
params["region"] = region
|
||||
end
|
||||
|
||||
url.query_params = params
|
||||
LOGGER.trace("Videos: new url is '#{url}'")
|
||||
|
||||
return url.to_s
|
||||
rescue ex
|
||||
LOGGER.debug("Videos: Error when parsing video URL")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return ""
|
||||
end
|
||||
|
||||
def fmt_stream
|
||||
return @fmt_stream.as(Array(Hash(String, JSON::Any))) if @fmt_stream
|
||||
|
||||
fmt_stream = info["streamingData"]?.try &.["formats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
fmt_stream.each do |fmt|
|
||||
if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) }
|
||||
s.each do |k, v|
|
||||
fmt[k] = JSON::Any.new(v)
|
||||
end
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}#{DECRYPT_FUNCTION.decrypt_signature(fmt)}")
|
||||
end
|
||||
fmt_stream = info.dig?("streamingData", "formats")
|
||||
.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}®ion=#{self.info["region"]}") if self.info["region"]?
|
||||
fmt_stream.each do |fmt|
|
||||
fmt["url"] = JSON::Any.new(self.convert_url(fmt))
|
||||
end
|
||||
|
||||
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
|
||||
@ -121,21 +150,17 @@ struct Video
|
||||
|
||||
def adaptive_fmts
|
||||
return @adaptive_fmts.as(Array(Hash(String, JSON::Any))) if @adaptive_fmts
|
||||
fmt_stream = info["streamingData"]?.try &.["adaptiveFormats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
fmt_stream.each do |fmt|
|
||||
if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) }
|
||||
s.each do |k, v|
|
||||
fmt[k] = JSON::Any.new(v)
|
||||
end
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}#{DECRYPT_FUNCTION.decrypt_signature(fmt)}")
|
||||
end
|
||||
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}®ion=#{self.info["region"]}") if self.info["region"]?
|
||||
fmt_stream = info.dig("streamingData", "adaptiveFormats")
|
||||
.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
|
||||
fmt_stream.each do |fmt|
|
||||
fmt["url"] = JSON::Any.new(self.convert_url(fmt))
|
||||
end
|
||||
|
||||
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
|
||||
@adaptive_fmts = fmt_stream
|
||||
|
||||
return @adaptive_fmts.as(Array(Hash(String, JSON::Any)))
|
||||
end
|
||||
|
||||
@ -250,7 +275,7 @@ struct Video
|
||||
end
|
||||
|
||||
def genre_url : String?
|
||||
info["genreUcid"]? ? "/channel/#{info["genreUcid"]}" : nil
|
||||
info["genreUcid"].try &.as_s? ? "/channel/#{info["genreUcid"]}" : nil
|
||||
end
|
||||
|
||||
def is_vr : Bool?
|
||||
|
@ -424,7 +424,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
|
||||
"shortDescription" => JSON::Any.new(short_description.try &.as_s || nil),
|
||||
# Video metadata
|
||||
"genre" => JSON::Any.new(genre.try &.as_s || ""),
|
||||
"genreUcid" => JSON::Any.new(genre_ucid.try &.as_s || ""),
|
||||
"genreUcid" => JSON::Any.new(genre_ucid.try &.as_s?),
|
||||
"license" => JSON::Any.new(license.try &.as_s || ""),
|
||||
# Music section
|
||||
"music" => JSON.parse(music_list.to_json),
|
||||
|
@ -1,8 +1,26 @@
|
||||
module Invidious::Videos
|
||||
# Namespace for methods primarily relating to Transcripts
|
||||
module Transcript
|
||||
record TranscriptLine, start_ms : Time::Span, end_ms : Time::Span, line : String
|
||||
# A `Transcripts` struct encapsulates a sequence of lines that together forms the whole transcript for a given YouTube video.
|
||||
# These lines can be categorized into two types: section headings and regular lines representing content from the video.
|
||||
struct Transcript
|
||||
# Types
|
||||
record HeadingLine, start_ms : Time::Span, end_ms : Time::Span, line : String
|
||||
record RegularLine, start_ms : Time::Span, end_ms : Time::Span, line : String
|
||||
alias TranscriptLine = HeadingLine | RegularLine
|
||||
|
||||
property lines : Array(TranscriptLine)
|
||||
|
||||
property language_code : String
|
||||
property auto_generated : Bool
|
||||
|
||||
# User friendly label for the current transcript.
|
||||
# Example: "English (auto-generated)"
|
||||
property label : String
|
||||
|
||||
# Initializes a new Transcript struct with the contents and associated metadata describing it
|
||||
def initialize(@lines : Array(TranscriptLine), @language_code : String, @auto_generated : Bool, @label : String)
|
||||
end
|
||||
|
||||
# Generates a protobuf string to fetch the requested transcript from YouTube
|
||||
def self.generate_param(video_id : String, language_code : String, auto_generated : Bool) : String
|
||||
kind = auto_generated ? "asr" : ""
|
||||
|
||||
@ -30,48 +48,79 @@ module Invidious::Videos
|
||||
return params
|
||||
end
|
||||
|
||||
def self.convert_transcripts_to_vtt(initial_data : Hash(String, JSON::Any), target_language : String) : String
|
||||
# Convert into array of TranscriptLine
|
||||
lines = self.parse(initial_data)
|
||||
# Constructs a Transcripts struct from the initial YouTube response
|
||||
def self.from_raw(initial_data : Hash(String, JSON::Any), language_code : String, auto_generated : Bool)
|
||||
transcript_panel = initial_data.dig("actions", 0, "updateEngagementPanelAction", "content", "transcriptRenderer",
|
||||
"content", "transcriptSearchPanelRenderer")
|
||||
|
||||
segment_list = transcript_panel.dig("body", "transcriptSegmentListRenderer")
|
||||
|
||||
if !segment_list["initialSegments"]?
|
||||
raise NotFoundException.new("Requested transcript does not exist")
|
||||
end
|
||||
|
||||
# Extract user-friendly label for the current transcript
|
||||
|
||||
footer_language_menu = transcript_panel.dig?(
|
||||
"footer", "transcriptFooterRenderer", "languageMenu", "sortFilterSubMenuRenderer", "subMenuItems"
|
||||
)
|
||||
|
||||
if footer_language_menu
|
||||
label = footer_language_menu.as_a.select(&.["selected"].as_bool)[0]["title"].as_s
|
||||
else
|
||||
label = language_code
|
||||
end
|
||||
|
||||
# Extract transcript lines
|
||||
|
||||
initial_segments = segment_list["initialSegments"].as_a
|
||||
|
||||
lines = [] of TranscriptLine
|
||||
|
||||
initial_segments.each do |line|
|
||||
if unpacked_line = line["transcriptSectionHeaderRenderer"]?
|
||||
line_type = HeadingLine
|
||||
else
|
||||
unpacked_line = line["transcriptSegmentRenderer"]
|
||||
line_type = RegularLine
|
||||
end
|
||||
|
||||
start_ms = unpacked_line["startMs"].as_s.to_i.millisecond
|
||||
end_ms = unpacked_line["endMs"].as_s.to_i.millisecond
|
||||
text = extract_text(unpacked_line["snippet"]) || ""
|
||||
|
||||
lines << line_type.new(start_ms, end_ms, text)
|
||||
end
|
||||
|
||||
return Transcript.new(
|
||||
lines: lines,
|
||||
language_code: language_code,
|
||||
auto_generated: auto_generated,
|
||||
label: label
|
||||
)
|
||||
end
|
||||
|
||||
# Converts transcript lines to a WebVTT file
|
||||
#
|
||||
# This is used within Invidious to replace subtitles
|
||||
# as to workaround YouTube's rate-limited timedtext endpoint.
|
||||
def to_vtt
|
||||
settings_field = {
|
||||
"Kind" => "captions",
|
||||
"Language" => target_language,
|
||||
"Language" => @language_code,
|
||||
}
|
||||
|
||||
# Taken from Invidious::Videos::Captions::Metadata.timedtext_to_vtt()
|
||||
vtt = WebVTT.build(settings_field) do |vtt|
|
||||
lines.each do |line|
|
||||
@lines.each do |line|
|
||||
# Section headers are excluded from the VTT conversion as to
|
||||
# match the regular captions returned from YouTube as much as possible
|
||||
next if line.is_a? HeadingLine
|
||||
|
||||
vtt.cue(line.start_ms, line.end_ms, line.line)
|
||||
end
|
||||
end
|
||||
|
||||
return vtt
|
||||
end
|
||||
|
||||
private def self.parse(initial_data : Hash(String, JSON::Any))
|
||||
body = initial_data.dig("actions", 0, "updateEngagementPanelAction", "content", "transcriptRenderer",
|
||||
"content", "transcriptSearchPanelRenderer", "body", "transcriptSegmentListRenderer",
|
||||
"initialSegments").as_a
|
||||
|
||||
lines = [] of TranscriptLine
|
||||
body.each do |line|
|
||||
# Transcript section headers. They are not apart of the captions and as such we can safely skip them.
|
||||
if line.as_h.has_key?("transcriptSectionHeaderRenderer")
|
||||
next
|
||||
end
|
||||
|
||||
line = line["transcriptSegmentRenderer"]
|
||||
|
||||
start_ms = line["startMs"].as_s.to_i.millisecond
|
||||
end_ms = line["endMs"].as_s.to_i.millisecond
|
||||
|
||||
text = extract_text(line["snippet"]) || ""
|
||||
|
||||
lines << TranscriptLine.new(start_ms, end_ms, text)
|
||||
end
|
||||
|
||||
return lines
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -30,13 +30,13 @@
|
||||
<meta property="og:site_name" content="Invidious">
|
||||
<meta property="og:url" content="<%= HOST_URL %>/channel/<%= ucid %>">
|
||||
<meta property="og:title" content="<%= author %>">
|
||||
<meta property="og:image" content="/ggpht<%= channel_profile_pic %>">
|
||||
<meta property="og:image" content="<%= HOST_URL %>/ggpht<%= channel_profile_pic %>">
|
||||
<meta property="og:description" content="<%= channel.description %>">
|
||||
<meta name="twitter:card" content="summary">
|
||||
<meta name="twitter:url" content="<%= HOST_URL %>/channel/<%= ucid %>">
|
||||
<meta name="twitter:title" content="<%= author %>">
|
||||
<meta name="twitter:description" content="<%= channel.description %>">
|
||||
<meta name="twitter:image" content="/ggpht<%= channel_profile_pic %>">
|
||||
<meta name="twitter:image" content="<%= HOST_URL %>/ggpht<%= channel_profile_pic %>">
|
||||
<link rel="alternate" type="application/rss+xml" title="RSS" href="/feed/channel/<%= ucid %>" />
|
||||
<%- end -%>
|
||||
|
||||
|
@ -310,7 +310,7 @@
|
||||
|
||||
<div class="pure-control-group">
|
||||
<label for="modified_source_code_url"><%= translate(locale, "adminprefs_modified_source_code_url_label") %></label>
|
||||
<input name="modified_source_code_url" id="modified_source_code_url" type="input" <% if CONFIG.modified_source_code_url %>checked<% end %>>
|
||||
<input name="modified_source_code_url" id="modified_source_code_url" type="url" value="<%= CONFIG.modified_source_code_url %>">
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
<meta property="og:site_name" content="<%= author %> | Invidious">
|
||||
<meta property="og:url" content="<%= HOST_URL %>/watch?v=<%= video.id %>">
|
||||
<meta property="og:title" content="<%= title %>">
|
||||
<meta property="og:image" content="/vi/<%= video.id %>/maxres.jpg">
|
||||
<meta property="og:image" content="<%= HOST_URL %>/vi/<%= video.id %>/maxres.jpg">
|
||||
<meta property="og:description" content="<%= HTML.escape(video.short_description) %>">
|
||||
<meta property="og:type" content="video.other">
|
||||
<meta property="og:video:url" content="<%= HOST_URL %>/embed/<%= video.id %>">
|
||||
|
@ -2,12 +2,11 @@
|
||||
# This file contains youtube API wrappers
|
||||
#
|
||||
|
||||
private STS_FETCHER = IV::DecryptFunction.new
|
||||
|
||||
module YoutubeAPI
|
||||
extend self
|
||||
|
||||
private DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
|
||||
private ANDROID_API_KEY = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w"
|
||||
|
||||
# For Android versions, see https://en.wikipedia.org/wiki/Android_version_history
|
||||
private ANDROID_APP_VERSION = "19.14.42"
|
||||
private ANDROID_USER_AGENT = "com.google.android.youtube/19.14.42 (Linux; U; Android 12; US) gzip"
|
||||
@ -52,7 +51,6 @@ module YoutubeAPI
|
||||
name: "WEB",
|
||||
name_proto: "1",
|
||||
version: "2.20240304.00.00",
|
||||
api_key: DEFAULT_API_KEY,
|
||||
screen: "WATCH_FULL_SCREEN",
|
||||
os_name: "Windows",
|
||||
os_version: WINDOWS_VERSION,
|
||||
@ -62,7 +60,6 @@ module YoutubeAPI
|
||||
name: "WEB_EMBEDDED_PLAYER",
|
||||
name_proto: "56",
|
||||
version: "1.20240303.00.00",
|
||||
api_key: DEFAULT_API_KEY,
|
||||
screen: "EMBED",
|
||||
os_name: "Windows",
|
||||
os_version: WINDOWS_VERSION,
|
||||
@ -72,7 +69,6 @@ module YoutubeAPI
|
||||
name: "MWEB",
|
||||
name_proto: "2",
|
||||
version: "2.20240304.08.00",
|
||||
api_key: DEFAULT_API_KEY,
|
||||
os_name: "Android",
|
||||
os_version: ANDROID_VERSION,
|
||||
platform: "MOBILE",
|
||||
@ -81,7 +77,6 @@ module YoutubeAPI
|
||||
name: "WEB",
|
||||
name_proto: "1",
|
||||
version: "2.20240304.00.00",
|
||||
api_key: DEFAULT_API_KEY,
|
||||
screen: "EMBED",
|
||||
os_name: "Windows",
|
||||
os_version: WINDOWS_VERSION,
|
||||
@ -94,7 +89,6 @@ module YoutubeAPI
|
||||
name: "ANDROID",
|
||||
name_proto: "3",
|
||||
version: ANDROID_APP_VERSION,
|
||||
api_key: ANDROID_API_KEY,
|
||||
android_sdk_version: ANDROID_SDK_VERSION,
|
||||
user_agent: ANDROID_USER_AGENT,
|
||||
os_name: "Android",
|
||||
@ -105,13 +99,11 @@ module YoutubeAPI
|
||||
name: "ANDROID_EMBEDDED_PLAYER",
|
||||
name_proto: "55",
|
||||
version: ANDROID_APP_VERSION,
|
||||
api_key: "AIzaSyCjc_pVEDi4qsv5MtC2dMXzpIaDoRFLsxw",
|
||||
},
|
||||
ClientType::AndroidScreenEmbed => {
|
||||
name: "ANDROID",
|
||||
name_proto: "3",
|
||||
version: ANDROID_APP_VERSION,
|
||||
api_key: DEFAULT_API_KEY,
|
||||
screen: "EMBED",
|
||||
android_sdk_version: ANDROID_SDK_VERSION,
|
||||
user_agent: ANDROID_USER_AGENT,
|
||||
@ -123,7 +115,6 @@ module YoutubeAPI
|
||||
name: "ANDROID_TESTSUITE",
|
||||
name_proto: "30",
|
||||
version: ANDROID_TS_APP_VERSION,
|
||||
api_key: ANDROID_API_KEY,
|
||||
android_sdk_version: ANDROID_SDK_VERSION,
|
||||
user_agent: ANDROID_TS_USER_AGENT,
|
||||
os_name: "Android",
|
||||
@ -137,7 +128,6 @@ module YoutubeAPI
|
||||
name: "IOS",
|
||||
name_proto: "5",
|
||||
version: IOS_APP_VERSION,
|
||||
api_key: "AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc",
|
||||
user_agent: IOS_USER_AGENT,
|
||||
device_make: "Apple",
|
||||
device_model: "iPhone14,5",
|
||||
@ -149,7 +139,6 @@ module YoutubeAPI
|
||||
name: "IOS_MESSAGES_EXTENSION",
|
||||
name_proto: "66",
|
||||
version: IOS_APP_VERSION,
|
||||
api_key: DEFAULT_API_KEY,
|
||||
user_agent: IOS_USER_AGENT,
|
||||
device_make: "Apple",
|
||||
device_model: "iPhone14,5",
|
||||
@ -161,7 +150,6 @@ module YoutubeAPI
|
||||
name: "IOS_MUSIC",
|
||||
name_proto: "26",
|
||||
version: "6.42",
|
||||
api_key: "AIzaSyBAETezhkwP0ZWA02RsqT1zu78Fpt0bC_s",
|
||||
user_agent: "com.google.ios.youtubemusic/6.42 (iPhone14,5; U; CPU iOS 17_4 like Mac OS X;)",
|
||||
device_make: "Apple",
|
||||
device_model: "iPhone14,5",
|
||||
@ -176,13 +164,11 @@ module YoutubeAPI
|
||||
name: "TVHTML5",
|
||||
name_proto: "7",
|
||||
version: "7.20240304.10.00",
|
||||
api_key: DEFAULT_API_KEY,
|
||||
},
|
||||
ClientType::TvHtml5ScreenEmbed => {
|
||||
name: "TVHTML5_SIMPLY_EMBEDDED_PLAYER",
|
||||
name_proto: "85",
|
||||
version: "2.0",
|
||||
api_key: DEFAULT_API_KEY,
|
||||
screen: "EMBED",
|
||||
},
|
||||
}
|
||||
@ -237,11 +223,6 @@ module YoutubeAPI
|
||||
HARDCODED_CLIENTS[@client_type][:version]
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def api_key : String
|
||||
HARDCODED_CLIENTS[@client_type][:api_key]
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def screen : String
|
||||
HARDCODED_CLIENTS[@client_type][:screen]? || ""
|
||||
@ -293,7 +274,7 @@ module YoutubeAPI
|
||||
# Return, as a Hash, the "context" data required to request the
|
||||
# youtube API endpoints.
|
||||
#
|
||||
private def make_context(client_config : ClientConfig | Nil) : Hash
|
||||
private def make_context(client_config : ClientConfig | Nil, video_id = "dQw4w9WgXcQ") : Hash
|
||||
# Use the default client config if nil is passed
|
||||
client_config ||= DEFAULT_CLIENT_CONFIG
|
||||
|
||||
@ -313,7 +294,7 @@ module YoutubeAPI
|
||||
|
||||
if client_config.screen == "EMBED"
|
||||
client_context["thirdParty"] = {
|
||||
"embedUrl" => "https://www.youtube.com/embed/dQw4w9WgXcQ",
|
||||
"embedUrl" => "https://www.youtube.com/embed/#{video_id}",
|
||||
} of String => String | Int64
|
||||
end
|
||||
|
||||
@ -474,19 +455,29 @@ module YoutubeAPI
|
||||
params : String,
|
||||
client_config : ClientConfig | Nil = nil
|
||||
)
|
||||
# Playback context, separate because it can be different between clients
|
||||
playback_ctx = {
|
||||
"html5Preference" => "HTML5_PREF_WANTS",
|
||||
"referer" => "https://www.youtube.com/watch?v=#{video_id}",
|
||||
} of String => String | Int64
|
||||
|
||||
if {"WEB", "TVHTML5"}.any? { |s| client_config.name.starts_with? s }
|
||||
if sts = STS_FETCHER.get_sts
|
||||
playback_ctx["signatureTimestamp"] = sts.to_i64
|
||||
end
|
||||
end
|
||||
|
||||
# JSON Request data, required by the API
|
||||
data = {
|
||||
"contentCheckOk" => true,
|
||||
"videoId" => video_id,
|
||||
"context" => self.make_context(client_config),
|
||||
"context" => self.make_context(client_config, video_id),
|
||||
"racyCheckOk" => true,
|
||||
"user" => {
|
||||
"lockedSafetyMode" => false,
|
||||
},
|
||||
"playbackContext" => {
|
||||
"contentPlaybackContext" => {
|
||||
"html5Preference": "HTML5_PREF_WANTS",
|
||||
},
|
||||
"contentPlaybackContext" => playback_ctx,
|
||||
},
|
||||
}
|
||||
|
||||
@ -606,7 +597,7 @@ module YoutubeAPI
|
||||
client_config ||= DEFAULT_CLIENT_CONFIG
|
||||
|
||||
# Query parameters
|
||||
url = "#{endpoint}?key=#{client_config.api_key}&prettyPrint=false"
|
||||
url = "#{endpoint}?prettyPrint=false"
|
||||
|
||||
headers = HTTP::Headers{
|
||||
"Content-Type" => "application/json; charset=UTF-8",
|
||||
|
Loading…
Reference in New Issue
Block a user