use of fredboat.definitions.SearchProvider in project FredBoat by Frederikam.
the class TrackSearcher method searchForTracks.
/**
* @param query The search term
* @param cacheMaxAge Age of acceptable results from cache.
* @param timeoutMillis How long to wait for each lavaplayer search to answer
* @param providers Providers that shall be used for the search. They will be used in the order they are provided, the
* result of the first successful one will be returned
* @return The result of the search, or an empty list.
* @throws SearchingException If none of the search providers could give us a result, and there was at least one SearchingException thrown by them
*/
public AudioPlaylist searchForTracks(String query, long cacheMaxAge, int timeoutMillis, List<SearchProvider> providers) throws SearchingException {
Metrics.searchRequests.inc();
List<SearchProvider> provs = new ArrayList<>();
if (providers == null || providers.isEmpty()) {
log.warn("No search provider provided, defaulting to youtube -> soundcloud.");
provs.add(SearchProvider.YOUTUBE);
provs.add(SearchProvider.SOUNDCLOUD);
} else {
provs.addAll(providers);
}
SearchingException searchingException = null;
for (SearchProvider provider : provs) {
// 1. cache
AudioPlaylist cacheResult = fromCache(provider, query, cacheMaxAge);
if (cacheResult != null && !cacheResult.getTracks().isEmpty()) {
log.debug("Loaded search result {} {} from cache", provider, query);
Metrics.searchHits.labels("cache").inc();
return cacheResult;
}
// 2. lavaplayer todo break up this beautiful construction of ifs and exception handling in a better readable one?
if (provider != SearchProvider.YOUTUBE || System.currentTimeMillis() > youtubeCooldownUntil) {
try {
AudioPlaylist lavaplayerResult = new SearchResultHandler().searchSync(audioPlayerManager, provider, query, timeoutMillis);
if (!lavaplayerResult.getTracks().isEmpty()) {
log.debug("Loaded search result {} {} from lavaplayer", provider, query);
// got a search result? cache and return it
executor.execute(() -> searchResultService.mergeSearchResult(new SearchResult(audioPlayerManager, provider, query, lavaplayerResult)));
Metrics.searchHits.labels("lavaplayer-" + provider.name().toLowerCase()).inc();
return lavaplayerResult;
}
} catch (Http503Exception e) {
if (provider == SearchProvider.YOUTUBE) {
log.warn("Got a 503 from Youtube. Not hitting it with searches it for {} minutes", TimeUnit.MILLISECONDS.toMinutes(DEFAULT_YOUTUBE_COOLDOWN));
youtubeCooldownUntil = System.currentTimeMillis() + DEFAULT_YOUTUBE_COOLDOWN;
}
searchingException = e;
} catch (SearchingException e) {
searchingException = e;
}
}
// 3. optional: youtube api
if (provider == SearchProvider.YOUTUBE && (appConfig.isPatronDistribution() || appConfig.isDevDistribution())) {
try {
AudioPlaylist youtubeApiResult = youtubeAPI.search(query, MAX_RESULTS, audioPlayerManager.source(YoutubeAudioSourceManager.class));
if (!youtubeApiResult.getTracks().isEmpty()) {
log.debug("Loaded search result {} {} from Youtube API", provider, query);
// got a search result? cache and return it
executor.execute(() -> searchResultService.mergeSearchResult(new SearchResult(audioPlayerManager, provider, query, youtubeApiResult)));
Metrics.searchHits.labels("youtube-api").inc();
return youtubeApiResult;
}
} catch (SearchingException e) {
searchingException = e;
}
}
}
// did we run into searching exceptions that made us end up here?
if (searchingException != null) {
Metrics.searchHits.labels("exception").inc();
throw searchingException;
}
// no result with any of the search providers
Metrics.searchHits.labels("empty").inc();
return new BasicAudioPlaylist("Search result for: " + query, Collections.emptyList(), null, true);
}
Aggregations