diff --git a/CHANGELOG.md b/CHANGELOG.md index 02fb5eb3..31e53253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing worth mentioning yet. +### Fixed + +- Try fetching multiple pages in the search, in case results are filtered out (e.g. shorts) ## [0.41.13] - 2025-09-18 diff --git a/playlet-lib/src/components/Services/Innertube/Parser.bs b/playlet-lib/src/components/Services/Innertube/Parser.bs index 1b681458..e25a70e8 100644 --- a/playlet-lib/src/components/Services/Innertube/Parser.bs +++ b/playlet-lib/src/components/Services/Innertube/Parser.bs @@ -32,9 +32,14 @@ namespace Innertube Innertube.ParseNode(containers, context) - if context.currentFeed <> invalid and (context.currentFeed.items.Count() > 0 or context.currentFeed.title <> "") - context.feeds.Push(context.currentFeed) - context.currentFeed = invalid + if context.currentFeed <> invalid + hasTitle = context.currentFeed.title <> "" + hasItems = context.currentFeed.items.Count() > 0 + hasContinuation = context.currentFeed.DoesExist("continuation") + if hasTitle or hasItems or hasContinuation + context.feeds.Push(context.currentFeed) + context.currentFeed = invalid + end if end if return context.feeds end function diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs index 60c7475b..1d9c31d2 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs @@ -420,31 +420,59 @@ namespace Http options.continuation = continuation end if - result = InnertubeService.Search(query, options) + fetchedPages = 0 + while true + ' Content can be filtered (for example, shorts removed) + ' so we could be fetching multiple pages. + if fetchedPages > 3 + response.Json({ + "error": "Failed to search: no results found" + }) + response.http_code = 500 + return true + end if + result = InnertubeService.Search(query, options) + fetchedPages += 1 - if raw - response.Json(result) - return true - end if + if raw + response.Json(result) + return true + end if - if result.error <> invalid - response.Json(result) - response.http_code = 500 - return true - end if + if result.error <> invalid + response.Json(result) + response.http_code = 500 + return true + end if - if not IsArray(result.feeds) or result.feeds.Count() = 0 - response.Json({ - "error": "Failed to search: no results found" - }) - response.http_code = 500 - return true - end if + if not IsArray(result.feeds) or result.feeds.Count() = 0 + response.Json({ + "error": "Failed to search: no results found" + }) + response.http_code = 500 + return true + end if - response.Json(result.feeds[0]) - if result.DoesExist("cache-control") - response.headers["cache-control"] = result["cache-control"] - end if + feed = result.feeds[0] + if feed.items.Count() > 0 + response.Json(feed) + if fetchedPages = 1 and result.DoesExist("cache-control") + response.headers["cache-control"] = result["cache-control"] + end if + return true + end if + + if StringUtils.IsNullOrEmpty(feed.continuation) + response.Json({ + "error": "Failed to search: no results found" + }) + response.http_code = 500 + return true + end if + + LogDebug("Search returned no results, fetching next page with continuation") + options.continuation = feed.continuation + end while return true end function