videos.cr: use '.dig?()' where possible

This commit is contained in:
Samantaz Fox 2022-01-20 22:22:48 +01:00
parent 84cc732281
commit 63e1165936
No known key found for this signature in database
GPG key ID: F42821059186176E

View file

@ -497,7 +497,7 @@ struct Video
end end
def length_seconds : Int32 def length_seconds : Int32
info["microformat"]?.try &.["playerMicroformatRenderer"]?.try &.["lengthSeconds"]?.try &.as_s.to_i || info.dig?("microformat", "playerMicroformatRenderer", "lengthSeconds").try &.as_s.to_i ||
info["videoDetails"]["lengthSeconds"]?.try &.as_s.to_i || 0 info["videoDetails"]["lengthSeconds"]?.try &.as_s.to_i || 0
end end
@ -519,7 +519,9 @@ struct Video
end end
def published : Time def published : Time
info["microformat"]?.try &.["playerMicroformatRenderer"]?.try &.["publishDate"]?.try { |t| Time.parse(t.as_s, "%Y-%m-%d", Time::Location::UTC) } || Time.utc info
.dig?("microformat", "playerMicroformatRenderer", "publishDate")
.try { |t| Time.parse(t.as_s, "%Y-%m-%d", Time::Location::UTC) } || Time.utc
end end
def published=(other : Time) def published=(other : Time)
@ -545,8 +547,9 @@ struct Video
end end
def premiere_timestamp : Time? def premiere_timestamp : Time?
info["microformat"]?.try &.["playerMicroformatRenderer"]? info
.try &.["liveBroadcastDetails"]?.try &.["startTimestamp"]?.try { |t| Time.parse_rfc3339(t.as_s) } .dig?("microformat", "playerMicroformatRenderer", "liveBroadcastDetails", "startTimestamp")
.try { |t| Time.parse_rfc3339(t.as_s) }
end end
def keywords def keywords
@ -558,8 +561,9 @@ struct Video
end end
def allowed_regions def allowed_regions
info["microformat"]?.try &.["playerMicroformatRenderer"]? info
.try &.["availableCountries"]?.try &.as_a.map &.as_s || [] of String .dig("microformat", "playerMicroformatRenderer", "availableCountries")
.try &.as_a.map &.as_s || [] of String
end end
def author_thumbnail : String def author_thumbnail : String
@ -621,18 +625,11 @@ struct Video
end end
def storyboards def storyboards
storyboards = info["storyboards"]? storyboards = info.dig?("storyboards", "playerStoryboardSpecRenderer", "spec")
.try &.as_h .try &.as_s.split("|")
.try &.["playerStoryboardSpecRenderer"]?
.try &.["spec"]?
.try &.as_s.split("|")
if !storyboards if !storyboards
if storyboard = info["storyboards"]? if storyboard = info.dig?("storyboards", "playerLiveStoryboardSpecRenderer", "spec").try &.as_s
.try &.as_h
.try &.["playerLiveStoryboardSpecRenderer"]?
.try &.["spec"]?
.try &.as_s
return [{ return [{
url: storyboard.split("#")[0], url: storyboard.split("#")[0],
width: 106, width: 106,
@ -690,9 +687,8 @@ struct Video
end end
def paid def paid
reason = info["playabilityStatus"]?.try &.["reason"]? reason = info.dig?("playabilityStatus", "reason") || ""
paid = reason == "This video requires payment to watch." ? true : false return reason.includes? "requires payment"
paid
end end
def premium def premium
@ -716,8 +712,9 @@ struct Video
end end
def description def description
description = info["microformat"]?.try &.["playerMicroformatRenderer"]? description = info!
.try &.["description"]?.try &.["simpleText"]?.try &.as_s || "" .dig?("microformat", "playerMicroformatRenderer", "description", "simpleText")
.try &.as_s || ""
end end
# TODO # TODO
@ -738,11 +735,11 @@ struct Video
end end
def hls_manifest_url : String? def hls_manifest_url : String?
info["streamingData"]?.try &.["hlsManifestUrl"]?.try &.as_s info.dig?("streamingData", "hlsManifestUrl").try &.as_s
end end
def dash_manifest_url def dash_manifest_url
info["streamingData"]?.try &.["dashManifestUrl"]?.try &.as_s info.dig?("streamingData", "dashManifestUrl").try &.as_s
end end
def genre : String def genre : String
@ -758,7 +755,7 @@ struct Video
end end
def is_family_friendly : Bool def is_family_friendly : Bool
info["microformat"]?.try &.["playerMicroformatRenderer"]["isFamilySafe"]?.try &.as_bool || false info.dig?("microformat", "playerMicroformatRenderer", "isFamilySafe").try &.as_bool || false
end end
def is_vr : Bool? def is_vr : Bool?