* Caching and refactor * Fix and tweak some parsing * Missing imports * refactor
58 lines
1.6 KiB
Plaintext
58 lines
1.6 KiB
Plaintext
import "pkg:/components/Services/Innertube/InnertubeService.bs"
|
|
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
|
import "pkg:/components/Services/SearchHistory/SearchHistoryUtils.bs"
|
|
|
|
@asynctask
|
|
function SearchSuggestionsTask(input as object) as object
|
|
q = input.q
|
|
invidiousNode = input.invidious
|
|
preferencesNode = input.preferences
|
|
|
|
if m.top.cancel
|
|
return invalid
|
|
end if
|
|
|
|
if StringUtils.IsNullOrEmpty(q)
|
|
searchSuggestsions = {
|
|
suggestions: []
|
|
}
|
|
else
|
|
backend = preferencesNode["playback.backend"]
|
|
if backend = "playlet"
|
|
searchSuggestsions = InnertubeService.SearchSuggestions(q, m.top.cancellation)
|
|
else
|
|
service = new Invidious.InvidiousService(invidiousNode)
|
|
searchSuggestsions = service.SearchSuggestions(q, m.top.cancellation)
|
|
end if
|
|
end if
|
|
|
|
if m.top.cancel
|
|
return invalid
|
|
end if
|
|
|
|
history = SearchHistoryUtils.GetSaved(preferencesNode, q)
|
|
|
|
if searchSuggestsions <> invalid
|
|
if searchSuggestsions.error <> invalid
|
|
LogError(searchSuggestsions.error)
|
|
else
|
|
history = MergeSearchSuggestions(history, searchSuggestsions.suggestions)
|
|
end if
|
|
end if
|
|
|
|
return {
|
|
q: q
|
|
response: history
|
|
}
|
|
end function
|
|
|
|
function MergeSearchSuggestions(history as object, queryResults as object, maxItems = 10 as integer) as object
|
|
if queryResults = invalid
|
|
return history
|
|
end if
|
|
while history.Count() < maxItems and queryResults.Count() > 0
|
|
history.push(queryResults.shift())
|
|
end while
|
|
return history
|
|
end function
|