From 8b56a038b7c30cb41dbaf67dd42566d84787d79f Mon Sep 17 00:00:00 2001 From: saltycrys <73420320+saltycrys@users.noreply.github.com> Date: Mon, 4 Jan 2021 05:24:08 +0100 Subject: [PATCH 1/2] Set content type for HTML error helpers This fixes `Unexpected char '<' at 1:1` errors caused by content type mismatch. --- src/invidious/helpers/errors.cr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/invidious/helpers/errors.cr b/src/invidious/helpers/errors.cr index 4487ff8c..2c62d44b 100644 --- a/src/invidious/helpers/errors.cr +++ b/src/invidious/helpers/errors.cr @@ -26,6 +26,7 @@ def error_template_helper(env : HTTP::Server::Context, config : Config, locale : if exception.is_a?(InfoException) return error_template_helper(env, config, locale, status_code, exception.message || "") end + env.response.content_type = "text/html" env.response.status_code = status_code issue_template = %(Title: `#{exception.message} (#{exception.class})`) issue_template += %(\nDate: `#{Time::Format::ISO_8601_DATE_TIME.format(Time.utc)}`) @@ -43,6 +44,7 @@ def error_template_helper(env : HTTP::Server::Context, config : Config, locale : end def error_template_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String) + env.response.content_type = "text/html" env.response.status_code = status_code error_message = translate(locale, message) return templated "error" From 36e9fb9d6835dc3738e6142d1c91d7a2bd249d35 Mon Sep 17 00:00:00 2001 From: saltycrys <73420320+saltycrys@users.noreply.github.com> Date: Mon, 4 Jan 2021 05:35:59 +0100 Subject: [PATCH 2/2] Fix `watch_videos` endpoint Playlists created by `watch_videos` do not have an author which caused a crash previously. --- src/invidious.cr | 2 ++ src/invidious/playlists.cr | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 8d4c2e58..1d2125cb 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -2621,6 +2621,8 @@ end begin playlist = get_playlist(PG_DB, plid, locale) + rescue ex : InfoException + next error_json(404, ex) rescue ex next error_json(404, "Playlist does not exist.") end diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index d5b41caa..25797a36 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -365,9 +365,13 @@ def fetch_playlist(plid, locale) end initial_data = extract_initial_data(response.body) - playlist_info = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]?.try &.[0]["playlistSidebarPrimaryInfoRenderer"]? + playlist_sidebar_renderer = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]? + raise InfoException.new("Could not extract playlistSidebarRenderer.") if !playlist_sidebar_renderer + + playlist_info = playlist_sidebar_renderer[0]["playlistSidebarPrimaryInfoRenderer"]? raise InfoException.new("Could not extract playlist info") if !playlist_info + title = playlist_info["title"]?.try &.["runs"][0]?.try &.["text"]?.try &.as_s || "" desc_item = playlist_info["description"]? @@ -392,14 +396,18 @@ def fetch_playlist(plid, locale) end end - author_info = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]?.try &.[1]["playlistSidebarSecondaryInfoRenderer"]? - .try &.["videoOwner"]["videoOwnerRenderer"]? + if playlist_sidebar_renderer.size < 2 + author = "" + author_thumbnail = "" + ucid = "" + else + author_info = playlist_sidebar_renderer[1]["playlistSidebarSecondaryInfoRenderer"]?.try &.["videoOwner"]["videoOwnerRenderer"]? + raise InfoException.new("Could not extract author info") if !author_info - raise InfoException.new("Could not extract author info") if !author_info - - author_thumbnail = author_info["thumbnail"]["thumbnails"][0]["url"]?.try &.as_s || "" - author = author_info["title"]["runs"][0]["text"]?.try &.as_s || "" - ucid = author_info["title"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"]?.try &.as_s || "" + author = author_info["title"]["runs"][0]["text"]?.try &.as_s || "" + author_thumbnail = author_info["thumbnail"]["thumbnails"][0]["url"]?.try &.as_s || "" + ucid = author_info["title"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"]?.try &.as_s || "" + end return Playlist.new({ title: title,