Merge pull request #2892 from matthewmcgarvey/video-playability

Raise error if video not playable, also handle missing related videos
This commit is contained in:
Samantaz Fox 2022-02-14 00:59:07 +01:00 committed by GitHub
commit 8af202e86b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -881,11 +881,13 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config) player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config)
if player_response["playabilityStatus"]?.try &.["status"]?.try &.as_s != "OK" if player_response.dig?("playabilityStatus", "status").try &.as_s != "OK"
reason = player_response["playabilityStatus"]["errorScreen"]?.try &.["playerErrorMessageRenderer"]?.try &.["subreason"]?.try { |s| subreason = player_response.dig?("playabilityStatus", "errorScreen", "playerErrorMessageRenderer", "subreason")
s["simpleText"]?.try &.as_s || s["runs"].as_a.map { |r| r["text"] }.join("") reason = subreason.try &.[]?("simpleText").try &.as_s
} || player_response["playabilityStatus"]["reason"].as_s reason ||= subreason.try &.[]("runs").as_a.map(&.[]("text")).join("")
reason ||= player_response.dig("playabilityStatus", "reason").as_s
params["reason"] = JSON::Any.new(reason) params["reason"] = JSON::Any.new(reason)
return params
end end
params["shortDescription"] = player_response.dig?("videoDetails", "shortDescription") || JSON::Any.new(nil) params["shortDescription"] = player_response.dig?("videoDetails", "shortDescription") || JSON::Any.new(nil)
@ -928,11 +930,8 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
raise BrokenTubeException.new("twoColumnWatchNextResults") if !main_results raise BrokenTubeException.new("twoColumnWatchNextResults") if !main_results
primary_results = main_results.dig?("results", "results", "contents") primary_results = main_results.dig?("results", "results", "contents")
secondary_results = main_results
.dig?("secondaryResults", "secondaryResults", "results")
raise BrokenTubeException.new("results") if !primary_results raise BrokenTubeException.new("results") if !primary_results
raise BrokenTubeException.new("secondaryResults") if !secondary_results
video_primary_renderer = primary_results video_primary_renderer = primary_results
.as_a.find(&.["videoPrimaryInfoRenderer"]?) .as_a.find(&.["videoPrimaryInfoRenderer"]?)
@ -952,7 +951,9 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
related = [] of JSON::Any related = [] of JSON::Any
# Parse "compactVideoRenderer" items (under secondary results) # Parse "compactVideoRenderer" items (under secondary results)
secondary_results.as_a.each do |element| secondary_results = main_results
.dig?("secondaryResults", "secondaryResults", "results")
secondary_results.try &.as_a.each do |element|
if item = element["compactVideoRenderer"]? if item = element["compactVideoRenderer"]?
related_video = parse_related_video(item) related_video = parse_related_video(item)
related << JSON::Any.new(related_video) if related_video related << JSON::Any.new(related_video) if related_video
@ -1119,7 +1120,9 @@ def fetch_video(id, region)
info = embed_info if !embed_info["reason"]? info = embed_info if !embed_info["reason"]?
end end
raise InfoException.new(info["reason"]?.try &.as_s || "") if !info["videoDetails"]? if reason = info["reason"]?
raise InfoException.new(reason.as_s || "")
end
video = Video.new({ video = Video.new({
id: id, id: id,