Storyboards: Use replace the NamedTuple by a struct

This commit is contained in:
Samantaz Fox 2023-10-08 20:09:38 +02:00
parent 6878822c4d
commit 8327862697
No known key found for this signature in database
GPG key ID: F42821059186176E
3 changed files with 53 additions and 45 deletions

View file

@ -273,15 +273,15 @@ module Invidious::JSONify::APIv1
json.array do json.array do
storyboards.each do |storyboard| storyboards.each do |storyboard|
json.object do json.object do
json.field "url", "/api/v1/storyboards/#{id}?width=#{storyboard[:width]}&height=#{storyboard[:height]}" json.field "url", "/api/v1/storyboards/#{id}?width=#{storyboard.width}&height=#{storyboard.height}"
json.field "templateUrl", storyboard[:url] json.field "templateUrl", storyboard.url
json.field "width", storyboard[:width] json.field "width", storyboard.width
json.field "height", storyboard[:height] json.field "height", storyboard.height
json.field "count", storyboard[:count] json.field "count", storyboard.count
json.field "interval", storyboard[:interval] json.field "interval", storyboard.interval
json.field "storyboardWidth", storyboard[:storyboard_width] json.field "storyboardWidth", storyboard.storyboard_width
json.field "storyboardHeight", storyboard[:storyboard_height] json.field "storyboardHeight", storyboard.storyboard_height
json.field "storyboardCount", storyboard[:storyboard_count] json.field "storyboardCount", storyboard.storyboard_count
end end
end end
end end

View file

@ -205,7 +205,7 @@ module Invidious::Routes::API::V1::Videos
env.response.content_type = "text/vtt" env.response.content_type = "text/vtt"
storyboard = storyboards.select { |sb| width == "#{sb[:width]}" || height == "#{sb[:height]}" } storyboard = storyboards.select { |sb| width == "#{sb.width}" || height == "#{sb.height}" }
if storyboard.empty? if storyboard.empty?
haltf env, 404 haltf env, 404
@ -215,21 +215,22 @@ module Invidious::Routes::API::V1::Videos
WebVTT.build do |vtt| WebVTT.build do |vtt|
start_time = 0.milliseconds start_time = 0.milliseconds
end_time = storyboard[:interval].milliseconds end_time = storyboard.interval.milliseconds
storyboard[:storyboard_count].times do |i| storyboard.storyboard_count.times do |i|
url = storyboard[:url] url = storyboard.url
authority = /(i\d?).ytimg.com/.match!(url)[1]? authority = /(i\d?).ytimg.com/.match!(url)[1]?
url = url.gsub("$M", i).gsub(%r(https://i\d?.ytimg.com/sb/), "") url = url.gsub("$M", i).gsub(%r(https://i\d?.ytimg.com/sb/), "")
url = "#{HOST_URL}/sb/#{authority}/#{url}" url = "#{HOST_URL}/sb/#{authority}/#{url}"
storyboard[:storyboard_height].times do |j| storyboard.storyboard_height.times do |j|
storyboard[:storyboard_width].times do |k| storyboard.storyboard_width.times do |k|
current_cue_url = "#{url}#xywh=#{storyboard[:width] * k},#{storyboard[:height] * j},#{storyboard[:width] - 2},#{storyboard[:height]}" current_cue_url = "#{url}#xywh=#{storyboard.width * k},#{storyboard.height * j},#{storyboard.width - 2},#{storyboard.height}"
vtt.cue(start_time, end_time, current_cue_url) vtt.cue(start_time, end_time, current_cue_url)
start_time += storyboard[:interval].milliseconds start_time += storyboard.interval.milliseconds
end_time += storyboard[:interval].milliseconds end_time += storyboard.interval.milliseconds
end end
end end
end end

View file

@ -3,6 +3,21 @@ require "http/params"
module Invidious::Videos module Invidious::Videos
struct Storyboard struct Storyboard
getter url : String
getter width : Int32
getter height : Int32
getter count : Int32
getter interval : Int32
getter storyboard_width : Int32
getter storyboard_height : Int32
getter storyboard_count : Int32
def initialize(
*, @url, @width, @height, @count, @interval,
@storyboard_width, @storyboard_height, @storyboard_count
)
end
# Parse the JSON structure from Youtube # Parse the JSON structure from Youtube
def self.from_yt_json(container : JSON::Any) def self.from_yt_json(container : JSON::Any)
storyboards = container.dig?("playerStoryboardSpecRenderer", "spec") storyboards = container.dig?("playerStoryboardSpecRenderer", "spec")
@ -10,28 +25,20 @@ module Invidious::Videos
if !storyboards if !storyboards
if storyboard = container.dig?("playerLiveStoryboardSpecRenderer", "spec").try &.as_s if storyboard = container.dig?("playerLiveStoryboardSpecRenderer", "spec").try &.as_s
return [{ return [Storyboard.new(
url: storyboard.split("#")[0], url: storyboard.split("#")[0],
width: 106, width: 106,
height: 60, height: 60,
count: -1, count: -1,
interval: 5000, interval: 5000,
storyboard_width: 3, storyboard_width: 3,
storyboard_height: 3, storyboard_height: 3,
storyboard_count: -1, storyboard_count: -1,
}] )]
end end
end end
items = [] of NamedTuple( items = [] of Storyboard
url: String,
width: Int32,
height: Int32,
count: Int32,
interval: Int32,
storyboard_width: Int32,
storyboard_height: Int32,
storyboard_count: Int32)
return items if !storyboards return items if !storyboards
@ -51,16 +58,16 @@ module Invidious::Videos
storyboard_height = storyboard_height.to_i storyboard_height = storyboard_height.to_i
storyboard_count = (count / (storyboard_width * storyboard_height)).ceil.to_i storyboard_count = (count / (storyboard_width * storyboard_height)).ceil.to_i
items << { items << Storyboard.new(
url: url.to_s.sub("$L", i).sub("$N", "M$M"), url: url.to_s.sub("$L", i).sub("$N", "M$M"),
width: width, width: width,
height: height, height: height,
count: count, count: count,
interval: interval, interval: interval,
storyboard_width: storyboard_width, storyboard_width: storyboard_width,
storyboard_height: storyboard_height, storyboard_height: storyboard_height,
storyboard_count: storyboard_count, storyboard_count: storyboard_count
} )
end end
items items