Search in sources :

Example 1 with SearchResult

use of com.uwetrottmann.trakt5.entities.SearchResult 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)

Example 2 with SearchResult

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

the class TraktAddLoader method parseTraktShowsToSearchResults.

/**
     * Transforms a list of trakt shows to a list of {@link SearchResult}, marks shows already in
     * the local database as added.
     */
public static List<SearchResult> parseTraktShowsToSearchResults(Context context, @NonNull List<Show> traktShows, @Nullable String overrideLanguage) {
    List<SearchResult> results = new ArrayList<>();
    // build list
    SparseArrayCompat<String> existingPosterPaths = ShowTools.getShowTvdbIdsAndPosters(context);
    for (Show show : traktShows) {
        if (show.ids == null || show.ids.tvdb == null) {
            // has no TheTVDB id
            continue;
        }
        SearchResult result = new SearchResult();
        result.tvdbid = show.ids.tvdb;
        result.title = show.title;
        // search results return an overview, while trending and other lists do not
        result.overview = !TextUtils.isEmpty(show.overview) ? show.overview : show.year != null ? String.valueOf(show.year) : "";
        if (existingPosterPaths != null && existingPosterPaths.indexOfKey(show.ids.tvdb) >= 0) {
            // is already in local database
            result.state = SearchResult.STATE_ADDED;
            // use the poster we fetched for it (or null if there is none)
            result.posterPath = existingPosterPaths.get(show.ids.tvdb);
        }
        if (overrideLanguage != null) {
            result.language = overrideLanguage;
        }
        results.add(result);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) SearchResult(com.battlelancer.seriesguide.items.SearchResult) Show(com.uwetrottmann.trakt5.entities.Show) BaseShow(com.uwetrottmann.trakt5.entities.BaseShow)

Example 3 with SearchResult

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

the class AddShowTask method doInBackground.

@Override
protected Void doInBackground(Void... params) {
    Timber.d("Starting to add shows...");
    // don't even get started
    if (addQueue.isEmpty()) {
        Timber.d("Finished. Queue was empty.");
        return null;
    }
    // set values required for progress update
    SearchResult nextShow = addQueue.peek();
    currentShowName = nextShow.title;
    currentShowTvdbId = nextShow.tvdbid;
    if (!AndroidUtils.isNetworkConnected(app)) {
        Timber.d("Finished. No internet connection.");
        publishProgress(RESULT_OFFLINE);
        return null;
    }
    if (isCancelled()) {
        Timber.d("Finished. Cancelled.");
        return null;
    }
    // if not connected to Hexagon, get episodes from trakt
    HashMap<Integer, BaseShow> traktCollection = null;
    HashMap<Integer, BaseShow> traktWatched = null;
    if (!HexagonSettings.isEnabled(app) && TraktCredentials.get(app).hasCredentials()) {
        Timber.d("Getting watched and collected episodes from trakt.");
        // get collection
        HashMap<Integer, BaseShow> traktShows = getTraktShows("get collection", true);
        if (traktShows == null) {
            // can not get collected state from trakt, give up.
            return null;
        }
        traktCollection = traktShows;
        // get watched
        traktShows = getTraktShows("get watched", false);
        if (traktShows == null) {
            // can not get watched state from trakt, give up.
            return null;
        }
        traktWatched = traktShows;
    }
    int result;
    boolean addedAtLeastOneShow = false;
    boolean failedMergingShows = false;
    while (!addQueue.isEmpty()) {
        Timber.d("Starting to add next show...");
        if (isCancelled()) {
            Timber.d("Finished. Cancelled.");
            // table yet
            return null;
        }
        nextShow = addQueue.removeFirst();
        // set values required for progress update
        currentShowName = nextShow.title;
        currentShowTvdbId = nextShow.tvdbid;
        if (!AndroidUtils.isNetworkConnected(app)) {
            Timber.d("Finished. No connection.");
            publishProgress(RESULT_OFFLINE);
            failedMergingShows = true;
            break;
        }
        try {
            boolean addedShow = TvdbTools.getInstance(app).addShow(nextShow.tvdbid, nextShow.language, traktCollection, traktWatched);
            result = addedShow ? PROGRESS_SUCCESS : PROGRESS_EXISTS;
            addedAtLeastOneShow = addedShow || // do not overwrite previous success
            addedAtLeastOneShow;
        } catch (TvdbException e) {
            // because it does not exist (any longer)
            if (!(isMergingShows && e.itemDoesNotExist())) {
                failedMergingShows = true;
            }
            if (e.service() == TvdbException.Service.TVDB) {
                if (e.itemDoesNotExist()) {
                    result = PROGRESS_ERROR_TVDB_NOT_EXISTS;
                } else {
                    result = PROGRESS_ERROR_TVDB;
                }
            } else if (e.service() == TvdbException.Service.HEXAGON) {
                result = PROGRESS_ERROR_HEXAGON;
            } else if (e.service() == TvdbException.Service.DATA) {
                result = PROGRESS_ERROR_DATA;
            } else {
                result = PROGRESS_ERROR;
            }
            Timber.e(e, "Adding show failed");
        }
        publishProgress(result);
        Timber.d("Finished adding show. (Result code: %s)", result);
    }
    isFinishedAddingShows = true;
    // when merging shows down from Hexagon, set success flag
    if (isMergingShows && !failedMergingShows) {
        HexagonSettings.setHasMergedShows(app, true);
    }
    if (addedAtLeastOneShow) {
        // make sure the next sync will download all ratings
        PreferenceManager.getDefaultSharedPreferences(app).edit().putLong(TraktSettings.KEY_LAST_SHOWS_RATED_AT, 0).putLong(TraktSettings.KEY_LAST_EPISODES_RATED_AT, 0).apply();
        // renew FTS3 table
        Timber.d("Renewing search table.");
        DBUtils.rebuildFtsTable(app);
    }
    Timber.d("Finished adding shows.");
    return null;
}
Also used : BaseShow(com.uwetrottmann.trakt5.entities.BaseShow) SearchResult(com.battlelancer.seriesguide.items.SearchResult) TvdbException(com.battlelancer.seriesguide.thetvdbapi.TvdbException)

Example 4 with SearchResult

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

the class MovieTools method lookupTraktId.

/**
     * @return {@code null} if looking up the id failed, -1 if the movie was not found or the movie
     * id if it was found.
     */
@Nullable
public Integer lookupTraktId(int movieTmdbId) {
    try {
        Response<List<SearchResult>> response = traktSearch.get().idLookup(IdType.TMDB, String.valueOf(movieTmdbId), Type.MOVIE, null, 1, 1).execute();
        if (response.isSuccessful()) {
            List<SearchResult> results = response.body();
            if (results == null || results.size() != 1) {
                Timber.e("Finding trakt movie failed (no results)");
                return -1;
            }
            SearchResult result = results.get(0);
            if (result.movie != null && result.movie.ids != null) {
                return result.movie.ids.trakt;
            }
            Timber.e("Finding trakt movie failed (not in results)");
            return -1;
        } else {
            SgTrakt.trackFailedRequest(context, "movie trakt id lookup", response);
        }
    } catch (IOException e) {
        SgTrakt.trackFailedRequest(context, "movie trakt id lookup", e);
    }
    return null;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) MovieList(com.uwetrottmann.seriesguide.backend.movies.model.MovieList) SearchResult(com.uwetrottmann.trakt5.entities.SearchResult) IOException(java.io.IOException) Nullable(android.support.annotation.Nullable)

Aggregations

SearchResult (com.battlelancer.seriesguide.items.SearchResult)3 TvdbException (com.battlelancer.seriesguide.thetvdbapi.TvdbException)2 BaseShow (com.uwetrottmann.trakt5.entities.BaseShow)2 Show (com.uwetrottmann.trakt5.entities.Show)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Nullable (android.support.annotation.Nullable)1 MovieList (com.uwetrottmann.seriesguide.backend.movies.model.MovieList)1 SearchResult (com.uwetrottmann.trakt5.entities.SearchResult)1 TrendingShow (com.uwetrottmann.trakt5.entities.TrendingShow)1 IOException (java.io.IOException)1 List (java.util.List)1