Search in sources :

Example 1 with SgEpisode2Numbers

use of com.battlelancer.seriesguide.provider.SgEpisode2Numbers in project SeriesGuide by UweTrottmann.

the class TraktCommentsLoader method loadInBackground.

@Override
public Result loadInBackground() {
    // movie comments?
    int movieTmdbId = args.getInt(InitBundle.MOVIE_TMDB_ID);
    if (movieTmdbId != 0) {
        Integer movieTraktId = movieTools.lookupTraktId(movieTmdbId);
        if (movieTraktId != null) {
            if (movieTraktId == -1) {
                return buildResultFailure(R.string.trakt_error_not_exists);
            }
            try {
                Response<List<Comment>> response = traktMovies.get().comments(String.valueOf(movieTraktId), 1, PAGE_SIZE, Extended.FULL).execute();
                if (response.isSuccessful()) {
                    return buildResultSuccess(response.body());
                } else {
                    Errors.logAndReport("get movie comments", response);
                }
            } catch (Exception e) {
                Errors.logAndReport("get movie comments", e);
            }
        }
        return buildResultFailureWithOfflineCheck();
    }
    // episode comments?
    long episodeId = args.getLong(InitBundle.EPISODE_ID);
    if (episodeId != 0) {
        // look up episode number, season and show id
        SgRoomDatabase database = SgRoomDatabase.getInstance(getContext());
        SgEpisode2Numbers episode = database.sgEpisode2Helper().getEpisodeNumbers(episodeId);
        if (episode == null) {
            Timber.e("Failed to get episode %d", episodeId);
            return buildResultFailure(R.string.unknown);
        }
        // look up show trakt id
        Integer showTraktId = ShowTools.getShowTraktId(getContext(), episode.getShowId());
        if (showTraktId == null) {
            Timber.e("Failed to get show %d", episode.getShowId());
            return buildResultFailure(R.string.trakt_error_not_exists);
        }
        try {
            Response<List<Comment>> response = traktEpisodes.get().comments(String.valueOf(showTraktId), episode.getSeason(), episode.getEpisodenumber(), 1, PAGE_SIZE, Extended.FULL).execute();
            if (response.isSuccessful()) {
                return buildResultSuccess(response.body());
            } else {
                Errors.logAndReport("get episode comments", response);
            }
        } catch (Exception e) {
            Errors.logAndReport("get episode comments", e);
        }
        return buildResultFailureWithOfflineCheck();
    }
    // show comments!
    long showId = args.getLong(InitBundle.SHOW_ID);
    Integer showTraktId = ShowTools.getShowTraktId(getContext(), showId);
    if (showTraktId == null) {
        return buildResultFailure(R.string.trakt_error_not_exists);
    }
    try {
        Response<List<Comment>> response = traktShows.get().comments(String.valueOf(showTraktId), 1, PAGE_SIZE, Extended.FULL).execute();
        if (response.isSuccessful()) {
            return buildResultSuccess(response.body());
        } else {
            Errors.logAndReport("get show comments", response);
        }
    } catch (Exception e) {
        Errors.logAndReport("get show comments", e);
    }
    return buildResultFailureWithOfflineCheck();
}
Also used : SgRoomDatabase(com.battlelancer.seriesguide.provider.SgRoomDatabase) SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) List(java.util.List)

Example 2 with SgEpisode2Numbers

use of com.battlelancer.seriesguide.provider.SgEpisode2Numbers in project SeriesGuide by UweTrottmann.

the class RateEpisodeTask method buildTraktSyncItems.

@Nullable
@Override
protected SyncItems buildTraktSyncItems() {
    SgRoomDatabase database = SgRoomDatabase.getInstance(getContext());
    SgEpisode2Numbers episode = database.sgEpisode2Helper().getEpisodeNumbers(episodeId);
    if (episode == null)
        return null;
    int showTmdbId = database.sgShow2Helper().getShowTmdbId(episode.getShowId());
    if (showTmdbId == 0)
        return null;
    return new SyncItems().shows(new SyncShow().id(ShowIds.tmdb(showTmdbId)).seasons(new SyncSeason().number(episode.getSeason()).episodes(new SyncEpisode().number(episode.getEpisodenumber()).rating(getRating()))));
}
Also used : SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SgRoomDatabase(com.battlelancer.seriesguide.provider.SgRoomDatabase) SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) SyncShow(com.uwetrottmann.trakt5.entities.SyncShow) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason) Nullable(androidx.annotation.Nullable)

Example 3 with SgEpisode2Numbers

use of com.battlelancer.seriesguide.provider.SgEpisode2Numbers in project SeriesGuide by UweTrottmann.

the class BaseEpisodesJob method prepareNetworkJob.

@Nullable
private byte[] prepareNetworkJob(Context context) {
    // store affected episodes for network part
    List<SgEpisode2Numbers> episodes = getEpisodesForNetworkJob(context);
    FlatBufferBuilder builder = new FlatBufferBuilder(0);
    int[] episodeInfos = new int[episodes.size()];
    for (int i = 0; i < episodes.size(); i++) {
        SgEpisode2Numbers episode = episodes.get(i);
        int plays = getPlaysForNetworkJob(episode.getPlays());
        episodeInfos[i] = EpisodeInfo.createEpisodeInfo(builder, episode.getSeason(), episode.getEpisodenumber(), plays);
    }
    int episodesVector = SgJobInfo.createEpisodesVector(builder, episodeInfos);
    int jobInfo = SgJobInfo.createSgJobInfo(builder, flagValue, episodesVector, 0, 0, getShowId());
    builder.finish(jobInfo);
    return builder.sizedByteArray();
}
Also used : SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) Nullable(androidx.annotation.Nullable)

