From ca7c376874fd7b491dd388a40943ae8d78cae6e7 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 6 Aug 2018 13:23:36 -0500 Subject: [PATCH] Add preferred captions --- src/invidious.cr | 13 ++++ src/invidious/users.cr | 10 +-- src/invidious/videos.cr | 109 ++++++++++++++++++++++++++++ src/invidious/views/embed.ecr | 2 +- src/invidious/views/preferences.ecr | 24 ++++++ src/invidious/views/watch.ecr | 7 +- 6 files changed, 158 insertions(+), 7 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 0913a137..ce818b69 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -215,6 +215,13 @@ get "/watch" do |env| audio_streams = video.audio_streams(adaptive_fmts) 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 = add_alt_links(video.description) @@ -713,6 +720,11 @@ post "/preferences" do |env| comments = env.params.body["comments"]? 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 ||= "off" redirect_feed = redirect_feed == "on" @@ -750,6 +762,7 @@ post "/preferences" do |env| "quality" => quality, "volume" => volume, "comments" => comments, + "captions" => captions, "redirect_feed" => redirect_feed, "dark_mode" => dark_mode, "thin_mode" => thin_mode, diff --git a/src/invidious/users.cr b/src/invidious/users.cr index c27825e7..be71cd94 100644 --- a/src/invidious/users.cr +++ b/src/invidious/users.cr @@ -33,6 +33,7 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({ "quality" => "hd720", "volume" => 100, "comments" => "youtube", + "captions" => ["", "", ""], "dark_mode" => false, "thin_mode " => false, "max_results" => 40, @@ -41,7 +42,6 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({ "unseen_only" => false, }.to_json) -# TODO: Migrate preferences so fields will not be nilable class Preferences JSON.mapping({ video_loop: Bool, @@ -51,18 +51,19 @@ class Preferences volume: Int32, comments: { type: String, - nilable: true, default: "youtube", }, + captions: { + type: Array(String), + default: ["", "", ""], + }, redirect_feed: { type: Bool, - nilable: true, default: false, }, dark_mode: Bool, thin_mode: { type: Bool, - nilable: true, default: false, }, max_results: Int32, @@ -71,7 +72,6 @@ class Preferences unseen_only: Bool, notifications_only: { type: Bool, - nilable: true, default: false, }, }) diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index cb66bd22..1985ac77 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -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 module HTTPParamConverter def self.from_rs(rs) diff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecr index 222b02f6..b7bec5d5 100644 --- a/src/invidious/views/embed.ecr +++ b/src/invidious/views/embed.ecr @@ -55,7 +55,7 @@ video, #my_video, .video-js, .vjs-default-skin <% end %> <% captions.each do |caption| %> " - srclang="<%= caption["languageCode"] %>" label="<%= caption["name"]["simpleText"]%> "> + label="<%= caption["name"]["simpleText"]%> "> <% end %> <% end %> <% end %> diff --git a/src/invidious/views/preferences.ecr b/src/invidious/views/preferences.ecr index 57e40967..5fe4cf52 100644 --- a/src/invidious/views/preferences.ecr +++ b/src/invidious/views/preferences.ecr @@ -56,6 +56,30 @@ function update_value(element) { +
+ + +
+ +
+ + + + +
+ Visual preferences
diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 963f1733..0aacf91b 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -60,9 +60,14 @@ <% end %> <% end %> + <% preferred_captions.each_with_index do |caption, i| %> + " + label="<%= caption["name"]["simpleText"]%>" <% if i == 0 %>default<% end %>> + <% end %> + <% captions.each do |caption| %> " - srclang="<%= caption["languageCode"] %>" label="<%= caption["name"]["simpleText"]%> "> + label="<%= caption["name"]["simpleText"]%>"> <% end %> <% end %> <% end %>