use of com.uwetrottmann.trakt5.entities.LastActivities in project SeriesGuide by UweTrottmann.
the class SgSyncAdapter method performTraktSync.
private UpdateResult performTraktSync(HashSet<Integer> localShows, long currentTime) {
if (!TraktCredentials.get(getContext()).hasCredentials()) {
Timber.d("performTraktSync: no auth, skip");
return UpdateResult.SUCCESS;
}
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
// get last activity timestamps
TraktTools traktTools = app.getTraktTools();
LastActivities lastActivity = traktTools.getLastActivity();
if (lastActivity == null) {
// trakt is likely offline or busy, try later
Timber.e("performTraktSync: last activity download failed");
return UpdateResult.INCOMPLETE;
}
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
if (localShows.size() == 0) {
Timber.d("performTraktSync: no local shows, skip shows");
} else {
// download and upload episode watched and collected flags
if (performTraktEpisodeSync(localShows, lastActivity.episodes, currentTime) != UpdateResult.SUCCESS) {
return UpdateResult.INCOMPLETE;
}
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
// download show ratings
if (traktTools.downloadShowRatings(lastActivity.shows.rated_at) != UpdateResult.SUCCESS) {
return UpdateResult.INCOMPLETE;
}
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
// download episode ratings
if (traktTools.downloadEpisodeRatings(lastActivity.episodes.rated_at) != UpdateResult.SUCCESS) {
return UpdateResult.INCOMPLETE;
}
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
}
// sync watchlist and collection with trakt
if (app.getMovieTools().syncMovieListsWithTrakt(lastActivity.movies) != UpdateResult.SUCCESS) {
return UpdateResult.INCOMPLETE;
}
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
// download watched movies
if (traktTools.downloadWatchedMovies(lastActivity.movies.watched_at) != UpdateResult.SUCCESS) {
return UpdateResult.INCOMPLETE;
}
// clean up any useless movies (not watched or not in any list)
MovieTools.deleteUnusedMovies(getContext());
if (!AndroidUtils.isNetworkConnected(getContext())) {
return UpdateResult.INCOMPLETE;
}
// download movie ratings
return traktTools.downloadMovieRatings(lastActivity.movies.rated_at);
}
use of com.uwetrottmann.trakt5.entities.LastActivities 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;
}
Aggregations