Example 4 with SgEpisode2Numbers

use of com.battlelancer.seriesguide.provider.SgEpisode2Numbers in project SeriesGuide by UweTrottmann.

the class EpisodeBaseJob method applyLocalChanges.

@Override
public boolean applyLocalChanges(Context context, boolean requiresNetworkJob) {
    // Gather data needed for later steps.
    SgEpisode2Helper helper = SgRoomDatabase.getInstance(context).sgEpisode2Helper();
    SgEpisode2Numbers episode = helper.getEpisodeNumbers(episodeId);
    if (episode == null) {
        return false;
    }
    this.episode = episode;
    return super.applyLocalChanges(context, requiresNetworkJob);
}
Also used : SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) SgEpisode2Helper(com.battlelancer.seriesguide.provider.SgEpisode2Helper)

Example 5 with SgEpisode2Numbers

use of com.battlelancer.seriesguide.provider.SgEpisode2Numbers in project SeriesGuide by UweTrottmann.

the class TraktTask method doCheckInAction.

private TraktResponse doCheckInAction() {
    TraktV2 trakt = SgApp.getServicesComponent(context).trakt();
    try {
        retrofit2.Response response;
        String message = args.getString(InitBundle.MESSAGE);
        switch(action) {
            case CHECKIN_EPISODE:
                {
                    // Check in using show Trakt ID
                    // and season and episode number (likely most reliable).
                    long episodeId = args.getLong(InitBundle.EPISODE_ID);
                    SgRoomDatabase database = SgRoomDatabase.getInstance(context);
                    SgEpisode2Numbers episode = database.sgEpisode2Helper().getEpisodeNumbers(episodeId);
                    if (episode == null) {
                        Timber.e("Failed to get episode %d", episodeId);
                        return buildErrorResponse();
                    }
                    Integer showTraktId = ShowTools.getShowTraktId(context, episode.getShowId());
                    if (showTraktId == null) {
                        Timber.e("Failed to get show %d", episode.getShowId());
                        return buildErrorResponse();
                    }
                    SyncEpisode traktEpisode = new SyncEpisode().season(episode.getSeason()).number(episode.getEpisodenumber());
                    Show traktShow = new Show();
                    traktShow.ids = ShowIds.trakt(showTraktId);
                    EpisodeCheckin checkin = new EpisodeCheckin.Builder(traktEpisode, APP_VERSION, null).show(traktShow).message(message).build();
                    response = trakt.checkin().checkin(checkin).execute();
                    break;
                }
            case CHECKIN_MOVIE:
                {
                    int movieTmdbId = args.getInt(InitBundle.MOVIE_TMDB_ID);
                    MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
                    response = trakt.checkin().checkin(checkin).execute();
                    break;
                }
            default:
                throw new IllegalArgumentException("check-in action unknown.");
        }
        if (response.isSuccessful()) {
            return new TraktResponse(true, context.getString(R.string.checkin_success_trakt, args.getString(InitBundle.TITLE)));
        } else {
            // check if the user wants to check-in, but there is already a check-in in progress
            CheckinError checkinError = trakt.checkForCheckinError(response);
            if (checkinError != null) {
                OffsetDateTime expiresAt = checkinError.expires_at;
                int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.toInstant().toEpochMilli() - 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, context.getString(R.string.trakt_error_not_exists));
            } else if (SgTrakt.isUnauthorized(context, response)) {
                return new TraktResponse(false, context.getString(R.string.trakt_error_credentials));
            } else {
                Errors.logAndReport("check-in", response);
            }
        }
    } catch (Exception e) {
        Errors.logAndReport("check-in", e);
    }
    // return generic failure message
    return buildErrorResponse();
}
Also used : CheckinError(com.uwetrottmann.trakt5.entities.CheckinError) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SgRoomDatabase(com.battlelancer.seriesguide.provider.SgRoomDatabase) EpisodeCheckin(com.uwetrottmann.trakt5.entities.EpisodeCheckin) SuppressLint(android.annotation.SuppressLint) MovieCheckin(com.uwetrottmann.trakt5.entities.MovieCheckin) TraktV2(com.uwetrottmann.trakt5.TraktV2) OffsetDateTime(org.threeten.bp.OffsetDateTime) SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) Show(com.uwetrottmann.trakt5.entities.Show)

Aggregations

SgEpisode2Numbers (com.battlelancer.seriesguide.provider.SgEpisode2Numbers)5 SgRoomDatabase (com.battlelancer.seriesguide.provider.SgRoomDatabase)3 Nullable (androidx.annotation.Nullable)2 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)2 SuppressLint (android.annotation.SuppressLint)1 SgEpisode2Helper (com.battlelancer.seriesguide.provider.SgEpisode2Helper)1 FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)1 TraktV2 (com.uwetrottmann.trakt5.TraktV2)1 CheckinError (com.uwetrottmann.trakt5.entities.CheckinError)1 EpisodeCheckin (com.uwetrottmann.trakt5.entities.EpisodeCheckin)1 MovieCheckin (com.uwetrottmann.trakt5.entities.MovieCheckin)1 Show (com.uwetrottmann.trakt5.entities.Show)1 SyncItems (com.uwetrottmann.trakt5.entities.SyncItems)1 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)1 SyncSeason (com.uwetrottmann.trakt5.entities.SyncSeason)1 SyncShow (com.uwetrottmann.trakt5.entities.SyncShow)1 List (java.util.List)1 OffsetDateTime (org.threeten.bp.OffsetDateTime)1