Add logic to parse video chapters

This commit is contained in:
syeopite 2023-08-19 19:31:11 -07:00 committed by tezlm
parent 4782a67038
commit 0bd89cead0
Signed by: tezlm
GPG key ID: 649733FCD94AFBBA
2 changed files with 20 additions and 3 deletions

View file

@ -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

View file

@ -248,11 +248,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"
)
@ -394,6 +395,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
@ -439,6 +454,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