Invidious is an alternative front-end to YouTube
Add 404 status code on all possible endpoints
Émilien Devosc201ea53ba4b
diff --git a/src/invidious/channels/about.cr b/src/invidious/channels/about.crindex da71e9a8..31b19bbe 100644--- a/src/invidious/channels/about.cr+++ b/src/invidious/channels/about.cr@@ -31,7 +31,12 @@ def get_about_info(ucid, locale) : AboutChannelendif initdata.dig?("alerts", 0, "alertRenderer", "type") == "ERROR"- raise InfoException.new(initdata["alerts"][0]["alertRenderer"]["text"]["simpleText"].as_s)+ error_message = initdata["alerts"][0]["alertRenderer"]["text"]["simpleText"].as_s+ if error_message == "This channel does not exist."+ raise NotFoundException.new(error_message)+ else+ raise InfoException.new(error_message)+ endendif browse_endpoint = initdata["onResponseReceivedActions"]?.try &.[0]?.try &.["navigateAction"]?.try &.["endpoint"]?.try &.["browseEndpoint"]?diff --git a/src/invidious/channels/community.cr b/src/invidious/channels/community.crindex 4701ecbd..ebef0edb 100644--- a/src/invidious/channels/community.cr+++ b/src/invidious/channels/community.cr@@ -6,7 +6,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)endif response.status_code != 200- raise InfoException.new("This channel does not exist.")+ raise NotFoundException.new("This channel does not exist.")enducid = response.body.match(/https:\/\/www.youtube.com\/channel\/(?<ucid>UC[a-zA-Z0-9_-]{22})/).not_nil!["ucid"]@@ -49,7 +49,11 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)error_message = (message["text"]["simpleText"]? ||message["text"]["runs"]?.try &.[0]?.try &.["text"]?).try &.as_s || ""- raise InfoException.new(error_message)+ if error_message == "This channel does not exist."+ raise NotFoundException.new(error_message)+ else+ raise InfoException.new(error_message)+ endendresponse = JSON.build do |json|diff --git a/src/invidious/comments.cr b/src/invidious/comments.crindex d8496978..f2e63265 100644--- a/src/invidious/comments.cr+++ b/src/invidious/comments.cr@@ -95,7 +95,7 @@ def fetch_youtube_comments(id, cursor, format, locale, thin_mode, region, sort_bcontents = body["contents"]?header = body["header"]?else- raise InfoException.new("Could not fetch comments")+ raise NotFoundException.new("Comments not found.")endif !contents@@ -290,7 +290,7 @@ def fetch_reddit_comments(id, sort_by = "confidence")thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)else- raise InfoException.new("Could not fetch comments")+ raise NotFoundException.new("Comments not found.")endclient.closediff --git a/src/invidious/exceptions.cr b/src/invidious/exceptions.crindex bfaa3fd5..1706ba6a 100644--- a/src/invidious/exceptions.cr+++ b/src/invidious/exceptions.cr@@ -18,3 +18,7 @@ class BrokenTubeException < Exceptionreturn "Missing JSON element \"#{@element}\""endend++# Exception used to hold the bogus UCID during a channel search.+class NotFoundException < InfoException+enddiff --git a/src/invidious/playlists.cr b/src/invidious/playlists.crindex aefa34cc..c4eb7507 100644--- a/src/invidious/playlists.cr+++ b/src/invidious/playlists.cr@@ -317,7 +317,7 @@ def get_playlist(plid : String)if playlist = Invidious::Database::Playlists.select(id: plid)return playlistelse- raise InfoException.new("Playlist does not exist.")+ raise NotFoundException.new("Playlist does not exist.")endelsereturn fetch_playlist(plid)diff --git a/src/invidious/routes/api/manifest.cr b/src/invidious/routes/api/manifest.crindex 8bc36946..f8766b66 100644--- a/src/invidious/routes/api/manifest.cr+++ b/src/invidious/routes/api/manifest.cr@@ -16,6 +16,8 @@ module Invidious::Routes::API::Manifestvideo = get_video(id, region: region)rescue ex : VideoRedirectreturn env.redirect env.request.resource.gsub(id, ex.video_id)+ rescue ex : NotFoundException+ haltf env, status_code: 404rescue exhaltf env, status_code: 403enddiff --git a/src/invidious/routes/api/v1/authenticated.cr b/src/invidious/routes/api/v1/authenticated.crindex b559a01a..1f5ad8ef 100644--- a/src/invidious/routes/api/v1/authenticated.cr+++ b/src/invidious/routes/api/v1/authenticated.cr@@ -237,6 +237,8 @@ module Invidious::Routes::API::V1::Authenticatedbeginvideo = get_video(video_id)+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exreturn error_json(500, ex)enddiff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.crindex 8650976d..6b81c546 100644--- a/src/invidious/routes/api/v1/channels.cr+++ b/src/invidious/routes/api/v1/channels.cr@@ -13,6 +13,8 @@ module Invidious::Routes::API::V1::Channelsrescue ex : ChannelRedirectenv.response.headers["Location"] = env.request.resource.gsub(ucid, ex.channel_id)return error_json(302, "Channel is unavailable", {"authorId" => ex.channel_id})+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exreturn error_json(500, ex)end@@ -170,6 +172,8 @@ module Invidious::Routes::API::V1::Channelsrescue ex : ChannelRedirectenv.response.headers["Location"] = env.request.resource.gsub(ucid, ex.channel_id)return error_json(302, "Channel is unavailable", {"authorId" => ex.channel_id})+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exreturn error_json(500, ex)end@@ -205,6 +209,8 @@ module Invidious::Routes::API::V1::Channelsrescue ex : ChannelRedirectenv.response.headers["Location"] = env.request.resource.gsub(ucid, ex.channel_id)return error_json(302, "Channel is unavailable", {"authorId" => ex.channel_id})+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exreturn error_json(500, ex)enddiff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.crindex a9f891f5..1b7b4fa7 100644--- a/src/invidious/routes/api/v1/videos.cr+++ b/src/invidious/routes/api/v1/videos.cr@@ -12,6 +12,8 @@ module Invidious::Routes::API::V1::Videosrescue ex : VideoRedirectenv.response.headers["Location"] = env.request.resource.gsub(id, ex.video_id)return error_json(302, "Video is unavailable", {"videoId" => ex.video_id})+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exreturn error_json(500, ex)end@@ -42,6 +44,8 @@ module Invidious::Routes::API::V1::Videosrescue ex : VideoRedirectenv.response.headers["Location"] = env.request.resource.gsub(id, ex.video_id)return error_json(302, "Video is unavailable", {"videoId" => ex.video_id})+ rescue ex : NotFoundException+ haltf env, 404rescue exhaltf env, 500end@@ -167,6 +171,8 @@ module Invidious::Routes::API::V1::Videosrescue ex : VideoRedirectenv.response.headers["Location"] = env.request.resource.gsub(id, ex.video_id)return error_json(302, "Video is unavailable", {"videoId" => ex.video_id})+ rescue ex : NotFoundException+ haltf env, 404rescue exhaltf env, 500end@@ -324,6 +330,8 @@ module Invidious::Routes::API::V1::Videosbegincomments = fetch_youtube_comments(id, continuation, format, locale, thin_mode, region, sort_by: sort_by)+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exreturn error_json(500, ex)enddiff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.crindex cd2e3323..c6e02cbd 100644--- a/src/invidious/routes/channels.cr+++ b/src/invidious/routes/channels.cr@@ -85,6 +85,9 @@ module Invidious::Routes::Channelsrescue ex : InfoExceptionenv.response.status_code = 500error_message = ex.message+ rescue ex : NotFoundException+ env.response.status_code = 404+ error_message = ex.messagerescue exreturn error_template(500, ex)end@@ -118,7 +121,7 @@ module Invidious::Routes::Channelsresolved_url = YoutubeAPI.resolve_url("https://youtube.com#{env.request.path}#{yt_url_params.size > 0 ? "?#{yt_url_params}" : ""}")ucid = resolved_url["endpoint"]["browseEndpoint"]["browseId"]rescue ex : InfoException | KeyError- raise InfoException.new(translate(locale, "This channel does not exist."))+ return error_template(404, translate(locale, "This channel does not exist."))endselected_tab = env.request.path.split("/")[-1]@@ -141,7 +144,7 @@ module Invidious::Routes::Channelsuser = env.params.query["user"]?if !user- raise InfoException.new("This channel does not exist.")+ return error_template(404, "This channel does not exist.")elseenv.redirect "/user/#{user}#{uri_params}"end@@ -197,6 +200,8 @@ module Invidious::Routes::Channelschannel = get_about_info(ucid, locale)rescue ex : ChannelRedirectreturn env.redirect env.request.resource.gsub(ucid, ex.channel_id)+ rescue ex : NotFoundException+ return error_template(404, ex)rescue exreturn error_template(500, ex)enddiff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.crindex 207970b0..84da9993 100644--- a/src/invidious/routes/embed.cr+++ b/src/invidious/routes/embed.cr@@ -7,6 +7,8 @@ module Invidious::Routes::Embedplaylist = get_playlist(plid)offset = env.params.query["index"]?.try &.to_i? || 0videos = get_playlist_videos(playlist, offset: offset)+ rescue ex : NotFoundException+ return error_template(404, ex)rescue exreturn error_template(500, ex)end@@ -60,6 +62,8 @@ module Invidious::Routes::Embedplaylist = get_playlist(plid)offset = env.params.query["index"]?.try &.to_i? || 0videos = get_playlist_videos(playlist, offset: offset)+ rescue ex : NotFoundException+ return error_template(404, ex)rescue exreturn error_template(500, ex)end@@ -119,6 +123,8 @@ module Invidious::Routes::Embedvideo = get_video(id, region: params.region)rescue ex : VideoRedirectreturn env.redirect env.request.resource.gsub(id, ex.video_id)+ rescue ex : NotFoundException+ return error_template(404, ex)rescue exreturn error_template(500, ex)enddiff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.crindex b5b58399..31120ecb 100644--- a/src/invidious/routes/feeds.cr+++ b/src/invidious/routes/feeds.cr@@ -150,6 +150,8 @@ module Invidious::Routes::Feedschannel = get_about_info(ucid, locale)rescue ex : ChannelRedirectreturn env.redirect env.request.resource.gsub(ucid, ex.channel_id)+ rescue ex : NotFoundException+ return error_atom(404, ex)rescue exreturn error_atom(500, ex)enddiff --git a/src/invidious/routes/playlists.cr b/src/invidious/routes/playlists.crindex de981d81..fe7e4e1c 100644--- a/src/invidious/routes/playlists.cr+++ b/src/invidious/routes/playlists.cr@@ -66,7 +66,13 @@ module Invidious::Routes::Playlistsuser = user.as(User)playlist_id = env.params.query["list"]- playlist = get_playlist(playlist_id)+ begin+ playlist = get_playlist(playlist_id)+ rescue ex : NotFoundException+ return error_template(404, ex)+ rescue ex+ return error_template(500, ex)+ endsubscribe_playlist(user, playlist)env.redirect "/playlist?list=#{playlist.id}"@@ -304,6 +310,8 @@ module Invidious::Routes::Playlistsplaylist_id = env.params.query["playlist_id"]playlist = get_playlist(playlist_id).as(InvidiousPlaylist)raise "Invalid user" if playlist.author != user.email+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exif redirectreturn error_template(400, ex)@@ -334,6 +342,8 @@ module Invidious::Routes::Playlistsbeginvideo = get_video(video_id)+ rescue ex : NotFoundException+ return error_json(404, ex)rescue exif redirectreturn error_template(500, ex)@@ -394,6 +404,8 @@ module Invidious::Routes::Playlistsbeginplaylist = get_playlist(plid)+ rescue ex : NotFoundException+ return error_template(404, ex)rescue exreturn error_template(500, ex)enddiff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.crindex 3a92ef96..560f9c19 100644--- a/src/invidious/routes/video_playback.cr+++ b/src/invidious/routes/video_playback.cr@@ -265,7 +265,13 @@ module Invidious::Routes::VideoPlaybackreturn error_template(403, "Administrator has disabled this endpoint.")end- video = get_video(id, region: region)+ begin+ video = get_video(id, region: region)+ rescue ex : NotFoundException+ return error_template(404, ex)+ rescue ex+ return error_template(500, ex)+ endfmt = video.fmt_stream.find(nil) { |f| f["itag"].as_i == itag } || video.adaptive_fmts.find(nil) { |f| f["itag"].as_i == itag }url = fmt.try &.["url"]?.try &.as_sdiff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.crindex 7280de4f..fe1d8e54 100644--- a/src/invidious/routes/watch.cr+++ b/src/invidious/routes/watch.cr@@ -63,6 +63,9 @@ module Invidious::Routes::Watchvideo = get_video(id, region: params.region)rescue ex : VideoRedirectreturn env.redirect env.request.resource.gsub(id, ex.video_id)+ rescue ex : NotFoundException+ LOGGER.error("get_video not found: #{id} : #{ex.message}")+ return error_template(404, ex)rescue exLOGGER.error("get_video: #{id} : #{ex.message}")return error_template(500, ex)diff --git a/src/invidious/videos.cr b/src/invidious/videos.crindex f65b05bb..20204d81 100644--- a/src/invidious/videos.cr+++ b/src/invidious/videos.cr@@ -1159,7 +1159,11 @@ def fetch_video(id, region)endif reason = info["reason"]?- raise InfoException.new(reason.as_s || "")+ if reason == "Video unavailable"+ raise NotFoundException.new(reason.as_s || "")+ else+ raise InfoException.new(reason.as_s || "")+ endendvideo = Video.new({