From 19de693298d849e4364f43e4513bafe28aaad58d Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sat, 25 Nov 2023 19:50:13 -0500 Subject: [PATCH] Add instance test to make sure thumbnails are reachable --- CHANGELOG.md | 4 ++ .../InvidiousInstanceTestingTask.bs | 60 +++++++++++++++++++ playlet-lib/src/source/services/HttpClient.bs | 14 ++++- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3458f485..6decc4cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- An instance test to make sure thumbnails are reachable + ## [0.17.0] - 2023-11-17 ### Added diff --git a/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTestingTask.bs b/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTestingTask.bs index b1308232..3469526a 100644 --- a/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTestingTask.bs +++ b/playlet-lib/src/components/Screens/SettingsScreen/InvidiousInstance/InvidiousInstanceTesting/InvidiousInstanceTestingTask.bs @@ -2,6 +2,7 @@ import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/source/services/HttpClient.bs" import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/StringUtils.bs" +import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" @asynctask function InvidiousInstanceTestingTask(input as object) as object @@ -29,6 +30,7 @@ function PrepareTests() as object content.AppendChild(CreateTestNode("Fetch video details", CanFetchVideoDetails)) content.AppendChild(CreateTestNode("Fetch video comments", CanFetchVideoComments)) content.AppendChild(CreateTestNode("Fetch video captions", CanFetchVideoCaptions)) + content.AppendChild(CreateTestNode("Fetch video thumbnails", CanFetchVideoThumbails)) return content end function @@ -251,3 +253,61 @@ function CanFetchVideoCaptions(instance as string, testNode as object) as void testNode.details = ErrorUtils.Format(e) end try end function + +function CanFetchVideoThumbails(instance as string, testNode as object) as void + try + videoId = "jNQXAC9IVRw" + url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}` + response = HttpClient.Get(url).NoCache().Await() + + if not response.IsSuccess() + testNode.state = "failed" + testNode.details = response.ErrorMessage() + return + end if + + json = response.Json() + if json = invalid + testNode.state = "failed" + testNode.details = `Invalid json from ${url}` + return + end if + + videoThumbnails = json.videoThumbnails + if not IsArray(videoThumbnails) or videoThumbnails.Count() = 0 + testNode.state = "failed" + testNode.details = `No thumbnails found in json from ${url}` + return + end if + + contentNode = InvidiousContent.ToVideoContentNode(invalid, json, instance) + thumbnail = contentNode.thumbnail + if StringUtils.IsNullOrEmpty(thumbnail) + testNode.state = "failed" + testNode.details = `No thumbnail found in json from ${url}` + return + end if + + ' Note: we could do a HEAD request, but Invidious returns 200 even for invalid thumbnails + ' To make sure we don't get a 200 for an invalid thumbnail, we do a GET request instead + response = HttpClient.Get(thumbnail).NoCache().Await() + if not response.IsSuccess() + testNode.state = "failed" + testNode.details = `${thumbnail} is not reachable\n${response.ErrorMessage(false)}` + return + end if + + contentType = response.Headers()["Content-Type"] + if StringUtils.IsNullOrEmpty(contentType) or not contentType.StartsWith("image/") + testNode.state = "failed" + testNode.details = `Invalid thumbnail data ${thumbnail} (Content-Type is not image/*)` + return + end if + + testNode.state = "passed" + testNode.details = `The instance can fetch video thumbnail for id "${videoId}" (${thumbnail})` + catch e + testNode.state = "failed" + testNode.details = ErrorUtils.Format(e) + end try +end function diff --git a/playlet-lib/src/source/services/HttpClient.bs b/playlet-lib/src/source/services/HttpClient.bs index 7f004cca..5ec28d57 100644 --- a/playlet-lib/src/source/services/HttpClient.bs +++ b/playlet-lib/src/source/services/HttpClient.bs @@ -185,7 +185,12 @@ namespace HttpClient function ToCurlCommand() as string command = `curl "${m.BuildUrl()}"` - command += ` \\\n -X ${m._method}` + if m._method = "HEAD" + command += ` \\\n -I` + else if m._method <> "GET" + command += ` \\\n -X ${m._method}` + end if + command += ` \\\n --max-time ${m._timeoutSeconds}` command += ` \\\n --compressed` @@ -573,7 +578,7 @@ namespace HttpClient return m._headers end function - function ErrorMessage() as string + function ErrorMessage(includeBody = true as boolean) as string if m._errorMessage <> invalid return m._errorMessage end if @@ -588,7 +593,10 @@ namespace HttpClient else if m.IsCancelled() m._errorMessage = "Request cancelled." else if type(m.event) = "roUrlEvent" - m._errorMessage = `Reason :${m.event.GetFailureReason() }\nStatusCode: ${m.StatusCode()}\nRequestId: ${m.event.GetSourceIdentity()}\nBody: ${m.event.GetString()}` + m._errorMessage = `Reason :${m.event.GetFailureReason() }\nStatusCode: ${m.StatusCode()}\nRequestId: ${m.event.GetSourceIdentity()}` + if includeBody + m._errorMessage += `\nBody: ${m.event.GetString()}` + end if end if end if