From 7fd0f93d029b8bd69409256c39bb0ead7929aa3a Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 25 Aug 2018 20:05:51 -0500 Subject: [PATCH] Add support for preferences as query params --- src/invidious.cr | 57 ++++++++++++----------- src/invidious/videos.cr | 36 +++++++++++--- src/invidious/views/components/player.ecr | 30 ++++++------ src/invidious/views/watch.ecr | 2 +- 4 files changed, 75 insertions(+), 50 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 68514a44..d2b37705 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -215,8 +215,9 @@ get "/watch" do |env| end subscriptions ||= [] of String - autoplay, video_loop, video_start, video_end, listen, raw, quality, controls = process_video_params(env.params.query, preferences) - if listen + params = process_video_params(env.params.query, preferences) + + if params[:listen] env.params.query.delete_all("listen") end @@ -234,13 +235,17 @@ 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 Caption + preferred_captions = captions.select { |caption| + params[:preferred_captions].includes?(caption.name.simpleText) || + params[:preferred_captions].includes?(caption.languageCode.split("-")[0]) + } + preferred_captions.sort_by! { |caption| + (params[:preferred_captions].index(caption.languageCode) || + params[:preferred_captions].index(caption.languageCode.split("-")[0])).not_nil! + } + captions = captions - preferred_captions + aspect_ratio = "16:9" video.description = fill_links(video.description, "https", "www.youtube.com") @@ -259,11 +264,11 @@ get "/watch" do |env| # TODO: Find highest resolution thumbnail automatically thumbnail = "https://i.ytimg.com/vi/#{video.id}/mqdefault.jpg" - if raw + if params[:raw] url = fmt_stream[0]["url"] fmt_stream.each do |fmt| - if fmt["label"].split(" - ")[0] == quality + if fmt["label"].split(" - ")[0] == params[:quality] url = fmt["url"] end end @@ -313,21 +318,7 @@ get "/embed/:id" do |env| next env.redirect url end - autoplay, video_loop, video_start, video_end, listen, raw, quality, controls = process_video_params(env.params.query, nil) - preferred_captions = [] of Caption - preferences = Preferences.from_json({ - "video_loop" => video_loop, - "autoplay" => autoplay, - "speed" => 1.0, - "quality" => quality, - "volume" => 100, - "max_results" => 0, - "sort" => "", - "latest_only" => false, - "unseen_only" => false, - "dark_mode" => false, - }.to_json) - aspect_ratio = nil + params = process_video_params(env.params.query, nil) begin video = get_video(id, PG_DB) @@ -343,6 +334,18 @@ get "/embed/:id" do |env| captions = video.captions + preferred_captions = captions.select { |caption| + params[:preferred_captions].includes?(caption.name.simpleText) || + params[:preferred_captions].includes?(caption.languageCode.split("-")[0]) + } + preferred_captions.sort_by! { |caption| + (params[:preferred_captions].index(caption.languageCode) || + params[:preferred_captions].index(caption.languageCode.split("-")[0])).not_nil! + } + captions = captions - preferred_captions + + aspect_ratio = nil + video.description = fill_links(video.description, "https", "www.youtube.com") video.description = add_alt_links(video.description) description = video.short_description @@ -359,11 +362,11 @@ get "/embed/:id" do |env| # TODO: Find highest resolution thumbnail automatically thumbnail = "https://i.ytimg.com/vi/#{video.id}/mqdefault.jpg" - if raw + if params[:raw] url = fmt_stream[0]["url"] fmt_stream.each do |fmt| - if fmt["label"].split(" - ")[0] == quality + if fmt["label"].split(" - ")[0] == params[:quality] url = fmt["url"] end end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index ee9be2c1..c9e303d3 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -504,16 +504,29 @@ end def process_video_params(query, preferences) autoplay = query["autoplay"]?.try &.to_i? + preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase } + quality = query["quality"]? + speed = query["speed"]?.try &.to_f? video_loop = query["loop"]?.try &.to_i? + volume = query["volume"]?.try &.to_i? if preferences autoplay ||= preferences.autoplay.to_unsafe + preferred_captions ||= preferences.captions + quality ||= preferences.quality + speed ||= preferences.speed video_loop ||= preferences.video_loop.to_unsafe + volume ||= preferences.volume end - autoplay ||= 0 - autoplay = autoplay == 1 + autoplay ||= 0 + preferred_captions ||= [] of String + quality ||= "hd720" + speed ||= 1 video_loop ||= 0 + volume ||= 100 + + autoplay = autoplay == 1 video_loop = video_loop == 1 if query["t"]? @@ -542,14 +555,25 @@ def process_video_params(query, preferences) raw ||= 0 raw = raw == 1 - quality = query["quality"]? - quality ||= "hd720" - controls = query["controls"]?.try &.to_i? controls ||= 1 controls = controls == 1 - return autoplay, video_loop, video_start, video_end, listen, raw, quality, controls + params = { + autoplay: autoplay, + controls: controls, + listen: listen, + preferred_captions: preferred_captions, + quality: quality, + raw: raw, + speed: speed, + video_end: video_end, + video_loop: video_loop, + video_start: video_start, + volume: volume, + } + + return params end def generate_thumbnails(json, id) diff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecr index 30d9f724..38a4bbe1 100644 --- a/src/invidious/views/components/player.ecr +++ b/src/invidious/views/components/player.ecr @@ -1,19 +1,19 @@