diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/invidious.cr | 28 | ||||
| -rw-r--r-- | src/invidious/helpers/helpers.cr | 22 | ||||
| -rw-r--r-- | src/invidious/helpers/logger.cr | 35 | ||||
| -rw-r--r-- | src/invidious/jobs.cr | 16 | ||||
| -rw-r--r-- | src/invidious/views/components/player_sources.ecr | 2 | ||||
| -rw-r--r-- | src/invidious/views/watch.ecr | 61 |
6 files changed, 133 insertions, 31 deletions
diff --git a/src/invidious.cr b/src/invidious.cr index 4bde39ef..1a96b50a 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -16,6 +16,7 @@ require "detect_language" require "digest/md5" +require "file_utils" require "kemal" require "openssl/hmac" require "option_parser" @@ -35,6 +36,8 @@ channel_threads = CONFIG.channel_threads feed_threads = CONFIG.feed_threads video_threads = CONFIG.video_threads +logger = Invidious::LogHandler.new + Kemal.config.extra_options do |parser| parser.banner = "Usage: invidious [arguments]" parser.on("-t THREADS", "--crawl-threads=THREADS", "Number of threads for crawling YouTube (default: #{crawl_threads})") do |number| @@ -69,6 +72,10 @@ Kemal.config.extra_options do |parser| exit end end + parser.on("-o OUTPUT", "--output=OUTPUT", "Redirect output (default: STDOUT)") do |output| + FileUtils.mkdir_p(File.dirname(output)) + logger = Invidious::LogHandler.new(File.open(output, mode: "a")) + end end Kemal::CLI.new @@ -101,17 +108,17 @@ LOCALES = { crawl_threads.times do spawn do - crawl_videos(PG_DB) + crawl_videos(PG_DB, logger) end end -refresh_channels(PG_DB, channel_threads, CONFIG.full_refresh) +refresh_channels(PG_DB, logger, channel_threads, CONFIG.full_refresh) -refresh_feeds(PG_DB, feed_threads) +refresh_feeds(PG_DB, logger, feed_threads) video_threads.times do |i| spawn do - refresh_videos(PG_DB) + refresh_videos(PG_DB, logger) end end @@ -295,7 +302,7 @@ get "/watch" do |env| next env.redirect "/watch?v=#{ex.message}" rescue ex error_message = ex.message - STDOUT << id << " : " << ex.message << "\n" + logger.write("#{id} : #{ex.message}\n") next templated "error" end @@ -2135,6 +2142,16 @@ get "/c/:user" do |env| env.redirect anchor["href"] end +# Legacy endpoint for /user/:username +get "/profile" do |env| + user = env.params.query["user"]? + if !user + env.redirect "/" + else + env.redirect "/user/#{user}" + end +end + get "/user/:user" do |env| user = env.params.url["user"] env.redirect "/channel/#{user}" @@ -3849,4 +3866,5 @@ add_handler FilteredCompressHandler.new add_handler DenyFrame.new add_context_storage_type(User) +Kemal.config.logger = logger Kemal.run diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index d5e233b0..91a80203 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -1,21 +1,21 @@ class Config YAML.mapping({ - crawl_threads: Int32, - channel_threads: Int32, - feed_threads: Int32, - video_threads: Int32, - db: NamedTuple( - user: String, + crawl_threads: Int32, # Number of threads to use for finding new videos from YouTube (used to populate "top" page) + channel_threads: Int32, # Number of threads to use for crawling videos from channels (for updating subscriptions) + feed_threads: Int32, # Number of threads to use for updating feeds + video_threads: Int32, # Number of threads to use for updating videos in cache (mostly non-functional) + db: NamedTuple( # Database configuration +user: String, password: String, host: String, port: Int32, dbname: String, ), - dl_api_key: String?, - https_only: Bool?, - hmac_key: String?, - full_refresh: Bool, - domain: String, + dl_api_key: String?, # DetectLanguage API Key (used to filter non-English results from "top" page), mostly non-functional + https_only: Bool?, # Used to tell Invidious it is behind a proxy, so links to resources should be https:// + hmac_key: String?, # HMAC signing key for CSRF tokens + full_refresh: Bool, # Used for crawling channels: threads should check all videos uploaded by a channel + domain: String, # Domain to be used for links to resources on the site where an absolute URL is required }) end diff --git a/src/invidious/helpers/logger.cr b/src/invidious/helpers/logger.cr new file mode 100644 index 00000000..5bb1eb40 --- /dev/null +++ b/src/invidious/helpers/logger.cr @@ -0,0 +1,35 @@ +require "logger" + +class Invidious::LogHandler < Kemal::BaseLogHandler + def initialize(@io : IO = STDOUT) + end + + def call(context : HTTP::Server::Context) + time = Time.now + call_next(context) + elapsed_text = elapsed_text(Time.now - time) + + @io << time << ' ' << context.response.status_code << ' ' << context.request.method << ' ' << context.request.resource << ' ' << elapsed_text << '\n' + + if @io.is_a? File + @io.flush + end + + context + end + + def write(message : String) + @io << message + + if @io.is_a? File + @io.flush + end + end + + private def elapsed_text(elapsed) + millis = elapsed.total_milliseconds + return "#{millis.round(2)}ms" if millis >= 1 + + "#{(millis * 1000).round(2)}µs" + end +end diff --git a/src/invidious/jobs.cr b/src/invidious/jobs.cr index df02c7fb..f6e2d8fe 100644 --- a/src/invidious/jobs.cr +++ b/src/invidious/jobs.cr @@ -1,4 +1,4 @@ -def crawl_videos(db) +def crawl_videos(db, logger) ids = Deque(String).new random = Random.new @@ -21,7 +21,7 @@ def crawl_videos(db) id = ids[0] video = get_video(id, db) rescue ex - STDOUT << id << " : " << ex.message << "\n" + logger.write("#{id} : #{ex.message}\n") next ensure ids.delete(id) @@ -46,7 +46,7 @@ def crawl_videos(db) end end -def refresh_channels(db, max_threads = 1, full_refresh = false) +def refresh_channels(db, logger, max_threads = 1, full_refresh = false) max_channel = Channel(Int32).new spawn do @@ -73,7 +73,7 @@ def refresh_channels(db, max_threads = 1, full_refresh = false) db.exec("UPDATE channels SET updated = $1, author = $2 WHERE id = $3", Time.now, channel.author, id) rescue ex - STDOUT << id << " : " << ex.message << "\n" + logger.write("#{id} : #{ex.message}\n") end active_channel.send(true) @@ -86,7 +86,7 @@ def refresh_channels(db, max_threads = 1, full_refresh = false) max_channel.send(max_threads) end -def refresh_videos(db) +def refresh_videos(db, logger) loop do db.query("SELECT id FROM videos ORDER BY updated") do |rs| rs.each do @@ -94,7 +94,7 @@ def refresh_videos(db) id = rs.read(String) video = get_video(id, db) rescue ex - STDOUT << id << " : " << ex.message << "\n" + logger.write("#{id} : #{ex.message}\n") next end end @@ -104,7 +104,7 @@ def refresh_videos(db) end end -def refresh_feeds(db, max_threads = 1) +def refresh_feeds(db, logger, max_threads = 1) max_channel = Channel(Int32).new spawn do @@ -129,7 +129,7 @@ def refresh_feeds(db, max_threads = 1) begin db.exec("REFRESH MATERIALIZED VIEW #{view_name}") rescue ex - STDOUT << "REFRESH " << email << " : " << ex.message << "\n" + logger.write("REFRESH #{email} : #{ex.message}\n") end active_channel.send(true) diff --git a/src/invidious/views/components/player_sources.ecr b/src/invidious/views/components/player_sources.ecr index 3afce6cb..aed606af 100644 --- a/src/invidious/views/components/player_sources.ecr +++ b/src/invidious/views/components/player_sources.ecr @@ -8,7 +8,7 @@ <script src="/js/videojs-markers.min.js"></script> <script src="/js/videojs-share.min.js"></script> <script src="/js/videojs-http-streaming.min.js"></script> -<% if env.get?("user") && env.get("user").as(User).preferences.quality == "dash" %> +<% if params[:quality] == "dash" %> <script src="/js/dash.mediaplayer.min.js"></script> <script src="/js/videojs-dash.min.js"></script> <script src="/js/videojs-contrib-quality-levels.min.js"></script> diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 832d13d1..d5ab5411 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -53,6 +53,34 @@ <div class="pure-u-1 pure-u-md-1-5"> <div class="h-box"> <p><a href="https://www.youtube.com/watch?v=<%= video.id %>"><%= translate(locale, "Watch video on Youtube") %></a></p> + + <form class="pure-form pure-form-stacked"> + <div class="pure-control-group"> + <label for="download_widget"><%= translate(locale, "Download as: ") %></label> + <select style="width:100%" name="download_widget" id="download_widget"> + <% video_streams.each do |option| %> + <option data-url="<%= option["url"] %>"><%= option["quality_label"] %> - <%= option["type"].split(";")[0] %> @ <%= option["fps"] %>fps - video only</option> + <% end %> + <% audio_streams.each do |option| %> + <option data-url="<%= option["url"] %>"><%= option["type"].split(";")[0] %> @ <%= option["bitrate"] %>k - audio only</option> + <% end %> + <% fmt_stream.each do |option| %> + <option data-url="<%= option["url"] %>"><%= itag_to_metadata?(option["itag"]).try &.["height"]? || "~240" %>p - <%= option["type"].split(";")[0] %></option> + <% end %> + </select> + </div> + + <div id="progress-container" style="width:100%; display:none"> + <div id="download-progress"> + </div> + </div> + + <button type="button" data-title="<%= video.title.dump_unquoted %>-<%= video.id %>.mp4" onclick="download_video(this)" + class="pure-button pure-button-primary"> + <%= translate(locale, "Download") %> + </button> + </form> + <p><i class="icon ion-ios-eye"></i> <%= number_with_separator(video.views) %></p> <p><i class="icon ion-ios-thumbs-up"></i> <%= number_with_separator(video.likes) %></p> <p><i class="icon ion-ios-thumbs-down"></i> <%= number_with_separator(video.dislikes) %></p> @@ -268,8 +296,15 @@ function unsubscribe() { } <% if plid %> -function get_playlist() { +function get_playlist(timeouts = 0) { playlist = document.getElementById("playlist"); + + if (timeouts > 10) { + console.log("Failed to pull playlist"); + playlist.innerHTML = ""; + return; + } + playlist.innerHTML = ' \ <h3><center class="loading"><i class="icon ion-ios-refresh"></i></center></h3> \ <hr>' @@ -323,15 +358,22 @@ function get_playlist() { comments = document.getElementById("playlist"); comments.innerHTML = '<h3><center class="loading"><i class="icon ion-ios-refresh"></i></center></h3><hr>'; - get_playlist(); + get_playlist(timeouts + 1); }; } get_playlist(); <% end %> -function get_reddit_comments() { +function get_reddit_comments(timeouts = 0) { comments = document.getElementById("comments"); + + if (timeouts > 10) { + console.log("Failed to pull comments"); + comments.innerHTML = ""; + return; + } + var fallback = comments.innerHTML; comments.innerHTML = '<h3><center class="loading"><i class="icon ion-ios-refresh"></i></center></h3>'; @@ -382,12 +424,19 @@ function get_reddit_comments() { xhr.ontimeout = function() { console.log("Pulling comments timed out."); - get_reddit_comments(); + get_reddit_comments(timeouts + 1); }; } -function get_youtube_comments() { +function get_youtube_comments(timeouts = 0) { comments = document.getElementById("comments"); + + if (timeouts > 10) { + console.log("Failed to pull comments"); + comments.innerHTML = ""; + return; + } + var fallback = comments.innerHTML; comments.innerHTML = '<h3><center class="loading"><i class="icon ion-ios-refresh"></i></center></h3>'; @@ -438,7 +487,7 @@ function get_youtube_comments() { comments.innerHTML = '<h3><center class="loading"><i class="icon ion-ios-refresh"></i></center></h3>'; - get_youtube_comments(); + get_youtube_comments(timeouts + 1); }; } |
