add null safety to clip parsing

This commit is contained in:
ChunkyProgrammer 2023-12-19 22:37:48 -05:00
parent b344d98c25
commit 7da4a7f72b
2 changed files with 6 additions and 4 deletions

View File

@ -277,8 +277,8 @@ 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)
env.params.query["start"] = start_time.to_s
env.params.query["end"] = end_time.to_s
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
return env.redirect "/watch?v=#{video_id}&#{env.params.query}"

View File

@ -1,7 +1,7 @@
require "json"
# returns start_time, end_time and clip_title
def parse_clip_parameters(params) : {Float64, Float64, String}
def parse_clip_parameters(params) : {Float64?, Float64?, String?}
decoded_protobuf = params.try { |i| URI.decode_www_form(i) }
.try { |i| Base64.decode(i) }
.try { |i| IO::Memory.new(i) }
@ -9,12 +9,14 @@ def parse_clip_parameters(params) : {Float64, Float64, String}
start_time = decoded_protobuf
.try(&.["50:0:embedded"]["2:1:varint"].as_i64)
.try { |i| i/1000 }
end_time = decoded_protobuf
.try(&.["50:0:embedded"]["3:2:varint"].as_i64)
.try { |i| i/1000 }
clip_title = decoded_protobuf
.try(&.["50:0:embedded"]["4:3:string"].as_s)
return (start_time / 1000), (end_time / 1000), clip_title
return start_time, end_time, clip_title
end