Refactor structure of caption.cr

Rename CaptionsMetadata to Metadata
Nest Metadata under Captions
Unnest LANGUAGES constant from Metadata to main Captions module
This commit is contained in:
syeopite 2023-08-24 16:00:02 -07:00
parent 3509752b79
commit 1f7592e599
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
5 changed files with 90 additions and 88 deletions

View File

@ -7,7 +7,7 @@ module Invidious::Frontend::WatchPage
getter full_videos : Array(Hash(String, JSON::Any)) getter full_videos : Array(Hash(String, JSON::Any))
getter video_streams : Array(Hash(String, JSON::Any)) getter video_streams : Array(Hash(String, JSON::Any))
getter audio_streams : Array(Hash(String, JSON::Any)) getter audio_streams : Array(Hash(String, JSON::Any))
getter captions : Array(Invidious::Videos::CaptionMetadata) getter captions : Array(Invidious::Videos::Captions::Metadata)
def initialize( def initialize(
@full_videos, @full_videos,

View File

@ -24,7 +24,7 @@ struct Video
property updated : Time property updated : Time
@[DB::Field(ignore: true)] @[DB::Field(ignore: true)]
@captions = [] of Invidious::Videos::CaptionMetadata @captions = [] of Invidious::Videos::Captions::Metadata
@[DB::Field(ignore: true)] @[DB::Field(ignore: true)]
property adaptive_fmts : Array(Hash(String, JSON::Any))? property adaptive_fmts : Array(Hash(String, JSON::Any))?
@ -215,9 +215,9 @@ struct Video
keywords.includes? "YouTube Red" keywords.includes? "YouTube Red"
end end
def captions : Array(Invidious::Videos::CaptionMetadata) def captions : Array(Invidious::Videos::Captions::Metadata)
if @captions.empty? && @info.has_key?("captions") if @captions.empty? && @info.has_key?("captions")
@captions = Invidious::Videos::CaptionMetadata.from_yt_json(info["captions"]) @captions = Invidious::Videos::Captions::Metadata.from_yt_json(info["captions"])
end end
return @captions return @captions

View File

@ -1,7 +1,8 @@
require "json" require "json"
module Invidious::Videos module Invidious::Videos
struct CaptionMetadata module Captions
struct Metadata
property name : String property name : String
property language_code : String property language_code : String
property base_url : String property base_url : String
@ -12,12 +13,12 @@ module Invidious::Videos
end end
# Parse the JSON structure from Youtube # Parse the JSON structure from Youtube
def self.from_yt_json(container : JSON::Any) : Array(CaptionMetadata) def self.from_yt_json(container : JSON::Any) : Array(Captions::Metadata)
caption_tracks = container caption_tracks = container
.dig?("playerCaptionsTracklistRenderer", "captionTracks") .dig?("playerCaptionsTracklistRenderer", "captionTracks")
.try &.as_a .try &.as_a
captions_list = [] of CaptionMetadata captions_list = [] of Captions::Metadata
return captions_list if caption_tracks.nil? return captions_list if caption_tracks.nil?
caption_tracks.each do |caption| caption_tracks.each do |caption|
@ -32,7 +33,7 @@ module Invidious::Videos
auto_generated = true auto_generated = true
end end
captions_list << CaptionMetadata.new(name, language_code, base_url, auto_generated) captions_list << Captions::Metadata.new(name, language_code, base_url, auto_generated)
end end
return captions_list return captions_list
@ -103,6 +104,7 @@ module Invidious::Videos
end end
return result return result
end end
end
# List of all caption languages available on Youtube. # List of all caption languages available on Youtube.
LANGUAGES = { LANGUAGES = {

View File

@ -37,7 +37,7 @@ module Invidious::Videos
# Convert into array of TranscriptLine # Convert into array of TranscriptLine
lines = self.parse(initial_data) lines = self.parse(initial_data)
# Taken from Invidious::Videos::CaptionMetadata.timedtext_to_vtt() # Taken from Invidious::Videos::Captions::Metadata.timedtext_to_vtt()
vtt = String.build do |vtt| vtt = String.build do |vtt|
vtt << <<-END_VTT vtt << <<-END_VTT
WEBVTT WEBVTT

View File

@ -89,7 +89,7 @@
<label for="captions[0]"><%= translate(locale, "preferences_captions_label") %></label> <label for="captions[0]"><%= translate(locale, "preferences_captions_label") %></label>
<% preferences.captions.each_with_index do |caption, index| %> <% preferences.captions.each_with_index do |caption, index| %>
<select class="pure-u-1-6" name="captions[<%= index %>]" id="captions[<%= index %>]"> <select class="pure-u-1-6" name="captions[<%= index %>]" id="captions[<%= index %>]">
<% Invidious::Videos::CaptionMetadata::LANGUAGES.each do |option| %> <% Invidious::Videos::Captions::LANGUAGES.each do |option| %>
<option value="<%= option %>" <% if preferences.captions[index] == option %> selected <% end %>><%= translate(locale, option.blank? ? "none" : option) %></option> <option value="<%= option %>" <% if preferences.captions[index] == option %> selected <% end %>><%= translate(locale, option.blank? ? "none" : option) %></option>
<% end %> <% end %>
</select> </select>