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();
}
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()))));
}
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();
}
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);
}
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();
}
Aggregations