Skip to content

Commit

Permalink
search suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Dec 5, 2024
1 parent d3f443e commit 335761a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions playlet-lib/src/components/Services/Innertube/InnertubeService.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 335761a

Please sign in to comment.