Search in sources :

Example 21 with Show

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

the class TraktTools method processTraktShows.

private int processTraktShows(@NonNull List<BaseShow> remoteShows, @NonNull HashSet<Integer> localShows, boolean isInitialSync, Flag flag) {
    HashMap<Integer, BaseShow> traktShows = buildTraktShowsMap(remoteShows);
    int uploadedShowsCount = 0;
    final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    for (Integer localShow : localShows) {
        if (traktShows.containsKey(localShow)) {
            // show watched/collected on trakt
            BaseShow traktShow = traktShows.get(localShow);
            int result = processTraktSeasons(isInitialSync, localShow, traktShow, flag);
            if (result < SUCCESS) {
                // processing seasons failed, give up.
                return result;
            }
            if (flag == Flag.WATCHED) {
                updateLastWatchedTime(localShow, traktShow, batch);
            }
        } else {
            // show not watched/collected on trakt
            // check if this is because the show can not be tracked with trakt (yet)
            // some shows only exist on TheTVDB, keep state local and maybe upload in the future
            Integer showTraktId = ShowTools.getShowTraktId(context, localShow);
            if (showTraktId != null) {
                if (isInitialSync) {
                    // upload all watched/collected episodes of the show
                    // do in between processing to stretch uploads over longer time periods
                    uploadEpisodes(localShow, showTraktId, flag);
                    uploadedShowsCount++;
                } else {
                    // set all watched/collected episodes of show not watched/collected
                    batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfShowUri(localShow)).withSelection(flag.clearFlagSelection, null).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
                }
            }
        }
    }
    try {
        DBUtils.applyInSmallBatches(context, batch);
    } catch (OperationApplicationException e) {
        Timber.e(e, "processTraktShows: failed to remove flag for %s.", flag.name);
    }
    if (uploadedShowsCount > 0) {
        Timber.d("processTraktShows: uploaded %s flags for %s complete shows.", flag.name, localShows.size());
    }
    return SUCCESS;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) BaseShow(com.uwetrottmann.trakt5.entities.BaseShow) ArrayList(java.util.ArrayList) OperationApplicationException(android.content.OperationApplicationException)

Example 22 with Show

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

the class BaseRateItemTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    if (isSendingToTrakt()) {
        if (!TraktCredentials.get(getContext()).hasCredentials()) {
            return ERROR_TRAKT_AUTH;
        }
        SyncItems ratedItems = buildTraktSyncItems();
        if (ratedItems == null) {
            return ERROR_TRAKT_API;
        }
        SyncErrors notFound;
        try {
            Response<SyncResponse> response = traktSync.get().addRatings(ratedItems).execute();
            if (response.isSuccessful()) {
                notFound = response.body().not_found;
            } else {
                if (SgTrakt.isUnauthorized(getContext(), response)) {
                    return ERROR_TRAKT_AUTH;
                }
                SgTrakt.trackFailedRequest(getContext(), getTraktAction(), response);
                return ERROR_TRAKT_API;
            }
        } catch (IOException e) {
            SgTrakt.trackFailedRequest(getContext(), "rate movie", e);
            return ERROR_TRAKT_API;
        }
        if (notFound != null) {
            if ((notFound.movies != null && notFound.movies.size() != 0) || (notFound.shows != null && notFound.shows.size() != 0) || (notFound.episodes != null && notFound.episodes.size() != 0)) {
                // movie, show or episode not found on trakt
                return ERROR_TRAKT_API_NOT_FOUND;
            }
        }
    }
    if (!doDatabaseUpdate()) {
        return ERROR_DATABASE;
    }
    return SUCCESS;
}
Also used : SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncResponse(com.uwetrottmann.trakt5.entities.SyncResponse) SyncErrors(com.uwetrottmann.trakt5.entities.SyncErrors) IOException(java.io.IOException)

Aggregations

BaseShow (com.uwetrottmann.trakt5.entities.BaseShow)8 ArrayList (java.util.ArrayList)7 ContentProviderOperation (android.content.ContentProviderOperation)5 Show (com.battlelancer.seriesguide.dataliberation.model.Show)5 NonNull (android.support.annotation.NonNull)4 HistoryEntry (com.uwetrottmann.trakt5.entities.HistoryEntry)4 IOException (java.io.IOException)4 OperationApplicationException (android.content.OperationApplicationException)3 Cursor (android.database.Cursor)3 SearchResult (com.battlelancer.seriesguide.items.SearchResult)3 Show (com.uwetrottmann.trakt5.entities.Show)3 LinkedList (java.util.LinkedList)3 NowAdapter (com.battlelancer.seriesguide.adapters.NowAdapter)2 TvdbException (com.battlelancer.seriesguide.thetvdbapi.TvdbException)2 TraktTools (com.battlelancer.seriesguide.util.TraktTools)2 Ratings (com.uwetrottmann.trakt5.entities.Ratings)2 SyncItems (com.uwetrottmann.trakt5.entities.SyncItems)2 SyncResponse (com.uwetrottmann.trakt5.entities.SyncResponse)2 SyncSeason (com.uwetrottmann.trakt5.entities.SyncSeason)2 ContentValues (android.content.ContentValues)1