Search in sources :

Example 1 with TrendingShow

use of com.uwetrottmann.trakt5.entities.TrendingShow in project SeriesGuide by UweTrottmann.

the class TvdbAddLoader method loadInBackground.

@Override
public Result loadInBackground() {
    List<SearchResult> results;
    if (TextUtils.isEmpty(query)) {
        // no query? load a list of trending shows from trakt
        List<TrendingShow> trendingShows = SgTrakt.executeCall(app, traktShows.get().trending(1, 35, Extended.FULL), "get trending shows");
        if (trendingShows != null) {
            List<Show> shows = new LinkedList<>();
            for (TrendingShow show : trendingShows) {
                if (show.show == null || show.show.ids == null || show.show.ids.tvdb == null) {
                    // skip if required values are missing
                    continue;
                }
                shows.add(show.show);
            }
            // manually set the language to the current search language
            results = TraktAddLoader.parseTraktShowsToSearchResults(getContext(), shows, language);
            return buildResultSuccess(results, R.string.add_empty);
        } else {
            return buildResultFailure(R.string.trakt);
        }
    } else {
        // use TheTVDB search for all other (or any) languages
        if (DisplaySettings.LANGUAGE_EN.equals(language)) {
            List<com.uwetrottmann.trakt5.entities.SearchResult> searchResults = SgTrakt.executeCall(app, traktSearch.get().textQueryShow(query, null, null, null, null, null, null, null, null, null, Extended.FULL, 1, 30), "search shows");
            if (searchResults != null) {
                List<Show> shows = new LinkedList<>();
                for (com.uwetrottmann.trakt5.entities.SearchResult result : searchResults) {
                    if (result.show == null || result.show.ids == null || result.show.ids.tvdb == null) {
                        // skip, TVDB id required
                        continue;
                    }
                    shows.add(result.show);
                }
                // manually set the language to English
                results = TraktAddLoader.parseTraktShowsToSearchResults(getContext(), shows, DisplaySettings.LANGUAGE_EN);
                return buildResultSuccess(results, R.string.no_results);
            } else {
                return buildResultFailure(R.string.trakt);
            }
        } else {
            try {
                if (TextUtils.isEmpty(language)) {
                    // use the v1 API to do an any language search not supported by v2
                    results = TvdbTools.getInstance(app).searchShow(query, null);
                } else {
                    results = TvdbTools.getInstance(app).searchSeries(query, language);
                }
                markLocalShows(results);
                return buildResultSuccess(results, R.string.no_results);
            } catch (TvdbException e) {
                Timber.e(e, "Searching show failed");
            }
            return buildResultFailure(R.string.tvdb);
        }
    }
}
Also used : SearchResult(com.battlelancer.seriesguide.items.SearchResult) TrendingShow(com.uwetrottmann.trakt5.entities.TrendingShow) LinkedList(java.util.LinkedList) Show(com.uwetrottmann.trakt5.entities.Show) TrendingShow(com.uwetrottmann.trakt5.entities.TrendingShow) TvdbException(com.battlelancer.seriesguide.thetvdbapi.TvdbException)

Aggregations

SearchResult (com.battlelancer.seriesguide.items.SearchResult)1 TvdbException (com.battlelancer.seriesguide.thetvdbapi.TvdbException)1 Show (com.uwetrottmann.trakt5.entities.Show)1 TrendingShow (com.uwetrottmann.trakt5.entities.TrendingShow)1 LinkedList (java.util.LinkedList)1