Add preferred captions

This commit is contained in:
Omar Roth 2018-08-06 13:23:36 -05:00
parent fe5142753c
commit ca7c376874
6 changed files with 158 additions and 7 deletions

View File

@ -215,6 +215,13 @@ get "/watch" do |env|
audio_streams = video.audio_streams(adaptive_fmts) audio_streams = video.audio_streams(adaptive_fmts)
captions = video.captions captions = video.captions
if preferences
preferred_captions = captions.select { |caption| preferences.captions.includes? caption["name"]["simpleText"] }
preferred_captions.sort_by! { |caption| preferences.captions.index(caption["name"]["simpleText"]).not_nil! }
captions = captions - preferred_captions
end
preferred_captions ||= [] of JSON::Any
video.description = fill_links(video.description, "https", "www.youtube.com") video.description = fill_links(video.description, "https", "www.youtube.com")
video.description = add_alt_links(video.description) video.description = add_alt_links(video.description)
@ -713,6 +720,11 @@ post "/preferences" do |env|
comments = env.params.body["comments"]? comments = env.params.body["comments"]?
comments ||= "youtube" comments ||= "youtube"
captions_0 = env.params.body["captions_0"]?.try &.as(String) || ""
captions_1 = env.params.body["captions_1"]?.try &.as(String) || ""
captions_2 = env.params.body["captions_2"]?.try &.as(String) || ""
captions = [captions_0, captions_1, captions_2]
redirect_feed = env.params.body["redirect_feed"]?.try &.as(String) redirect_feed = env.params.body["redirect_feed"]?.try &.as(String)
redirect_feed ||= "off" redirect_feed ||= "off"
redirect_feed = redirect_feed == "on" redirect_feed = redirect_feed == "on"
@ -750,6 +762,7 @@ post "/preferences" do |env|
"quality" => quality, "quality" => quality,
"volume" => volume, "volume" => volume,
"comments" => comments, "comments" => comments,
"captions" => captions,
"redirect_feed" => redirect_feed, "redirect_feed" => redirect_feed,
"dark_mode" => dark_mode, "dark_mode" => dark_mode,
"thin_mode" => thin_mode, "thin_mode" => thin_mode,

View File

@ -33,6 +33,7 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({
"quality" => "hd720", "quality" => "hd720",
"volume" => 100, "volume" => 100,
"comments" => "youtube", "comments" => "youtube",
"captions" => ["", "", ""],
"dark_mode" => false, "dark_mode" => false,
"thin_mode " => false, "thin_mode " => false,
"max_results" => 40, "max_results" => 40,
@ -41,7 +42,6 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({
"unseen_only" => false, "unseen_only" => false,
}.to_json) }.to_json)
# TODO: Migrate preferences so fields will not be nilable
class Preferences class Preferences
JSON.mapping({ JSON.mapping({
video_loop: Bool, video_loop: Bool,
@ -51,18 +51,19 @@ class Preferences
volume: Int32, volume: Int32,
comments: { comments: {
type: String, type: String,
nilable: true,
default: "youtube", default: "youtube",
}, },
captions: {
type: Array(String),
default: ["", "", ""],
},
redirect_feed: { redirect_feed: {
type: Bool, type: Bool,
nilable: true,
default: false, default: false,
}, },
dark_mode: Bool, dark_mode: Bool,
thin_mode: { thin_mode: {
type: Bool, type: Bool,
nilable: true,
default: false, default: false,
}, },
max_results: Int32, max_results: Int32,
@ -71,7 +72,6 @@ class Preferences
unseen_only: Bool, unseen_only: Bool,
notifications_only: { notifications_only: {
type: Bool, type: Bool,
nilable: true,
default: false, default: false,
}, },
}) })

View File

