Search in sources :

Example 1 with SyncEpisode

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

the class TraktTools method buildEpisodeList.

/**
     * @param episodesCursor Cursor of episodes sorted by season (ascending).
     * @param seasons Empty list.
     */
private static void buildEpisodeList(Cursor episodesCursor, List<SyncSeason> seasons) {
    SyncSeason currentSeason = null;
    while (episodesCursor.moveToNext()) {
        int season = episodesCursor.getInt(EpisodesQuery.SEASON);
        int episode = episodesCursor.getInt(EpisodesQuery.EPISODE);
        // create new season if none exists or number has changed
        if (currentSeason == null || currentSeason.number != season) {
            currentSeason = new SyncSeason().number(season);
            currentSeason.episodes = new LinkedList<>();
            seasons.add(currentSeason);
        }
        // add episode
        currentSeason.episodes.add(new SyncEpisode().number(episode));
    }
}
Also used : SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason)

Example 2 with SyncEpisode

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

the class RateEpisodeTask method buildTraktSyncItems.

@Nullable
@Override
protected SyncItems buildTraktSyncItems() {
    int season = -1;
    int episode = -1;
    int showTvdbId = -1;
    Cursor query = getContext().getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodeUri(episodeTvdbId), new String[] { SeriesGuideContract.Episodes.SEASON, SeriesGuideContract.Episodes.NUMBER, SeriesGuideContract.Shows.REF_SHOW_ID }, null, null, null);
    if (query != null) {
        if (query.moveToFirst()) {
            season = query.getInt(0);
            episode = query.getInt(1);
            showTvdbId = query.getInt(2);
        }
        query.close();
    }
    if (season == -1 || episode == -1 || showTvdbId == -1) {
        return null;
    }
    return new SyncItems().shows(new SyncShow().id(ShowIds.tvdb(showTvdbId)).seasons(new SyncSeason().number(season).episodes(new SyncEpisode().number(episode).rating(getRating()))));
}
Also used : SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncShow(com.uwetrottmann.trakt5.entities.SyncShow) Cursor(android.database.Cursor) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason) Nullable(android.support.annotation.Nullable)

Example 3 with SyncEpisode

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

the class TraktTools method processTraktEpisodes.

private boolean processTraktEpisodes(boolean isInitialSync, String seasonId, BaseSeason traktSeason, List<SyncSeason> syncSeasons, Flag flag) {
    HashSet<Integer> traktEpisodes = buildTraktEpisodesMap(traktSeason.episodes);
    Cursor localEpisodesQuery = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId), new String[] { SeriesGuideContract.Episodes._ID, SeriesGuideContract.Episodes.NUMBER, flag.databaseColumn }, null, null, null);
    if (localEpisodesQuery == null) {
        return false;
    }
    ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    List<SyncEpisode> syncEpisodes = new ArrayList<>();
    int episodesAddFlagCount = 0;
    int episodesRemoveFlagCount = 0;
    while (localEpisodesQuery.moveToNext()) {
        int episodeId = localEpisodesQuery.getInt(0);
        int episodeNumber = localEpisodesQuery.getInt(1);
        int flagValue = localEpisodesQuery.getInt(2);
        boolean isFlagged = flag == Flag.WATCHED ? EpisodeTools.isWatched(flagValue) : EpisodeTools.isCollected(flagValue);
        if (traktEpisodes.contains(episodeNumber)) {
            // episode watched/collected on trakt
            if (!isFlagged) {
                // set as watched/collected
                batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodeUri(episodeId)).withValue(flag.databaseColumn, flag.flaggedValue).build());
                episodesAddFlagCount++;
            }
        } else {
            // episode not watched/collected on trakt
            if (isFlagged) {
                if (isInitialSync) {
                    // upload to trakt
                    syncEpisodes.add(new SyncEpisode().number(episodeNumber));
                } else {
                    // set as not watched/collected if it is currently watched/collected
                    boolean isSkipped = flag == Flag.WATCHED && EpisodeTools.isSkipped(flagValue);
                    if (!isSkipped) {
                        batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodeUri(episodeId)).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
                        episodesRemoveFlagCount++;
                    }
                }
            }
        }
    }
    int localEpisodeCount = localEpisodesQuery.getCount();
    boolean addFlagToWholeSeason = episodesAddFlagCount == localEpisodeCount;
    boolean removeFlagFromWholeSeason = episodesRemoveFlagCount == localEpisodeCount;
    localEpisodesQuery.close();
    // if setting the whole season as (not) watched/collected, replace with single db op
    if (addFlagToWholeSeason || removeFlagFromWholeSeason) {
        batch.clear();
        batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId)).withValue(flag.databaseColumn, addFlagToWholeSeason ? flag.flaggedValue : flag.notFlaggedValue).build());
    }
    try {
        DBUtils.applyInSmallBatches(context, batch);
    } catch (OperationApplicationException e) {
        Timber.e(e, "Episodes watched/collected values database update failed.");
    }
    if (syncEpisodes.size() > 0) {
        syncSeasons.add(new SyncSeason().number(traktSeason.number).episodes(syncEpisodes));
    }
    return true;
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) OperationApplicationException(android.content.OperationApplicationException) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason)

