use of com.uwetrottmann.trakt5.entities.Episode in project SeriesGuide by UweTrottmann.
the class TraktTools method downloadEpisodeRatings.
/**
* Downloads trakt episode ratings and applies the latest ones to the database.
*
* <p> To apply all ratings, set {@link TraktSettings#KEY_LAST_EPISODES_RATED_AT} to 0.
*/
public UpdateResult downloadEpisodeRatings(@Nullable DateTime ratedAt) {
if (ratedAt == null) {
Timber.e("downloadEpisodeRatings: null rated_at");
return UpdateResult.INCOMPLETE;
}
long lastRatedAt = TraktSettings.getLastEpisodesRatedAt(context);
if (!ratedAt.isAfter(lastRatedAt)) {
// not initial sync, no ratings have changed
Timber.d("downloadEpisodeRatings: no changes since %tF %tT", lastRatedAt, lastRatedAt);
return UpdateResult.SUCCESS;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return UpdateResult.INCOMPLETE;
}
// download rated episodes
List<RatedEpisode> ratedEpisodes;
try {
Response<List<RatedEpisode>> response = traktSync.get().ratingsEpisodes(RatingsFilter.ALL, Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
ratedEpisodes = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get episode ratings", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get episode ratings", e);
return UpdateResult.INCOMPLETE;
}
if (ratedEpisodes == null) {
Timber.e("downloadEpisodeRatings: null response");
return UpdateResult.INCOMPLETE;
}
if (ratedEpisodes.isEmpty()) {
Timber.d("downloadEpisodeRatings: no ratings on trakt");
return UpdateResult.SUCCESS;
}
// trakt last activity rated_at timestamp is set after the rating timestamp
// so include ratings that are a little older
long ratedAtThreshold = lastRatedAt - 5 * DateUtils.MINUTE_IN_MILLIS;
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (RatedEpisode episode : ratedEpisodes) {
if (episode.rating == null || episode.episode == null || episode.episode.ids == null || episode.episode.ids.tvdb == null) {
// skip, can't handle
continue;
}
if (episode.rated_at != null && episode.rated_at.isBefore(ratedAtThreshold)) {
// no need to apply older ratings again
break;
}
// if an episode does not exist, this update will do nothing
ContentProviderOperation op = ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodeUri(episode.episode.ids.tvdb)).withValue(SeriesGuideContract.Episodes.RATING_USER, episode.rating.value).build();
batch.add(op);
}
// apply database updates
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "downloadEpisodeRatings: database update failed");
return UpdateResult.INCOMPLETE;
}
// save last rated instant
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_EPISODES_RATED_AT, ratedAt.getMillis()).commit();
Timber.d("downloadEpisodeRatings: success, last rated_at %tF %tT", ratedAt.getMillis(), ratedAt.getMillis());
return UpdateResult.SUCCESS;
}
use of com.uwetrottmann.trakt5.entities.Episode in project SeriesGuide by UweTrottmann.
the class TraktTask method buildComment.
@Nullable
private Comment buildComment() {
Comment comment = new Comment();
comment.comment = args.getString(InitBundle.MESSAGE);
comment.spoiler = args.getBoolean(InitBundle.ISSPOILER);
// episode?
long episodeId = args.getLong(InitBundle.EPISODE_ID);
if (episodeId != 0) {
// Check in using episode TMDB ID
// Note: using show Trakt ID and episode numbers does not work (comments on show).
int episodeTmdbIdOrZero = SgRoomDatabase.getInstance(context).sgEpisode2Helper().getEpisodeTmdbId(episodeId);
if (episodeTmdbIdOrZero == 0) {
Timber.e("Failed to get episode %d", episodeId);
return null;
}
comment.episode = new Episode();
comment.episode.ids = EpisodeIds.tmdb(episodeTmdbIdOrZero);
return comment;
}
// show?
long showId = args.getLong(InitBundle.SHOW_ID);
if (showId != 0) {
Integer showTraktId = ShowTools.getShowTraktId(context, showId);
if (showTraktId == null) {
Timber.e("Failed to get show %d", showId);
return null;
}
comment.show = new Show();
comment.show.ids = ShowIds.trakt(showTraktId);
return comment;
}
// movie!
int movieTmdbId = args.getInt(InitBundle.MOVIE_TMDB_ID);
comment.movie = new Movie();
comment.movie.ids = MovieIds.tmdb(movieTmdbId);
return comment;
}
use of com.uwetrottmann.trakt5.entities.Episode 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();
}
use of com.uwetrottmann.trakt5.entities.Episode in project SeriesGuide by UweTrottmann.
the class TraktSync method sync.
/**
* @param onlyRatings To not conflict with Hexagon sync, can turn on so only
* ratings are synced.
*/
public SgSyncAdapter.UpdateResult sync(long currentTime, boolean onlyRatings) {
// get last activity timestamps
progress.publish(SyncProgress.Step.TRAKT);
if (!AndroidUtils.isNetworkConnected(context)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
LastActivities lastActivity = getLastActivity();
if (lastActivity == null || lastActivity.episodes == null || lastActivity.shows == null || lastActivity.movies == null) {
// trakt is offline or busy, or there are server errors, try later.
progress.recordError();
Timber.e("performTraktSync: last activity download failed");
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
TraktRatingsSync ratingsSync = new TraktRatingsSync(context, traktSync);
Map<Integer, Long> tmdbIdsToShowIds = SgApp.getServicesComponent(context).showTools().getTmdbIdsToShowIds();
if (tmdbIdsToShowIds.size() == 0) {
Timber.d("performTraktSync: no local shows, skip shows");
} else {
if (!onlyRatings) {
// EPISODES
// download and upload episode watched and collected flags
progress.publish(SyncProgress.Step.TRAKT_EPISODES);
if (!AndroidUtils.isNetworkConnected(context)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
if (!syncEpisodes(tmdbIdsToShowIds, lastActivity.episodes, currentTime)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
}
// download ratings
progress.publish(SyncProgress.Step.TRAKT_RATINGS);
if (!AndroidUtils.isNetworkConnected(context)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
if (!ratingsSync.downloadForEpisodes(lastActivity.episodes.rated_at)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
// download ratings
if (!AndroidUtils.isNetworkConnected(context)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
if (!ratingsSync.downloadForShows(lastActivity.shows.rated_at)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
}
// MOVIES
progress.publish(SyncProgress.Step.TRAKT_MOVIES);
TraktMovieSync movieSync = new TraktMovieSync(context, movieTools, traktSync);
// sync watchlist, collection and watched movies with trakt
if (!onlyRatings) {
if (!AndroidUtils.isNetworkConnected(context)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
if (!movieSync.syncLists(lastActivity.movies)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
// clean up any useless movies (not watched or not in any list)
MovieTools.deleteUnusedMovies(context);
}
// download movie ratings
progress.publish(SyncProgress.Step.TRAKT_RATINGS);
if (!AndroidUtils.isNetworkConnected(context)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
if (!ratingsSync.downloadForMovies(lastActivity.movies.rated_at)) {
progress.recordError();
return SgSyncAdapter.UpdateResult.INCOMPLETE;
}
return SgSyncAdapter.UpdateResult.SUCCESS;
}
use of com.uwetrottmann.trakt5.entities.Episode in project SeriesGuide by UweTrottmann.
the class TraktRecentEpisodeHistoryLoader method addItems.
protected void addItems(List<NowAdapter.NowItem> items, List<HistoryEntry> history) {
SparseArrayCompat<String> tmdbIdsToPoster = SgApp.getServicesComponent(getContext()).showTools().getTmdbIdsToPoster();
SgEpisode2Helper episodeHelper = SgRoomDatabase.getInstance(getContext()).sgEpisode2Helper();
long timeDayAgo = System.currentTimeMillis() - DateUtils.DAY_IN_MILLIS;
for (int i = 0, size = history.size(); i < size; i++) {
HistoryEntry entry = history.get(i);
if (entry.episode == null || entry.show == null || entry.watched_at == null) {
// missing required values
continue;
}
// however, include at least one older episode if there are none, yet
if (TimeTools.isBeforeMillis(entry.watched_at, timeDayAgo) && items.size() > 1) {
break;
}
// look for a TVDB poster
String posterUrl;
Integer showTmdbId = entry.show.ids == null ? null : entry.show.ids.tmdb;
if (showTmdbId != null) {
// prefer poster of already added show, fall back to first uploaded poster
posterUrl = ImageTools.posterUrlOrResolve(tmdbIdsToPoster.get(showTmdbId), showTmdbId, DisplaySettings.LANGUAGE_EN, getContext());
} else {
posterUrl = null;
}
String description = (entry.episode.season == null || entry.episode.number == null) ? entry.episode.title : TextTools.getNextEpisodeString(getContext(), entry.episode.season, entry.episode.number, entry.episode.title);
Integer episodeTmdbIdOrNull = entry.episode.ids != null ? entry.episode.ids.tmdb : null;
long localEpisodeIdOrZero = episodeTmdbIdOrNull != null ? episodeHelper.getEpisodeIdByTmdbId(episodeTmdbIdOrNull) : 0;
NowAdapter.NowItem item = new NowAdapter.NowItem().displayData(entry.watched_at.toInstant().toEpochMilli(), entry.show.title, description, posterUrl).episodeIds(localEpisodeIdOrZero, showTmdbId != null ? showTmdbId : 0).recentlyWatchedTrakt(entry.action);
items.add(item);
}
}
Aggregations