From 8327862697774cd8076335fe2002875dd8c5a84a Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sun, 8 Oct 2023 20:09:38 +0200 Subject: [PATCH] Storyboards: Use replace the NamedTuple by a struct --- src/invidious/jsonify/api_v1/video_json.cr | 18 +++---- src/invidious/routes/api/v1/videos.cr | 19 +++---- src/invidious/videos/storyboard.cr | 61 ++++++++++++---------- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/invidious/jsonify/api_v1/video_json.cr b/src/invidious/jsonify/api_v1/video_json.cr index 59714828..44a34b18 100644 --- a/src/invidious/jsonify/api_v1/video_json.cr +++ b/src/invidious/jsonify/api_v1/video_json.cr @@ -273,15 +273,15 @@ module Invidious::JSONify::APIv1 json.array do storyboards.each do |storyboard| json.object do - json.field "url", "/api/v1/storyboards/#{id}?width=#{storyboard[:width]}&height=#{storyboard[:height]}" - json.field "templateUrl", storyboard[:url] - json.field "width", storyboard[:width] - json.field "height", storyboard[:height] - json.field "count", storyboard[:count] - json.field "interval", storyboard[:interval] - json.field "storyboardWidth", storyboard[:storyboard_width] - json.field "storyboardHeight", storyboard[:storyboard_height] - json.field "storyboardCount", storyboard[:storyboard_count] + json.field "url", "/api/v1/storyboards/#{id}?width=#{storyboard.width}&height=#{storyboard.height}" + json.field "templateUrl", storyboard.url + json.field "width", storyboard.width + json.field "height", storyboard.height + json.field "count", storyboard.count + json.field "interval", storyboard.interval + json.field "storyboardWidth", storyboard.storyboard_width + json.field "storyboardHeight", storyboard.storyboard_height + json.field "storyboardCount", storyboard.storyboard_count end end end diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 42282f44..78f91a2e 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -205,7 +205,7 @@ module Invidious::Routes::API::V1::Videos 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? haltf env, 404 @@ -215,21 +215,22 @@ module Invidious::Routes::API::V1::Videos WebVTT.build do |vtt| start_time = 0.milliseconds - end_time = storyboard[:interval].milliseconds + end_time = storyboard.interval.milliseconds - storyboard[:storyboard_count].times do |i| - url = storyboard[:url] + storyboard.storyboard_count.times do |i| + url = storyboard.url authority = /(i\d?).ytimg.com/.match!(url)[1]? + url = url.gsub("$M", i).gsub(%r(https://i\d?.ytimg.com/sb/), "") url = "#{HOST_URL}/sb/#{authority}/#{url}" - storyboard[:storyboard_height].times do |j| - storyboard[:storyboard_width].times do |k| - current_cue_url = "#{url}#xywh=#{storyboard[:width] * k},#{storyboard[:height] * j},#{storyboard[:width] - 2},#{storyboard[:height]}" + storyboard.storyboard_height.times do |j| + storyboard.storyboard_width.times do |k| + 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) - start_time += storyboard[:interval].milliseconds - end_time += storyboard[:interval].milliseconds + start_time += storyboard.interval.milliseconds + end_time += storyboard.interval.milliseconds end end end diff --git a/src/invidious/videos/storyboard.cr b/src/invidious/videos/storyboard.cr index b4302d88..797fba12 100644 --- a/src/invidious/videos/storyboard.cr +++ b/src/invidious/videos/storyboard.cr @@ -3,6 +3,21 @@ require "http/params" module Invidious::Videos 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 def self.from_yt_json(container : JSON::Any) storyboards = container.dig?("playerStoryboardSpecRenderer", "spec") @@ -10,28 +25,20 @@ module Invidious::Videos if !storyboards if storyboard = container.dig?("playerLiveStoryboardSpecRenderer", "spec").try &.as_s - return [{ - url: storyboard.split("#")[0], - width: 106, - height: 60, - count: -1, - interval: 5000, - storyboard_width: 3, + return [Storyboard.new( + url: storyboard.split("#")[0], + width: 106, + height: 60, + count: -1, + interval: 5000, + storyboard_width: 3, storyboard_height: 3, - storyboard_count: -1, - }] + storyboard_count: -1, + )] end end - items = [] of NamedTuple( - url: String, - width: Int32, - height: Int32, - count: Int32, - interval: Int32, - storyboard_width: Int32, - storyboard_height: Int32, - storyboard_count: Int32) + items = [] of Storyboard return items if !storyboards @@ -51,16 +58,16 @@ module Invidious::Videos storyboard_height = storyboard_height.to_i storyboard_count = (count / (storyboard_width * storyboard_height)).ceil.to_i - items << { - url: url.to_s.sub("$L", i).sub("$N", "M$M"), - width: width, - height: height, - count: count, - interval: interval, - storyboard_width: storyboard_width, + items << Storyboard.new( + url: url.to_s.sub("$L", i).sub("$N", "M$M"), + width: width, + height: height, + count: count, + interval: interval, + storyboard_width: storyboard_width, storyboard_height: storyboard_height, - storyboard_count: storyboard_count, - } + storyboard_count: storyboard_count + ) end items