search suggestions

This commit is contained in:
Brahim Hadriche committed 2024-12-04 20:07:06 -05:00
1 parent d3f443e304
commit 335761aa8e
3 files changed
+54 -3

No files matched your search

+6
View File
@@ -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
@@ -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
@@ -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