Skip to content

Commit fa99c9a

Browse files
committed
Use '.dig?()' in playlist parsing
1 parent 2d94983 commit fa99c9a

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/invidious/playlists.cr

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ def fetch_playlist(plid : String)
331331

332332
initial_data = YoutubeAPI.browse("VL" + plid, params: "")
333333

334-
playlist_sidebar_renderer = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]?
334+
playlist_sidebar_renderer = initial_data.dig?("sidebar", "playlistSidebarRenderer", "items")
335335
raise InfoException.new("Could not extract playlistSidebarRenderer.") if !playlist_sidebar_renderer
336336

337-
playlist_info = playlist_sidebar_renderer[0]["playlistSidebarPrimaryInfoRenderer"]?
337+
playlist_info = playlist_sidebar_renderer.dig?(0, "playlistSidebarPrimaryInfoRenderer")
338338
raise InfoException.new("Could not extract playlist info") if !playlist_info
339339

340340
title = playlist_info.dig?("title", "runs", 0, "text").try &.as_s || ""
@@ -347,12 +347,15 @@ def fetch_playlist(plid : String)
347347
description_html = desc_item.try &.["runs"]?.try &.as_a
348348
.try { |run| content_to_comment_html(run).try &.to_s } || "<p></p>"
349349

350-
thumbnail = playlist_info["thumbnailRenderer"]?.try &.["playlistVideoThumbnailRenderer"]?
351-
.try &.["thumbnail"]["thumbnails"][0]["url"]?.try &.as_s
350+
thumbnail = playlist_info.dig?(
351+
"thumbnailRenderer", "playlistVideoThumbnailRenderer",
352+
"thumbnail", "thumbnails", 0, "url"
353+
).try &.as_s
352354

353355
views = 0_i64
354356
updated = Time.utc
355357
video_count = 0
358+
356359
playlist_info["stats"]?.try &.as_a.each do |stat|
357360
text = stat["runs"]?.try &.as_a.map(&.["text"].as_s).join("") || stat["simpleText"]?.try &.as_s
358361
next if !text
@@ -371,12 +374,15 @@ def fetch_playlist(plid : String)
371374
author_thumbnail = ""
372375
ucid = ""
373376
else
374-
author_info = playlist_sidebar_renderer[1]["playlistSidebarSecondaryInfoRenderer"]?.try &.["videoOwner"]["videoOwnerRenderer"]?
377+
author_info = playlist_sidebar_renderer[1].dig?(
378+
"playlistSidebarSecondaryInfoRenderer", "videoOwner", "videoOwnerRenderer"
379+
)
380+
375381
raise InfoException.new("Could not extract author info") if !author_info
376382

377-
author = author_info["title"]["runs"][0]["text"]?.try &.as_s || ""
378-
author_thumbnail = author_info["thumbnail"]["thumbnails"][0]["url"]?.try &.as_s || ""
379-
ucid = author_info["title"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"]?.try &.as_s || ""
383+
author = author_info.dig?("title", "runs", 0, "text").try &.as_s || ""
384+
author_thumbnail = author_info.dig?("thumbnail", "thumbnails", 0, "url").try &.as_s || ""
385+
ucid = author_info.dig?("title", "runs", 0, "navigationEndpoint", "browseEndpoint", "browseId").try &.as_s || ""
380386
end
381387

382388
return Playlist.new({

0 commit comments

Comments
 (0)