Add logic to parse video chapters

This commit is contained in:
syeopite 2023-08-19 19:31:11 -07:00
parent eda7444ca4
commit 371dbd73fe
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
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

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