use of com.uwetrottmann.trakt5.entities.BaseMovie in project SeriesGuide by UweTrottmann.
the class TraktTools method downloadWatchedMovies.
/**
* Downloads trakt movie watched flags and mirrors them in the local database. Does NOT upload
* any flags (e.g. trakt is considered the truth).
*/
public UpdateResult downloadWatchedMovies(DateTime watchedAt) {
if (watchedAt == null) {
Timber.e("downloadWatchedMovies: null watched_at");
return UpdateResult.INCOMPLETE;
}
long lastWatchedAt = TraktSettings.getLastMoviesWatchedAt(context);
if (!watchedAt.isAfter(lastWatchedAt)) {
// not initial sync, no watched flags have changed
Timber.d("downloadWatchedMovies: no changes since %tF %tT", lastWatchedAt, lastWatchedAt);
return UpdateResult.SUCCESS;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return UpdateResult.INCOMPLETE;
}
// download watched movies
List<BaseMovie> watchedMovies;
try {
Response<List<BaseMovie>> response = traktSync.get().watchedMovies(Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
watchedMovies = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get watched movies", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get watched movies", e);
return UpdateResult.INCOMPLETE;
}
if (watchedMovies == null) {
Timber.e("downloadWatchedMovies: null response");
return UpdateResult.INCOMPLETE;
}
if (watchedMovies.isEmpty()) {
Timber.d("downloadWatchedMovies: no watched movies on trakt");
return UpdateResult.SUCCESS;
}
// apply watched flags for all watched trakt movies that are in the local database
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
Set<Integer> localMovies = MovieTools.getMovieTmdbIdsAsSet(context);
if (localMovies == null) {
return UpdateResult.INCOMPLETE;
}
Set<Integer> unwatchedMovies = new HashSet<>(localMovies);
for (BaseMovie movie : watchedMovies) {
if (movie.movie == null || movie.movie.ids == null || movie.movie.ids.tmdb == null) {
// required values are missing
continue;
}
if (!localMovies.contains(movie.movie.ids.tmdb)) {
// movie NOT in local database
// add a shell entry for storing watched state
batch.add(ContentProviderOperation.newInsert(SeriesGuideContract.Movies.CONTENT_URI).withValue(SeriesGuideContract.Movies.TMDB_ID, movie.movie.ids.tmdb).withValue(SeriesGuideContract.Movies.WATCHED, true).withValue(SeriesGuideContract.Movies.IN_COLLECTION, false).withValue(SeriesGuideContract.Movies.IN_WATCHLIST, false).build());
} else {
// movie IN local database
// set movie watched
batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Movies.buildMovieUri(movie.movie.ids.tmdb)).withValue(SeriesGuideContract.Movies.WATCHED, true).build());
unwatchedMovies.remove(movie.movie.ids.tmdb);
}
}
// remove watched flags from all remaining local movies
for (Integer tmdbId : unwatchedMovies) {
batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Movies.buildMovieUri(tmdbId)).withValue(SeriesGuideContract.Movies.WATCHED, false).build());
}
// apply database updates
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "downloadWatchedMovies: updating watched flags failed");
return UpdateResult.INCOMPLETE;
}
// save last watched instant
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_MOVIES_WATCHED_AT, watchedAt.getMillis()).commit();
Timber.d("downloadWatchedMovies: success, last watched_at %tF %tT", watchedAt.getMillis(), watchedAt.getMillis());
return UpdateResult.SUCCESS;
}
Aggregations