@ -1,3 +1,112 @@
CAPTION_LANGUAGES = [
"",
"English",
"English (auto-generated)",
"Afrikaans",
"Albanian",
"Amharic",
"Arabic",
"Armenian",
"Azerbaijani",
"Bangla",
"Basque",
"Belarusian",
"Bosnian",
"Bulgarian",
"Burmese",
"Catalan",
"Cebuano",
"Chinese (Simplified)",
"Chinese (Traditional)",
"Corsican",
"Croatian",
"Czech",
"Danish",
"Dutch",
"Esperanto",
"Estonian",
"Filipino",
"Finnish",
"French",
"Galician",
"Georgian",
"German",
"Greek",
"Gujarati",
"Haitian Creole",
"Hausa",
"Hawaiian",
"Hebrew",
"Hindi",
"Hmong",
"Hungarian",
"Icelandic",
"Igbo",
"Indonesian",
"Irish",
"Italian",
"Japanese",
"Javanese",
"Kannada",
"Kazakh",
"Khmer",
"Korean",
"Kurdish",
"Kyrgyz",
"Lao",
"Latin",
"Latvian",
"Lithuanian",
"Luxembourgish",
"Macedonian",
"Malagasy",
"Malay",
"Malayalam",
"Maltese",
"Maori",
"Marathi",
"Mongolian",
"Nepali",
"Norwegian",
"Nyanja",
"Pashto",
"Persian",
"Polish",
"Portuguese",
"Punjabi",
"Romanian",
"Russian",
"Samoan",
"Scottish Gaelic",
"Serbian",
"Shona",
"Sindhi",
"Sinhala",
"Slovak",
"Slovenian",
"Somali",
"Southern Sotho",
"Spanish",
"Sundanese",
"Swahili",
"Swedish",
"Tajik",
"Tamil",
"Telugu",
"Thai",
"Turkish",
"Ukrainian",
"Urdu",
"Uzbek",
"Vietnamese",
"Welsh",
"Western Frisian",
"Xhosa",
"Yiddish",
"Yoruba",
"Zulu",
]
class Video class Video
module HTTPParamConverter module HTTPParamConverter
def self.from_rs(rs) def self.from_rs(rs)

View File

@ -55,7 +55,7 @@ video, #my_video, .video-js, .vjs-default-skin
<% end %> <% end %>
<% captions.each do |caption| %> <% captions.each do |caption| %>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>" <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"
srclang="<%= caption["languageCode"] %>" label="<%= caption["name"]["simpleText"]%> "> label="<%= caption["name"]["simpleText"]%> ">
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

View File

@ -56,6 +56,30 @@ function update_value(element) {
</select> </select>
</div> </div>
<div class="pure-control-group">
<label for="captions_0">Default captions: </label>
<select class="pure-u-1-5" name="captions_0" id="captions_0">
<% CAPTION_LANGUAGES.each do |option| %>
<option <% if user.preferences.captions[0] == option %> selected <% end %>><%= option %></option>
<% end %>
</select>
</div>
<div class="pure-control-group">
<label for="captions_fallback">Fallback languages: </label>
<select class="pure-u-1-5" name="captions_1" id="captions_1">
<% CAPTION_LANGUAGES.each do |option| %>
<option <% if user.preferences.captions[1] == option %> selected <% end %>><%= option %></option>
<% end %>
</select>
<select class="pure-u-1-5" name="captions_2" id="captions_2">
<% CAPTION_LANGUAGES.each do |option| %>
<option <% if user.preferences.captions[2] == option %> selected <% end %>><%= option %></option>
<% end %>
</select>
</div>
<legend>Visual preferences</legend> <legend>Visual preferences</legend>
<div class="pure-control-group"> <div class="pure-control-group">
<label for="dark_mode">Dark mode: </label> <label for="dark_mode">Dark mode: </label>

View File

@ -60,9 +60,14 @@
<% end %> <% end %>
<% end %> <% end %>
<% preferred_captions.each_with_index do |caption, i| %>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"
label="<%= caption["name"]["simpleText"]%>" <% if i == 0 %>default<% end %>>
<% end %>
<% captions.each do |caption| %> <% captions.each do |caption| %>
<track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>" <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption["name"]["simpleText"] %>"
srclang="<%= caption["languageCode"] %>" label="<%= caption["name"]["simpleText"]%> "> label="<%= caption["name"]["simpleText"]%>">
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>