Encapsulate videos parser and clip functions inside it's own Invidious::Videos::Parser and Invidious::Videos::Clip module (#5745)

Part of https://github.com/iv-org/invidious/issues/5744
This commit is contained in:
Fijxu
2026-05-28 13:11:41 -04:00
committed by GitHub
parent 8b183caa2a
commit 4ae227ce91
9 changed files with 603 additions and 591 deletions

View File

@@ -7,7 +7,7 @@ Spectator.describe "parse_video_info" do
_next = load_mock("video/regular_mrbeast.next")
raw_data = _player.merge!(_next)
info = parse_video_info("2isYuQZMbdU", raw_data)
info = Invidious::Videos::Parser.parse_video_info("2isYuQZMbdU", raw_data)
# Some basic verifications
expect(typeof(info)).to eq(Hash(String, JSON::Any))
@@ -88,7 +88,7 @@ Spectator.describe "parse_video_info" do
_next = load_mock("video/regular_no-description.next")
raw_data = _player.merge!(_next)
info = parse_video_info("iuevw6218F0", raw_data)
info = Invidious::Videos::Parser.parse_video_info("iuevw6218F0", raw_data)
# Some basic verifications
expect(typeof(info)).to eq(Hash(String, JSON::Any))

View File

@@ -7,7 +7,7 @@ Spectator.describe "parse_video_info" do
_next = load_mock("video/scheduled_live_PBD-Podcast.next")
raw_data = _player.merge!(_next)
info = parse_video_info("N-yVic7BbY0", raw_data)
info = Invidious::Videos::Parser.parse_video_info("N-yVic7BbY0", raw_data)
# Some basic verifications
expect(typeof(info)).to eq(Hash(String, JSON::Any))

View File

@@ -410,7 +410,7 @@ module Invidious::Routes::API::V1::Videos
clip_title = nil
if params = response.dig?("endpoint", "watchEndpoint", "params").try &.as_s
start_time, end_time, clip_title = parse_clip_parameters(params)
start_time, end_time, clip_title = Invidious::Videos::Clip.parse_clip_parameters(params)
end
begin

View File

@@ -122,7 +122,7 @@ module Invidious::Routes::Embed
else nil # Continue
end
params = process_video_params(env.params.query, preferences)
params = Invidious::Videos.process_video_params(env.params.query, preferences)
user = env.get?("user").try &.as(User)
if user

View File

@@ -47,7 +47,7 @@ module Invidious::Routes::Watch
end
subscriptions ||= [] of String
params = process_video_params(env.params.query, preferences)
params = Invidious::Videos.process_video_params(env.params.query, preferences)
env.params.query.delete_all("listen")
begin
@@ -273,7 +273,7 @@ module Invidious::Routes::Watch
if video_id = response.dig?("endpoint", "watchEndpoint", "videoId")
if params = response.dig?("endpoint", "watchEndpoint", "params").try &.as_s
start_time, end_time, _ = parse_clip_parameters(params)
start_time, end_time, _ = Invidious::Videos::Clip.parse_clip_parameters(params)
env.params.query["start"] = start_time.to_s if start_time != nil
env.params.query["end"] = end_time.to_s if end_time != nil
end

View File

@@ -324,7 +324,7 @@ rescue DB::Error
end
def fetch_video(id, region)
info = extract_video_info(video_id: id)
info = Invidious::Videos::Parser.extract_video_info(video_id: id)
if info.nil?
raise InfoException.new("Invidious companion is not available. \

View File

@@ -1,5 +1,8 @@
require "json"
module Invidious::Videos::Clip
extend self
# returns start_time, end_time and clip_title
def parse_clip_parameters(params) : {Float64?, Float64?, String?}
decoded_protobuf = params.try { |i| URI.decode_www_form(i) }
@@ -20,3 +23,4 @@ def parse_clip_parameters(params) : {Float64?, Float64?, String?}
return start_time, end_time, clip_title
end
end

View File

@@ -1,5 +1,8 @@
require "json"
module Invidious::Videos::Parser
extend self
# Use to parse both "compactVideoRenderer" and "endScreenVideoRenderer".
# The former is preferred as it has more videos in it. The second has
# the same 11 first entries as the compact rendered.
@@ -103,7 +106,7 @@ def extract_video_info(video_id : String)
player_response = player_response.merge(next_response)
end
params = parse_video_info(video_id, player_response)
params = self.parse_video_info(video_id, player_response)
params["reason"] = JSON::Any.new(reason) if reason
{"captions", "playabilityStatus", "playerConfig", "storyboards"}.each do |f|
@@ -242,7 +245,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
.dig?("secondaryResults", "secondaryResults", "results")
secondary_results.try &.as_a.each do |element|
if item = element["compactVideoRenderer"]?
related_video = parse_related_video(item)
related_video = self.parse_related_video(item)
related << JSON::Any.new(related_video) if related_video
end
end
@@ -257,7 +260,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
player_overlays.try &.as_a.each do |element|
if item = element["endScreenVideoRenderer"]?
related_video = parse_related_video(item)
related_video = self.parse_related_video(item)
related << JSON::Any.new(related_video) if related_video
end
end
@@ -463,3 +466,4 @@ rescue ex
LOGGER.trace(ex.inspect_with_backtrace)
return ""
end
end

View File

@@ -1,3 +1,6 @@
module Invidious::Videos
extend self
struct VideoPreferences
include JSON::Serializable
@@ -160,3 +163,4 @@ def process_video_params(query, preferences)
return params
end
end