From 371dbd73fe8fa234c31f8e3827778a6606532bee Mon Sep 17 00:00:00 2001 From: syeopite Date: Sat, 19 Aug 2023 19:31:11 -0700 Subject: [PATCH] Add logic to parse video chapters --- src/invidious/videos.cr | 2 +- src/invidious/videos/parser.cr | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index c218b4ef..540ce6b2 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -15,7 +15,7 @@ struct Video # NOTE: don't forget to bump this number if any change is made to # the `params` structure in videos/parser.cr!!! # - SCHEMA_VERSION = 2 + SCHEMA_VERSION = 3 property id : String diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 0e1a947c..c7c9eeb7 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -243,11 +243,12 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any end end + player_overlays = player_response.dig?("playerOverlays", "playerOverlayRenderer") + # If nothing was found previously, fall back to end screen renderer if related.empty? # Container for "endScreenVideoRenderer" items - player_overlays = player_response.dig?( - "playerOverlays", "playerOverlayRenderer", + end_screen_watch_next_array = player_overlays.try &.dig?( "endScreen", "watchNextEndScreenRenderer", "results" ) @@ -389,6 +390,20 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any .try &.as_s.split(" ", 2)[0] end + # Chapters + chapters_array = [] of JSON::Any + + # Yes, `decoratedPlayerBarRenderer` is repeated twice. + if player_bar = player_overlays.try &.dig?("decoratedPlayerBarRenderer", "decoratedPlayerBarRenderer", "playerBar") + if markers = player_bar.dig?("multiMarkersPlayerBarRenderer", "markersMap") + potential_chapters_array = markers.as_a.find { |m| m["key"] == "DESCRIPTION_CHAPTERS" } + + if potential_chapters_array + chapters_array = potential_chapters_array.as_a + end + end + end + # Return data if live_now @@ -434,6 +449,8 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any "authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""), "authorVerified" => JSON::Any.new(author_verified || false), "subCountText" => JSON::Any.new(subs_text || "-"), + + "chapters" => JSON::Any.new(chapters_array), } return params