Skip to content

Commit

Permalink
Extract label for transcript in YouTube response
Browse files Browse the repository at this point in the history
  • Loading branch information
syeopite committed Jun 13, 2024
1 parent 5b51912 commit f466116
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/invidious/videos/transcript.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ module Invidious::Videos
alias TranscriptLine = HeadingLine | RegularLine

property lines : Array(TranscriptLine)

property language_code : String
property auto_generated : Bool

# User friendly label for the current transcript.
# Example: "English (auto-generated)"
property label : String

# Initializes a new Transcript struct with the contents and associated metadata describing it
def initialize(@lines : Array(TranscriptLine), @language_code : String, @auto_generated : Bool)
def initialize(@lines : Array(TranscriptLine), @language_code : String, @auto_generated : Bool, @label : String)
end

# Generates a protobuf string to fetch the requested transcript from YouTube
Expand Down Expand Up @@ -45,14 +50,29 @@ module Invidious::Videos

# Constructs a Transcripts struct from the initial YouTube response
def self.from_raw(initial_data : Hash(String, JSON::Any), language_code : String, auto_generated : Bool)
segment_list = initial_data.dig("actions", 0, "updateEngagementPanelAction", "content", "transcriptRenderer",
"content", "transcriptSearchPanelRenderer", "body", "transcriptSegmentListRenderer"
)
transcript_panel = initial_data.dig("actions", 0, "updateEngagementPanelAction", "content", "transcriptRenderer",
"content", "transcriptSearchPanelRenderer")

segment_list = transcript_panel.dig("body", "transcriptSegmentListRenderer")

if !segment_list["initialSegments"]?
raise NotFoundException.new("Requested transcript does not exist")
end

# Extract user-friendly label for the current transcript

footer_language_menu = transcript_panel.dig?(
"footer", "transcriptFooterRenderer", "languageMenu", "sortFilterSubMenuRenderer", "subMenuItems"
)

if footer_language_menu
label = footer_language_menu.as_a.select(&.["selected"].as_bool)[0]["title"].as_s
else
label = language_code
end

# Extract transcript lines

initial_segments = segment_list["initialSegments"].as_a

lines = [] of TranscriptLine
Expand All @@ -76,6 +96,7 @@ module Invidious::Videos
lines: lines,
language_code: language_code,
auto_generated: auto_generated,
label: label
)
end

Expand Down

0 comments on commit f466116

Please # to comment.