From a569b8f3d9eae13dcab1e4a8bad49910ae07ca1f Mon Sep 17 00:00:00 2001 From: syeopite Date: Mon, 21 Aug 2023 22:14:40 -0700 Subject: [PATCH] Add message for when chapters are auto generated --- src/invidious/videos.cr | 4 ++++ src/invidious/videos/parser.cr | 17 +++++++++++++++-- .../components/description_chapters_widget.ecr | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 1224ffff..cf401796 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -238,6 +238,10 @@ struct Video return @chapters end + def automatically_generated_chapters? : Bool? + return @info["AutoGeneratedChapters"]?.try &.as_bool + end + def hls_manifest_url : String? info.dig?("streamingData", "hlsManifestUrl").try &.as_s end diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 709ca644..1f92e5be 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -392,13 +392,25 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any # Chapters chapters_array = [] of JSON::Any + chapters_auto_generated = nil - # Yes, `decoratedPlayerBarRenderer` is repeated twice. + # 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" } + # Let manual chapters have higher precedence than automatically generated ones. + if !potential_chapters_array + potential_chapters_array = markers.as_a.find { |m| m["key"]? == "AUTO_CHAPTERS" } + end + if potential_chapters_array + if potential_chapters_array["key"] == "AUTO_CHAPTERS" + chapters_auto_generated = true + else + chapters_auto_generated = false + end + chapters_array = potential_chapters_array["value"]["chapters"].as_a end end @@ -450,7 +462,8 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any "authorVerified" => JSON::Any.new(author_verified || false), "subCountText" => JSON::Any.new(subs_text || "-"), - "chapters" => JSON::Any.new(chapters_array), + "AutoGeneratedChapters" => JSON::Any.new(chapters_auto_generated), + "chapters" => JSON::Any.new(chapters_array), } return params diff --git a/src/invidious/views/components/description_chapters_widget.ecr b/src/invidious/views/components/description_chapters_widget.ecr index e02f29cf..852798d1 100644 --- a/src/invidious/views/components/description_chapters_widget.ecr +++ b/src/invidious/views/components/description_chapters_widget.ecr @@ -2,6 +2,11 @@

Chapters

+ + <% if video.automatically_generated_chapters? %> +
Chapters are automatically generated
+ <% end %> +
<% chapters.each do | chapter | %> <%- start_in_seconds = chapter.start_ms.milliseconds.total_seconds.to_i %>