Invidious is an alternative front-end to YouTube
Move 'pretty=1' into middleware
Omar Roth57c05354c214
diff --git a/src/invidious.cr b/src/invidious.crindex 068ad201..ae7c9610 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -1385,7 +1385,8 @@ get "/mark_watched" do |env|id = env.params.query["id"]?if !id- halt env, status_code: 400+ env.response.status_code = 400+ nextendredirect = env.params.query["redirect"]?@@ -1415,7 +1416,8 @@ get "/mark_unwatched" do |env|id = env.params.query["id"]?if !id- halt env, status_code: 400+ env.response.status_code = 400+ nextendredirect = env.params.query["redirect"]?@@ -2071,7 +2073,8 @@ get "/feed/channel/:ucid" do |env|author, ucid, auto_generated = get_about_info(ucid, locale)rescue exerror_message = ex.message- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendclient = make_client(YT_URL)@@ -2181,12 +2184,14 @@ get "/feed/private" do |env|token = env.params.query["token"]?if !token- halt env, status_code: 403+ env.response.status_code = 403+ nextenduser = PG_DB.query_one?("SELECT * FROM users WHERE token = $1", token.strip, as: User)if !user- halt env, status_code: 403+ env.response.status_code = 403+ nextendmax_results = env.params.query["max_results"]?.try &.to_i?@@ -2342,17 +2347,20 @@ get "/feed/webhook/:token" do |env|# The hub will sometimes check if we're still subscribed after delivery errorsif Time.now.to_unix - time.to_i > 432000- halt env, status_code: 400+ env.response.status_code = 400+ nextendif OpenSSL::HMAC.hexdigest(:sha1, HMAC_KEY, data) != signature- halt env, status_code: 400+ env.response.status_code = 400+ nextenducid = HTTP::Params.parse(URI.parse(topic).query.not_nil!)["channel_id"]PG_DB.exec("UPDATE channels SET subscribed = $1 WHERE id = $2", Time.now, ucid)- halt env, status_code: 200, response: challenge+ env.response.status_code = 200+ next challengeendpost "/feed/webhook/:token" do |env|@@ -2362,7 +2370,8 @@ post "/feed/webhook/:token" do |env|if signature != OpenSSL::HMAC.hexdigest(:sha1, HMAC_KEY, body)logger.write("#{token} : Invalid signature")- halt env, status_code: 200+ env.response.status_code = 200+ nextendspawn do@@ -2388,7 +2397,8 @@ post "/feed/webhook/:token" do |env|endend- halt env, status_code: 200+ env.response.status_code = 200+ nextend# Channels@@ -2550,19 +2560,17 @@ get "/api/v1/stats" do |env|if !config.statistics_enablederror_message = {"error" => "Statistics are not enabled."}.to_json- halt env, status_code: 400, response: error_message+ env.response.status_code = 400+ next error_messageendif statistics["error"]?- halt env, status_code: 500, response: statistics.to_json+ env.response.status_code = 500+ next statistics.to_jsonend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- statistics.to_pretty_json- elsestatistics.to_jsonend-endget "/api/v1/captions/:id" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -2578,7 +2586,8 @@ get "/api/v1/captions/:id" do |env|rescue ex : VideoRedirectnext env.redirect "/api/v1/captions/#{ex.message}"rescue ex- halt env, status_code: 500+ env.response.status_code = 500+ nextendcaptions = video.captions@@ -2604,12 +2613,8 @@ get "/api/v1/captions/:id" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- next JSON.parse(response).to_pretty_json- elsenext responseend- endenv.response.content_type = "text/vtt"@@ -2620,7 +2625,8 @@ get "/api/v1/captions/:id" do |env|endif caption.empty?- halt env, status_code: 404+ env.response.status_code = 404+ nextelsecaption = caption[0]end@@ -2690,7 +2696,8 @@ get "/api/v1/comments/:id" do |env|comments = fetch_youtube_comments(id, PG_DB, continuation, proxies, format, locale, region)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendnext comments@@ -2708,18 +2715,15 @@ get "/api/v1/comments/:id" do |env|endif !reddit_thread || !comments- halt env, status_code: 404+ env.response.status_code = 404+ nextendif format == "json"reddit_thread = JSON.parse(reddit_thread.to_json).as_hreddit_thread["comments"] = JSON.parse(comments.to_json)- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- next reddit_thread.to_pretty_json- elsenext reddit_thread.to_json- endelseresponse = {"title" => reddit_thread.title,@@ -2727,14 +2731,10 @@ get "/api/v1/comments/:id" do |env|"contentHtml" => content_html,}- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- next response.to_pretty_json- elsenext response.to_jsonendendend-endget "/api/v1/insights/:id" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -2743,7 +2743,8 @@ get "/api/v1/insights/:id" do |env|env.response.content_type = "application/json"error_message = {"error" => "YouTube has removed publicly-available analytics."}.to_json- halt env, status_code: 410, response: error_message+ env.response.status_code = 410+ next error_messageclient = make_client(YT_URL)headers = HTTP::Headers.new@@ -2820,12 +2821,8 @@ get "/api/v1/insights/:id" do |env|"graphData" => graph_data,}- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- next response.to_pretty_json- elsenext response.to_jsonend-endget "/api/v1/videos/:id" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -2841,7 +2838,8 @@ get "/api/v1/videos/:id" do |env|next env.redirect "/api/v1/videos/#{ex.message}"rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendfmt_stream = video.fmt_stream(decrypt_function)@@ -3029,12 +3027,8 @@ get "/api/v1/videos/:id" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(video_info).to_pretty_json- elsevideo_infoend-endget "/api/v1/trending" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3048,7 +3042,8 @@ get "/api/v1/trending" do |env|trending = fetch_trending(trending_type, proxies, region, locale)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendvideos = JSON.build do |json|@@ -3080,12 +3075,8 @@ get "/api/v1/trending" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(videos).to_pretty_json- elsevideosend-endget "/api/v1/popular" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3114,12 +3105,8 @@ get "/api/v1/popular" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(videos).to_pretty_json- elsevideosend-endget "/api/v1/top" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3128,7 +3115,8 @@ get "/api/v1/top" do |env|if !config.top_enablederror_message = {"error" => "Administrator has disabled this endpoint."}.to_json- halt env, status_code: 400, response: error_message+ env.response.status_code = 400+ next error_messageendvideos = JSON.build do |json|@@ -3160,12 +3148,8 @@ get "/api/v1/top" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(videos).to_pretty_json- elsevideosend-endget "/api/v1/channels/:ucid" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3180,7 +3164,8 @@ get "/api/v1/channels/:ucid" do |env|author, ucid, auto_generated = get_about_info(ucid, locale)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendpage = 1@@ -3192,7 +3177,8 @@ get "/api/v1/channels/:ucid" do |env|videos, count = get_60_videos(ucid, page, auto_generated, sort_by)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendend@@ -3367,12 +3353,8 @@ get "/api/v1/channels/:ucid" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(channel_info).to_pretty_json- elsechannel_infoend-end["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route|get route do |env|@@ -3391,14 +3373,16 @@ endauthor, ucid, auto_generated = get_about_info(ucid, locale)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendbeginvideos, count = get_60_videos(ucid, page, auto_generated, sort_by)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendresult = JSON.build do |json|@@ -3437,13 +3421,9 @@ endendend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(result).to_pretty_json- elseresultendend-end["/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"].each do |route|get route do |env|@@ -3457,7 +3437,8 @@ endvideos = get_latest_videos(ucid)rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendresponse = JSON.build do |json|@@ -3489,13 +3470,9 @@ endendend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(response).to_pretty_json- elseresponseendend-end["/api/v1/channels/:ucid/playlists", "/api/v1/channels/playlists/:ucid"].each do |route|get route do |env|@@ -3512,8 +3489,9 @@ endbeginauthor, ucid, auto_generated = get_about_info(ucid, locale)rescue ex- error_message = ex.message- halt env, status_code: 500, response: error_message+ error_message = {"error" => ex.message}.to_json+ env.response.status_code = 500+ next error_messageenditems, continuation = fetch_channel_playlists(ucid, author, auto_generated, continuation, sort_by)@@ -3558,13 +3536,9 @@ endendend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(response).to_pretty_json- elseresponseendend-endget "/api/v1/channels/search/:ucid" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3663,12 +3637,8 @@ get "/api/v1/channels/search/:ucid" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(response).to_pretty_json- elseresponseend-endget "/api/v1/search" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3792,12 +3762,8 @@ get "/api/v1/search" do |env|endend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(response).to_pretty_json- elseresponseend-endget "/api/v1/playlists/:plid" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3821,7 +3787,8 @@ get "/api/v1/playlists/:plid" do |env|playlist = fetch_playlist(plid, locale)rescue exerror_message = {"error" => "Playlist is empty"}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendbegin@@ -3895,12 +3862,8 @@ get "/api/v1/playlists/:plid" do |env|}.to_jsonend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(response).to_pretty_json- elseresponseend-endget "/api/v1/mixes/:rdid" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?@@ -3927,7 +3890,8 @@ get "/api/v1/mixes/:rdid" do |env|mix.videos = mix.videos[index..-1]rescue exerror_message = {"error" => ex.message}.to_json- halt env, status_code: 500, response: error_message+ env.response.status_code = 500+ next error_messageendresponse = JSON.build do |json|@@ -3973,11 +3937,7 @@ get "/api/v1/mixes/:rdid" do |env|}.to_jsonend- if env.params.query["pretty"]? && env.params.query["pretty"] == "1"- JSON.parse(response).to_pretty_json- else- response- end+ responseendget "/api/manifest/dash/id/videoplayback" do |env|@@ -4009,7 +3969,8 @@ get "/api/manifest/dash/id/:id" do |env|next env.redirect urlrescue ex- halt env, status_code: 403+ env.response.status_code = 403+ nextendif dashmpd = video.player_response["streamingData"]?.try &.["dashManifestUrl"]?.try &.as_s@@ -4096,7 +4057,8 @@ get "/api/manifest/hls_variant/*" do |env|manifest = client.get(env.request.path)if manifest.status_code != 200- halt env, status_code: manifest.status_code+ env.response.status_code = manifest.status_code+ nextendenv.response.content_type = "application/x-mpegURL"@@ -4113,7 +4075,8 @@ get "/api/manifest/hls_playlist/*" do |env|manifest = client.get(env.request.path)if manifest.status_code != 200- halt env, status_code: manifest.status_code+ env.response.status_code = manifest.status_code+ nextendhost_url = make_host_url(config, Kemal.config)@@ -4149,7 +4112,8 @@ get "/latest_version" do |env|local = local == "true"if !id || !itag- halt env, status_code: 400+ env.response.status_code = 400+ nextendvideo = get_video(id, PG_DB, proxies, region: region)@@ -4159,9 +4123,11 @@ get "/latest_version" do |env|urls = (fmt_stream + adaptive_fmts).select { |fmt| fmt["itag"] == itag }if urls.empty?- halt env, status_code: 404+ env.response.status_code = 404+ nextelsif urls.size > 1- halt env, status_code: 409+ env.response.status_code = 409+ nextendurl = urls[0]["url"]@@ -4285,7 +4251,8 @@ get "/videoplayback" do |env|endif response.status_code >= 400- halt env, status_code: response.status_code+ env.response.status_code = response.status_code+ nextendclient = make_client(URI.parse(host), proxies, region)@@ -4509,8 +4476,8 @@ public_folder "assets"Kemal.config.powered_by_header = falseadd_handler FilteredCompressHandler.new-add_handler DenyFrame.newadd_handler APIHandler.new+add_handler DenyFrame.newadd_context_storage_type(User)add_context_storage_type(Preferences)diff --git a/src/invidious/helpers/handlers.cr b/src/invidious/helpers/handlers.crnew file mode 100644index 00000000..4a7b5650--- /dev/null+++ b/src/invidious/helpers/handlers.cr@@ -0,0 +1,132 @@+module HTTP::Handler+ @@exclude_routes_tree = Radix::Tree(String).new++ macro exclude(paths, method = "GET")+ class_name = {{@type.name}}+ method_downcase = {{method.downcase}}+ class_name_method = "#{class_name}/#{method_downcase}"+ ({{paths}}).each do |path|+ @@exclude_routes_tree.add class_name_method + path, '/' + method_downcase + path+ end+ end++ def exclude_match?(env : HTTP::Server::Context)+ @@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found?+ end++ private def radix_path(method : String, path : String)+ "#{self.class}/#{method.downcase}#{path}"+ end+end++class Kemal::RouteHandler+ exclude ["/api/v1/*"]++ # Processes the route if it's a match. Otherwise renders 404.+ private def process_request(context)+ raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_found?+ content = context.route.handler.call(context)++ if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(context.response.status_code) && exclude_match?(context)+ raise Kemal::Exceptions::CustomException.new(context)+ end++ context.response.print(content)+ context+ end+end++class Kemal::ExceptionHandler+ exclude ["/api/v1/*"]++ private def call_exception_with_status_code(context : HTTP::Server::Context, exception : Exception, status_code : Int32)+ return if context.response.closed?+ return if exclude_match? context++ if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(status_code)+ context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")+ context.response.status_code = status_code+ context.response.print Kemal.config.error_handlers[status_code].call(context, exception)+ context+ end+ end+end++class FilteredCompressHandler < Kemal::Handler+ exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/ggpht/*"]++ def call(env)+ return call_next env if exclude_match? env++ {% if flag?(:without_zlib) %}+ call_next env+ {% else %}+ request_headers = env.request.headers++ if request_headers.includes_word?("Accept-Encoding", "gzip")+ env.response.headers["Content-Encoding"] = "gzip"+ env.response.output = Gzip::Writer.new(env.response.output, sync_close: true)+ elsif request_headers.includes_word?("Accept-Encoding", "deflate")+ env.response.headers["Content-Encoding"] = "deflate"+ env.response.output = Flate::Writer.new(env.response.output, sync_close: true)+ end++ call_next env+ {% end %}+ end+end++class APIHandler < Kemal::Handler+ only ["/api/v1/*"]++ def call(env)+ return call_next env unless only_match? env++ env.response.headers["Access-Control-Allow-Origin"] = "*"++ # Here we swap out the socket IO so we can modify the response as needed+ output = env.response.output+ env.response.output = IO::Memory.new++ begin+ call_next env++ env.response.output.rewind+ response = env.response.output.gets_to_end++ if env.response.headers["Content-Type"]?.try &.== "application/json"+ response = JSON.parse(response)++ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"+ response = response.to_pretty_json+ else+ response = response.to_json+ end+ end+ ensure+ env.response.output = output+ env.response.puts response++ env.response.flush+ end+ end+end++class DenyFrame < Kemal::Handler+ exclude ["/embed/*"]++ def call(env)+ return call_next env if exclude_match? env++ env.response.headers["X-Frame-Options"] = "sameorigin"+ call_next env+ end+end++# Temp fix for https://github.com/crystal-lang/crystal/issues/7383+class HTTP::Client+ private def handle_response(response)+ # close unless response.keep_alive?+ response+ end+enddiff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.crindex 4e827bd8..23264559 100644--- a/src/invidious/helpers/helpers.cr+++ b/src/invidious/helpers/helpers.cr@@ -28,61 +28,6 @@ user: String,})end-class FilteredCompressHandler < Kemal::Handler- exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/api/*", "/ggpht/*"]-- def call(env)- return call_next env if exclude_match? env-- {% if flag?(:without_zlib) %}- call_next env- {% else %}- request_headers = env.request.headers-- if request_headers.includes_word?("Accept-Encoding", "gzip")- env.response.headers["Content-Encoding"] = "gzip"- env.response.output = Gzip::Writer.new(env.response.output, sync_close: true)- elsif request_headers.includes_word?("Accept-Encoding", "deflate")- env.response.headers["Content-Encoding"] = "deflate"- env.response.output = Flate::Writer.new(env.response.output, sync_close: true)- end-- call_next env- {% end %}- end-end--class APIHandler < Kemal::Handler- only ["/api/v1/*"]-- def call(env)- return call_next env unless only_match? env-- env.response.headers["Access-Control-Allow-Origin"] = "*"-- call_next env- end-end--class DenyFrame < Kemal::Handler- exclude ["/embed/*"]-- def call(env)- return call_next env if exclude_match? env-- env.response.headers["X-Frame-Options"] = "sameorigin"- call_next env- end-end--# Temp fix for https://github.com/crystal-lang/crystal/issues/7383-class HTTP::Client- private def handle_response(response)- # close unless response.keep_alive?- response- end-end-def rank_videos(db, n)top = [] of {Float64, String}