Skip to content

Commit 63e1165

Browse files
committed
videos.cr: use '.dig?()' where possible
1 parent 84cc732 commit 63e1165

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/invidious/videos.cr

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ struct Video
497497
end
498498

499499
def length_seconds : Int32
500-
info["microformat"]?.try &.["playerMicroformatRenderer"]?.try &.["lengthSeconds"]?.try &.as_s.to_i ||
500+
info.dig?("microformat", "playerMicroformatRenderer", "lengthSeconds").try &.as_s.to_i ||
501501
info["videoDetails"]["lengthSeconds"]?.try &.as_s.to_i || 0
502502
end
503503

@@ -519,7 +519,9 @@ struct Video
519519
end
520520

521521
def published : Time
522-
info["microformat"]?.try &.["playerMicroformatRenderer"]?.try &.["publishDate"]?.try { |t| Time.parse(t.as_s, "%Y-%m-%d", Time::Location::UTC) } || Time.utc
522+
info
523+
.dig?("microformat", "playerMicroformatRenderer", "publishDate")
524+
.try { |t| Time.parse(t.as_s, "%Y-%m-%d", Time::Location::UTC) } || Time.utc
523525
end
524526

525527
def published=(other : Time)
@@ -545,8 +547,9 @@ struct Video
545547
end
546548

547549
def premiere_timestamp : Time?
548-
info["microformat"]?.try &.["playerMicroformatRenderer"]?
549-
.try &.["liveBroadcastDetails"]?.try &.["startTimestamp"]?.try { |t| Time.parse_rfc3339(t.as_s) }
550+
info
551+
.dig?("microformat", "playerMicroformatRenderer", "liveBroadcastDetails", "startTimestamp")
552+
.try { |t| Time.parse_rfc3339(t.as_s) }
550553
end
551554

552555
def keywords
@@ -558,8 +561,9 @@ struct Video
558561
end
559562

560563
def allowed_regions
561-
info["microformat"]?.try &.["playerMicroformatRenderer"]?
562-
.try &.["availableCountries"]?.try &.as_a.map &.as_s || [] of String
564+
info
565+
.dig("microformat", "playerMicroformatRenderer", "availableCountries")
566+
.try &.as_a.map &.as_s || [] of String
563567
end
564568

565569
def author_thumbnail : String
@@ -621,18 +625,11 @@ struct Video
621625
end
622626

623627
def storyboards
624-
storyboards = info["storyboards"]?
625-
.try &.as_h
626-
.try &.["playerStoryboardSpecRenderer"]?
627-
.try &.["spec"]?
628-
.try &.as_s.split("|")
628+
storyboards = info.dig?("storyboards", "playerStoryboardSpecRenderer", "spec")
629+
.try &.as_s.split("|")
629630

630631
if !storyboards
631-
if storyboard = info["storyboards"]?
632-
.try &.as_h
633-
.try &.["playerLiveStoryboardSpecRenderer"]?
634-
.try &.["spec"]?
635-
.try &.as_s
632+
if storyboard = info.dig?("storyboards", "playerLiveStoryboardSpecRenderer", "spec").try &.as_s
636633
return [{
637634
url: storyboard.split("#")[0],
638635
width: 106,
@@ -690,9 +687,8 @@ struct Video
690687
end
691688

692689
def paid
693-
reason = info["playabilityStatus"]?.try &.["reason"]?
694-
paid = reason == "This video requires payment to watch." ? true : false
695-
paid
690+
reason = info.dig?("playabilityStatus", "reason") || ""
691+
return reason.includes? "requires payment"
696692
end
697693

698694
def premium
@@ -716,8 +712,9 @@ struct Video
716712
end
717713

718714
def description
719-
description = info["microformat"]?.try &.["playerMicroformatRenderer"]?
720-
.try &.["description"]?.try &.["simpleText"]?.try &.as_s || ""
715+
description = info!
716+
.dig?("microformat", "playerMicroformatRenderer", "description", "simpleText")
717+
.try &.as_s || ""
721718
end
722719

723720
# TODO
@@ -738,11 +735,11 @@ struct Video
738735
end
739736

740737
def hls_manifest_url : String?
741-
info["streamingData"]?.try &.["hlsManifestUrl"]?.try &.as_s
738+
info.dig?("streamingData", "hlsManifestUrl").try &.as_s
742739
end
743740

744741
def dash_manifest_url
745-
info["streamingData"]?.try &.["dashManifestUrl"]?.try &.as_s
742+
info.dig?("streamingData", "dashManifestUrl").try &.as_s
746743
end
747744

748745
def genre : String
@@ -758,7 +755,7 @@ struct Video
758755
end
759756

760757
def is_family_friendly : Bool
761-
info["microformat"]?.try &.["playerMicroformatRenderer"]["isFamilySafe"]?.try &.as_bool || false
758+
info.dig?("microformat", "playerMicroformatRenderer", "isFamilySafe").try &.as_bool || false
762759
end
763760

764761
def is_vr : Bool?

0 commit comments

Comments
 (0)