From bcd1f8596127bf13b167daaca10a562b1f0be4c1 Mon Sep 17 00:00:00 2001 From: "mk-pmb feat. Emilien Devos" <4016501+unixfox@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:30:00 +0200 Subject: [PATCH] Watch view: Better error message when video is unplayable. --- locales/en-US.json | 5 ++++- src/invidious/helpers/errors.cr | 13 ++++++----- src/invidious/videos.cr | 14 ++++++------ src/invidious/videos/parser.cr | 39 ++++++++++++++++++--------------- src/invidious/views/embed.ecr | 12 +++++++++- src/invidious/views/error.ecr | 1 - src/invidious/views/watch.ecr | 15 ++++++++++--- 7 files changed, 62 insertions(+), 37 deletions(-) diff --git a/locales/en-US.json b/locales/en-US.json index 4f2c2770..f7c76f69 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -501,5 +501,8 @@ "toggle_theme": "Toggle Theme", "carousel_slide": "Slide {{current}} of {{total}}", "carousel_skip": "Skip the Carousel", - "carousel_go_to": "Go to slide `x`" + "carousel_go_to": "Go to slide `x`", + "error_from_youtube_unplayable": "Video unplayable due to an error from YouTube:", + "error_processing_data_youtube": "Error while processing the data sent by YouTube", + "refresh_page": "Refresh the page" } diff --git a/src/invidious/helpers/errors.cr b/src/invidious/helpers/errors.cr index 900cb0c6..4cfe63f3 100644 --- a/src/invidious/helpers/errors.cr +++ b/src/invidious/helpers/errors.cr @@ -73,10 +73,6 @@ def error_template_helper(env : HTTP::Server::Context, status_code : Int32, exce END_HTML - # Don't show the usual "next steps" widget. The same options are - # proposed above the error message, just worded differently. - next_steps = "" - return templated "error" end @@ -86,8 +82,13 @@ def error_template_helper(env : HTTP::Server::Context, status_code : Int32, mess locale = env.get("preferences").as(Preferences).locale - error_message = translate(locale, message) - next_steps = error_redirect_helper(env) + error_message = <<-END_HTML +
+

#{translate(locale, "error_processing_data_youtube")}

+

#{translate(locale, message)}

+ #{error_redirect_helper(env)} +
+ END_HTML return templated "error" end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 348a0a66..d7a40cf7 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -313,7 +313,7 @@ def get_video(id, refresh = true, region = nil, force_refresh = false) end else video = fetch_video(id, region) - Invidious::Database::Videos.insert(video) if !region + Invidious::Database::Videos.insert(video) if !region && !video.info.dig?("reason") end return video @@ -326,13 +326,13 @@ end def fetch_video(id, region) info = extract_video_info(video_id: id) - if reason = info["reason"]? + if info["reason"]? && info["subreason"]? + reason = info["reason"].as_s + subreason = info["subreason"].as_s if reason == "Video unavailable" - raise NotFoundException.new(reason.as_s || "") - elsif !reason.as_s.starts_with? "Premieres" - # dont error when it's a premiere. - # we already parsed most of the data and display the premiere date - raise InfoException.new(reason.as_s || "") + raise NotFoundException.new(reason + ": Video not found" || "") + elsif {"Private video"}.any?(reason) + raise InfoException.new(reason + ": " + subreason || "") end end diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 0df2305a..387e5015 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -68,18 +68,20 @@ def extract_video_info(video_id : String) playability_status = player_response.dig?("playabilityStatus", "status").try &.as_s if playability_status != "OK" - subreason = player_response.dig?("playabilityStatus", "errorScreen", "playerErrorMessageRenderer", "subreason") - reason = subreason.try &.[]?("simpleText").try &.as_s - reason ||= subreason.try &.[]("runs").as_a.map(&.[]("text")).join("") - reason ||= player_response.dig("playabilityStatus", "reason").as_s + reason = player_response.dig?("playabilityStatus", "reason").try &.as_s + reason ||= player_response.dig("playabilityStatus", "errorScreen", "playerErrorMessageRenderer", "reason", "simpleText").as_s + subreason_main = player_response.dig?("playabilityStatus", "errorScreen", "playerErrorMessageRenderer", "subreason") + subreason = subreason_main.try &.[]?("simpleText").try &.as_s + subreason ||= subreason_main.try &.[]("runs").as_a.map(&.[]("text")).join("") - # Stop here if video is not a scheduled livestream or - # for LOGIN_REQUIRED when videoDetails element is not found because retrying won't help - if !{"LIVE_STREAM_OFFLINE", "LOGIN_REQUIRED"}.any?(playability_status) || - playability_status == "LOGIN_REQUIRED" && !player_response.dig?("videoDetails") + # Stop if private video or video not found. + # But for video unavailable, only stop if playability_status is ERROR because playability_status UNPLAYABLE + # still gives all the necessary info for displaying the video page (title, description and more) + if {"Private video", "Video unavailable"}.any?(reason) && !{"UNPLAYABLE"}.any?(playability_status) return { - "version" => JSON::Any.new(Video::SCHEMA_VERSION.to_i64), - "reason" => JSON::Any.new(reason), + "version" => JSON::Any.new(Video::SCHEMA_VERSION.to_i64), + "reason" => JSON::Any.new(reason), + "subreason" => JSON::Any.new(subreason), } end elsif video_id != player_response.dig("videoDetails", "videoId") @@ -99,11 +101,8 @@ def extract_video_info(video_id : String) reason = nil end - # Don't fetch the next endpoint if the video is unavailable. - if {"OK", "LIVE_STREAM_OFFLINE", "LOGIN_REQUIRED"}.any?(playability_status) - next_response = YoutubeAPI.next({"videoId": video_id, "params": ""}) - player_response = player_response.merge(next_response) - end + next_response = YoutubeAPI.next({"videoId": video_id, "params": ""}) + player_response = player_response.merge(next_response) params = parse_video_info(video_id, player_response) params["reason"] = JSON::Any.new(reason) if reason @@ -197,16 +196,20 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any end video_details = player_response.dig?("videoDetails") + video_details ||= {} of String => JSON::Any if !(microformat = player_response.dig?("microformat", "playerMicroformatRenderer")) microformat = {} of String => JSON::Any end - raise BrokenTubeException.new("videoDetails") if !video_details - # Basic video infos title = video_details["title"]?.try &.as_s + title ||= extract_text( + video_primary_renderer + .try &.dig?("title") + ) + # We have to try to extract viewCount from videoPrimaryInfoRenderer first, # then from videoDetails, as the latter is "0" for livestreams (we want # to get the amount of viewers watching). @@ -483,7 +486,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any # Description "description" => JSON::Any.new(description || ""), "descriptionHtml" => JSON::Any.new(description_html || "

"), - "shortDescription" => JSON::Any.new(short_description.try &.as_s || nil), + "shortDescription" => JSON::Any.new(short_description.try &.as_s || ""), # Video metadata "genre" => JSON::Any.new(genre.try &.as_s || ""), "genreUcid" => JSON::Any.new(genre_ucid.try &.as_s?), diff --git a/src/invidious/views/embed.ecr b/src/invidious/views/embed.ecr index 1bf5cc3e..a67c99fb 100644 --- a/src/invidious/views/embed.ecr +++ b/src/invidious/views/embed.ecr @@ -31,7 +31,17 @@ %> -<%= rendered "components/player" %> +<% if video.reason.nil? %> +
+ <%= rendered "components/player" %> +
+<% else %> +
+

+ <%= video.reason %> +

+
+<% end %> diff --git a/src/invidious/views/error.ecr b/src/invidious/views/error.ecr index 04eb74d5..d0752e5b 100644 --- a/src/invidious/views/error.ecr +++ b/src/invidious/views/error.ecr @@ -4,5 +4,4 @@
<%= error_message %> - <%= next_steps %>
diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 6f9ced6f..6f61f00e 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -70,9 +70,11 @@ we're going to need to do it here in order to allow for translations. %> +<% if video.reason.nil? %>
<%= rendered "components/player" %>
+<% end %>

@@ -96,7 +98,10 @@ we're going to need to do it here in order to allow for translations. <% if video.reason %>

- <%= video.reason %> + <%= translate(locale, "error_from_youtube_unplayable") %> <%= video.reason %> +

+

+ <%= translate(locale, "next_steps_error_message") %>

<% elsif video.premiere_timestamp.try &.> Time.utc %>

@@ -112,7 +117,7 @@ we're going to need to do it here in order to allow for translations.
- +

<%- link_yt_watch = URI.new(scheme: "https", host: "www.youtube.com", path: "/watch", query: "v=#{video.id}") link_yt_embed = URI.new(scheme: "https", host: "www.youtube.com", path: "/embed/#{video.id}") @@ -125,7 +130,7 @@ we're going to need to do it here in order to allow for translations. -%> <%= translate(locale, "videoinfo_watch_on_youTube") %> (<%= translate(locale, "videoinfo_youTube_embed_link") %>) - +

<%- link_iv_other = IV::Frontend::Misc.redirect_url(env) -%> @@ -185,11 +190,14 @@ we're going to need to do it here in order to allow for translations. <% end %> <% end %> + <% if video_assets %> <%= Invidious::Frontend::WatchPage.download_widget(locale, video, video_assets) %> + <% end %>

<%= number_with_separator(video.views) %>

<%= number_with_separator(video.likes) %>

+ <% if video.genre %>

<%= translate(locale, "Genre: ") %> <% if !video.genre_url %> <%= video.genre %> @@ -197,6 +205,7 @@ we're going to need to do it here in order to allow for translations. <%= video.genre %> <% end %>

+ <% end %> <% if video.license %> <% if video.license.empty? %>

<%= translate(locale, "License: ") %><%= translate(locale, "Standard YouTube license") %>