Add support for preferences as query params

This commit is contained in:
Omar Roth 2018-08-25 20:05:51 -05:00
parent 23aaf7f1b7
commit 7fd0f93d02
4 changed files with 75 additions and 50 deletions

View file

@ -215,8 +215,9 @@ get "/watch" do |env|
end end
subscriptions ||= [] of String subscriptions ||= [] of String
autoplay, video_loop, video_start, video_end, listen, raw, quality, controls = process_video_params(env.params.query, preferences) params = process_video_params(env.params.query, preferences)
if listen
if params[:listen]
env.params.query.delete_all("listen") env.params.query.delete_all("listen")
end end
@ -234,13 +235,17 @@ 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 preferred_captions = captions.select { |caption|
end params[:preferred_captions].includes?(caption.name.simpleText) ||
preferred_captions ||= [] of Caption 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" aspect_ratio = "16:9"
video.description = fill_links(video.description, "https", "www.youtube.com") video.description = fill_links(video.description, "https", "www.youtube.com")
@ -259,11 +264,11 @@ get "/watch" do |env|
# TODO: Find highest resolution thumbnail automatically # TODO: Find highest resolution thumbnail automatically
thumbnail = "https://i.ytimg.com/vi/#{video.id}/mqdefault.jpg" thumbnail = "https://i.ytimg.com/vi/#{video.id}/mqdefault.jpg"
if raw if params[:raw]
url = fmt_stream[0]["url"] url = fmt_stream[0]["url"]
fmt_stream.each do |fmt| fmt_stream.each do |fmt|
if fmt["label"].split(" - ")[0] == quality if fmt["label"].split(" - ")[0] == params[:quality]
url = fmt["url"] url = fmt["url"]
end end
end end
@ -313,21 +318,7 @@ get "/embed/:id" do |env|
next env.redirect url next env.redirect url
end end
autoplay, video_loop, video_start, video_end, listen, raw, quality, controls = process_video_params(env.params.query, nil) params = 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
begin begin
video = get_video(id, PG_DB) video = get_video(id, PG_DB)
@ -343,6 +334,18 @@ get "/embed/:id" do |env|
captions = video.captions 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 = fill_links(video.description, "https", "www.youtube.com")
video.description = add_alt_links(video.description) video.description = add_alt_links(video.description)
description = video.short_description description = video.short_description
@ -359,11 +362,11 @@ get "/embed/:id" do |env|
# TODO: Find highest resolution thumbnail automatically # TODO: Find highest resolution thumbnail automatically
thumbnail = "https://i.ytimg.com/vi/#{video.id}/mqdefault.jpg" thumbnail = "https://i.ytimg.com/vi/#{video.id}/mqdefault.jpg"
if raw if params[:raw]
url = fmt_stream[0]["url"] url = fmt_stream[0]["url"]
fmt_stream.each do |fmt| fmt_stream.each do |fmt|
if fmt["label"].split(" - ")[0] == quality if fmt["label"].split(" - ")[0] == params[:quality]
url = fmt["url"] url = fmt["url"]
end end
end end

View file

@ -504,16 +504,29 @@ end
def process_video_params(query, preferences) def process_video_params(query, preferences)
autoplay = query["autoplay"]?.try &.to_i? 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? video_loop = query["loop"]?.try &.to_i?
volume = query["volume"]?.try &.to_i?
if preferences if preferences
autoplay ||= preferences.autoplay.to_unsafe autoplay ||= preferences.autoplay.to_unsafe
preferred_captions ||= preferences.captions
quality ||= preferences.quality
speed ||= preferences.speed
video_loop ||= preferences.video_loop.to_unsafe video_loop ||= preferences.video_loop.to_unsafe
volume ||= preferences.volume
end end
autoplay ||= 0
autoplay = autoplay == 1
autoplay ||= 0
preferred_captions ||= [] of String
quality ||= "hd720"
speed ||= 1
video_loop ||= 0 video_loop ||= 0
volume ||= 100
autoplay = autoplay == 1
video_loop = video_loop == 1 video_loop = video_loop == 1
if query["t"]? if query["t"]?
@ -542,14 +555,25 @@ def process_video_params(query, preferences)
raw ||= 0 raw ||= 0
raw = raw == 1 raw = raw == 1
quality = query["quality"]?
quality ||= "hd720"
controls = query["controls"]?.try &.to_i? controls = query["controls"]?.try &.to_i?
controls ||= 1 controls ||= 1
controls = 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 end
def generate_thumbnails(json, id) def generate_thumbnails(json, id)

View file

@ -1,19 +1,19 @@
<video style="width:100%" playsinline poster="<%= thumbnail %>" title="<%= HTML.escape(video.title) %>" <video style="width:100%" playsinline poster="<%= thumbnail %>" title="<%= HTML.escape(video.title) %>"
id="player" class="video-js" id="player" class="video-js"
<% if autoplay %>autoplay<% end %> <% if params[:autoplay] %>autoplay<% end %>
<% if video_loop %>loop<% end %> <% if params[:video_loop] %>loop<% end %>
<% if controls %>controls<% end %>> <% if params[:controls] %>controls<% end %>>
<% if hlsvp %> <% if hlsvp %>
<source src="<%= hlsvp %>" type="application/x-mpegURL"> <source src="<%= hlsvp %>" type="application/x-mpegURL">
<% else %> <% else %>
<% if listen %> <% if params[:listen] %>
<% audio_streams.each_with_index do |fmt, i| %> <% audio_streams.each_with_index do |fmt, i| %>
<source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>"> <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>">
<% end %> <% end %>
<% else %> <% else %>
<% fmt_stream.each_with_index do |fmt, i| %> <% fmt_stream.each_with_index do |fmt, i| %>
<% if preferences %> <% if params[:quality] %>
<source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= preferences.quality == fmt["label"].split(" - ")[0] %>"> <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= params[:quality] == fmt["label"].split(" - ")[0] %>">
<% else %> <% else %>
<source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= i == 0 ? true : false %>"> <source src="<%= fmt["url"] %>" type='<%= fmt["type"] %>' label="<%= fmt["label"] %>" selected="<%= i == 0 ? true : false %>">
<% end %> <% end %>
@ -110,7 +110,7 @@ var player = videojs("player", options, function() {
player.share(shareOptions); player.share(shareOptions);
<% if video_start > 0 || video_end > 0 %> <% if params[:video_start] > 0 || params[:video_end] > 0 %>
player.markers({ player.markers({
onMarkerReached: function(marker) { onMarkerReached: function(marker) {
if (marker.text === "End") { if (marker.text === "End") {
@ -122,19 +122,19 @@ player.markers({
} }
}, },
markers: [ markers: [
{ time: <%= video_start %>, text: "Start" }, { time: <%= params[:video_start] %>, text: "Start" },
<% if video_end < 0 %> <% if params[:video_end] < 0 %>
{ time: <%= video.info["length_seconds"].to_f - 0.5 %>, text: "End" } { time: <%= video.info["length_seconds"].to_f - 0.5 %>, text: "End" }
<% else %> <% else %>
{ time: <%= video_end %>, text: "End" } { time: <%= params[:video_end] %>, text: "End" }
<% end %> <% end %>
] ]
}); });
player.currentTime(<%= video_start %>); player.currentTime(<%= params[:video_start] %>);
<% end %> <% end %>
<% if !listen %> <% if !params[:listen] %>
var currentSources = player.currentSources(); var currentSources = player.currentSources();
for (var i = 0; i < currentSources.length; i++) { for (var i = 0; i < currentSources.length; i++) {
if (player.canPlayType(currentSources[i]["type"].split(";")[0]) === "") { if (player.canPlayType(currentSources[i]["type"].split(";")[0]) === "") {
@ -146,8 +146,6 @@ for (var i = 0; i < currentSources.length; i++) {
player.src(currentSources); player.src(currentSources);
<% end %> <% end %>
<% if preferences %> player.volume(<%= params[:volume].to_f / 100 %>);
player.volume(<%= preferences.volume.to_f / 100 %>); player.playbackRate(<%= params[:speed] %>);
player.playbackRate(<%= preferences.speed %>);
<% end %>
</script> </script>

View file

@ -33,7 +33,7 @@
<div class="h-box"> <div class="h-box">
<h1> <h1>
<%= HTML.escape(video.title) %> <%= HTML.escape(video.title) %>
<% if listen %> <% if params[:listen] %>
<a href="/watch?<%= env.params.query %>"> <a href="/watch?<%= env.params.query %>">
<i class="icon ion-ios-videocam"></i> <i class="icon ion-ios-videocam"></i>
</a> </a>