diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c49a324..635bd886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Search suggestions to Playlet backend + ## [0.32.1] - 2024-12-03 ### Fixed diff --git a/playlet-lib/src/components/Screens/SearchScreen/SearchSuggestionsTask.bs b/playlet-lib/src/components/Screens/SearchScreen/SearchSuggestionsTask.bs index 1909cf7c..33fa0b8c 100644 --- a/playlet-lib/src/components/Screens/SearchScreen/SearchSuggestionsTask.bs +++ b/playlet-lib/src/components/Screens/SearchScreen/SearchSuggestionsTask.bs @@ -1,3 +1,4 @@ +import "pkg:/components/Services/Innertube/InnertubeService.bs" import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/SearchHistory/SearchHistoryUtils.bs" @@ -7,13 +8,17 @@ function SearchSuggestionsTask(input as object) as object invidiousNode = input.invidious preferencesNode = input.preferences - service = new Invidious.InvidiousService(invidiousNode) - if m.top.cancel return invalid end if - searchSuggestsions = StringUtils.IsNullOrEmpty(q) ? invalid : service.SearchSuggestions(q, m.top.cancellation) + backend = preferencesNode["playback.backend"] + if backend = "playlet" + searchSuggestsions = StringUtils.IsNullOrEmpty(q) ? invalid : InnertubeService.GetSearchSuggestions(q, m.top.cancellation) + else + service = new Invidious.InvidiousService(invidiousNode) + searchSuggestsions = StringUtils.IsNullOrEmpty(q) ? invalid : service.SearchSuggestions(q, m.top.cancellation) + end if if m.top.cancel return invalid diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index 66ca1398..4e68b57e 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -465,4 +465,44 @@ namespace InnertubeService return parsedResponse end function + function GetSearchSuggestions(q as string, cancellation = invalid as object) as object + request = HttpClient.Get("https://suggestqueries.google.com/complete/search?hl=en&gl=US&ds=yt&client=youtube&xssi=t&oe=UTF&q=" + q.EncodeUriComponent()) + request.Headers({ + "user-agent": INNERTUBE_WEB_USER_AGENT + }) + request.CacheSeconds(60 * 60 * 24) + request.Cancellation(cancellation) + + response = request.Await() + if not response.IsSuccess() + return { + suggestions: [] + } + end if + + text = response.Text() + text = text.Replace(`)]}'`, "") + + parsed = ParseJson(text) + if not IsArray(parsed) or parsed.Count() < 2 + return { + suggestions: [] + } + end if + parsed = parsed[1] + + suggestions = [] + for each suggestion in parsed + s = suggestion[0] + if not IsString(s) + continue for + end if + + suggestions.Push(s) + end for + + return { + suggestions: suggestions + } + end function end namespace