Example 4 with SyncEpisode

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

the class TraktTools method buildSyncSeason.

/**
     * Returns a list of watched/collected episodes of a season. Packaged ready for upload to
     * trakt.
     */
private SyncSeason buildSyncSeason(String seasonTvdbId, int seasonNumber, Flag flag) {
    // query for watched/collected episodes of the given season
    Cursor flaggedEpisodesQuery = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonTvdbId), new String[] { SeriesGuideContract.Episodes.NUMBER }, flag.flagSelection, null, SeriesGuideContract.Episodes.SORT_NUMBER_ASC);
    if (flaggedEpisodesQuery == null) {
        // query failed
        return null;
    }
    List<SyncEpisode> syncEpisodes = new ArrayList<>();
    while (flaggedEpisodesQuery.moveToNext()) {
        int episodeNumber = flaggedEpisodesQuery.getInt(0);
        syncEpisodes.add(new SyncEpisode().number(episodeNumber));
    }
    flaggedEpisodesQuery.close();
    if (syncEpisodes.size() == 0) {
        // no episodes watched/collected
        return null;
    }
    return new SyncSeason().number(seasonNumber).episodes(syncEpisodes);
}
Also used : SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason)

Example 5 with SyncEpisode

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

the class TraktTask method doCheckInAction.

private TraktResponse doCheckInAction() {
    try {
        retrofit2.Response response;
        String message = mArgs.getString(InitBundle.MESSAGE);
        switch(mAction) {
            case CHECKIN_EPISODE:
                {
                    int episodeTvdbId = mArgs.getInt(InitBundle.EPISODE_TVDBID);
                    EpisodeCheckin checkin = new EpisodeCheckin.Builder(new SyncEpisode().id(EpisodeIds.tvdb(episodeTvdbId)), APP_VERSION, null).message(message).build();
                    response = traktCheckin.get().checkin(checkin).execute();
                    break;
                }
            case CHECKIN_MOVIE:
                {
                    int movieTmdbId = mArgs.getInt(InitBundle.MOVIE_TMDB_ID);
                    MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
                    response = traktCheckin.get().checkin(checkin).execute();
                    break;
                }
            default:
                throw new IllegalArgumentException("check-in action unknown.");
        }
        if (response.isSuccessful()) {
            return new TraktResponse(true, mContext.getString(R.string.checkin_success_trakt, mArgs.getString(InitBundle.TITLE)));
        } else {
            // check if the user wants to check-in, but there is already a check-in in progress
            CheckinError checkinError = trakt.get().checkForCheckinError(response);
            if (checkinError != null) {
                DateTime expiresAt = checkinError.expires_at;
                int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.getMillis() - System.currentTimeMillis()) / 1000);
                return new CheckinBlockedResponse(waitTimeMin);
            } else // check if item does not exist on trakt (yet)
            if (response.code() == 404) {
                return new TraktResponse(false, mContext.getString(R.string.trakt_error_not_exists));
            } else if (SgTrakt.isUnauthorized(mContext, response)) {
                return new TraktResponse(false, mContext.getString(R.string.trakt_error_credentials));
            } else {
                SgTrakt.trackFailedRequest(mContext, "check-in", response);
            }
        }
    } catch (IOException e) {
        SgTrakt.trackFailedRequest(mContext, "check-in", e);
    }
    // return generic failure message
    return buildErrorResponse();
}
Also used : CheckinError(com.uwetrottmann.trakt5.entities.CheckinError) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) EpisodeCheckin(com.uwetrottmann.trakt5.entities.EpisodeCheckin) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MovieCheckin(com.uwetrottmann.trakt5.entities.MovieCheckin) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie)

Aggregations

SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)5 SyncSeason (com.uwetrottmann.trakt5.entities.SyncSeason)4 Cursor (android.database.Cursor)3 ArrayList (java.util.ArrayList)2 ContentProviderOperation (android.content.ContentProviderOperation)1 OperationApplicationException (android.content.OperationApplicationException)1 Nullable (android.support.annotation.Nullable)1 CheckinError (com.uwetrottmann.trakt5.entities.CheckinError)1 EpisodeCheckin (com.uwetrottmann.trakt5.entities.EpisodeCheckin)1 MovieCheckin (com.uwetrottmann.trakt5.entities.MovieCheckin)1 SyncItems (com.uwetrottmann.trakt5.entities.SyncItems)1 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)1 SyncShow (com.uwetrottmann.trakt5.entities.SyncShow)1 IOException (java.io.IOException)1 DateTime (org.joda.time.DateTime)1