Add audio-only

This commit is contained in:
Omar Roth 2018-01-03 20:06:16 -06:00
parent 90fe4ab4b4
commit d1eb81b653
2 changed files with 31 additions and 5 deletions

View file

@ -107,11 +107,19 @@ context = OpenSSL::SSL::Context::Client.insecure
get "/watch" do |env| get "/watch" do |env|
video_id = env.params.query["v"] video_id = env.params.query["v"]
if env.params.query["listen"]? && env.params.query["listen"] == "true"
env.request.query_params.delete_all("listen")
listen = true
else
env.request.query_params["listen"] = "true"
listen = false
end
if pg.query_one?("select exists (select true from videos where video_id = $1)", video_id, as: Bool) if pg.query_one?("select exists (select true from videos where video_id = $1)", video_id, as: Bool)
video_record = pg.query_one("select * from videos where video_id = $1", video_id, as: Video) video_record = pg.query_one("select * from videos where video_id = $1", video_id, as: Video)
# If record was last updated more than 1 hour ago, refresh # If record was last updated more than 5 hours ago, refresh (expire param in response lasts for 6 hours)
if Time.now - video_record.last_updated > Time::Span.new(1, 0, 0, 0) if Time.now - video_record.last_updated > Time::Span.new(0, 5, 0, 0)
video_record = get_video(video_id, context) video_record = get_video(video_id, context)
pg.exec("update videos set last_updated = $1, video_info = $3, video_html = $4,\ pg.exec("update videos set last_updated = $1, video_info = $3, video_html = $4,\
views = $5, likes = $6, dislikes = $7, rating = $8, description = $9 where video_id = $2", views = $5, likes = $6, dislikes = $7, rating = $8, description = $9 where video_id = $2",
@ -140,6 +148,11 @@ get "/watch" do |env|
fmt_stream << HTTP::Params.parse(string) fmt_stream << HTTP::Params.parse(string)
end end
adaptive_fmts = [] of HTTP::Params
video_info["adaptive_fmts"].split(",") do |string|
adaptive_fmts << HTTP::Params.parse(string)
end
fmt_stream.reverse! # We want lowest quality first fmt_stream.reverse! # We want lowest quality first
related_videos = video_html.xpath_nodes(%q(//li/div/a[contains(@class,"content-link")]/@href)) related_videos = video_html.xpath_nodes(%q(//li/div/a[contains(@class,"content-link")]/@href))

View file

@ -1,10 +1,23 @@
<title><%= video_info["title"] %> - Invidious</title> <title><%= video_info["title"] %> - Invidious</title>
<video style="width: 100%" poster="<%= video_info.has_key?("iurlhq720") ? video_info["iurlhq720"] : video_info["iurlmq"] %>" controls> <video style="width: 100%" poster="<%= video_info.has_key?("iurlhq720") ? video_info["iurlhq720"] : video_info["iurlmq"] %>" controls>
<% fmt_stream.each do |fmt| %> <% if listen %>
<source src="<%= fmt["url"] %>" type="<%= fmt["type"].split(";")[0] %>"> <% adaptive_fmts.each do |fmt| %>
<% url = fmt["url"] %>
<% type = fmt["type"].to_s.split(";")[0] %>
<% if type.starts_with?("audio") %>
<source src="<%= url %>" type="<%= type %>">
<% end %>
<% end %> <% end %>
<% else %>
<% fmt_stream.each do |fmt| %>
<source src="<%= fmt["url"] %>" type="<%= fmt["type"].split(";")[0] %>">
<% end %>
<% end %>
</video> </video>
<h1><%= video_info["title"] %></h1> <h1><%= video_info["title"] %> <a href="/watch?<%= env.request.query %>">
<i class="fa <%= listen ? "fa-video-camera" : "fa-volume-up" %>" aria-hidden="true"></i>
</a>
</h1>
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1 pure-u-md-1-5"> <div class="pure-u-1 pure-u-md-1-5">
<p><i class="fa fa-eye" aria-hidden="true"></i> <%= video_record.views %></p> <p><i class="fa fa-eye" aria-hidden="true"></i> <%= video_record.views %></p>