use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class TraktTools method downloadShowRatings.
/**
* Downloads trakt show ratings and applies the latest ones to the database.
*
* <p> To apply all ratings, set {@link TraktSettings#KEY_LAST_SHOWS_RATED_AT} to 0.
*/
public UpdateResult downloadShowRatings(@Nullable DateTime ratedAt) {
if (ratedAt == null) {
Timber.e("downloadShowRatings: null rated_at");
return UpdateResult.INCOMPLETE;
}
long lastRatedAt = TraktSettings.getLastShowsRatedAt(context);
if (!ratedAt.isAfter(lastRatedAt)) {
// not initial sync, no ratings have changed
Timber.d("downloadShowRatings: no changes since %tF %tT", lastRatedAt, lastRatedAt);
return UpdateResult.SUCCESS;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return UpdateResult.INCOMPLETE;
}
// download rated shows
List<RatedShow> ratedShows;
try {
Response<List<RatedShow>> response = traktSync.get().ratingsShows(RatingsFilter.ALL, Extended.DEFAULT_MIN).execute();
if (response.isSuccessful()) {
ratedShows = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return UpdateResult.INCOMPLETE;
}
SgTrakt.trackFailedRequest(context, "get show ratings", response);
return UpdateResult.INCOMPLETE;
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "get show ratings", e);
return UpdateResult.INCOMPLETE;
}
if (ratedShows == null) {
Timber.e("downloadShowRatings: null response");
return UpdateResult.INCOMPLETE;
}
if (ratedShows.isEmpty()) {
Timber.d("downloadShowRatings: 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;
// go through ratings, latest first (trakt sends in that order)
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
for (RatedShow show : ratedShows) {
if (show.rating == null || show.show == null || show.show.ids == null || show.show.ids.tvdb == null) {
// skip, can't handle
continue;
}
if (show.rated_at != null && show.rated_at.isBefore(ratedAtThreshold)) {
// no need to apply older ratings again
break;
}
// if a show does not exist, this update will do nothing
ContentProviderOperation op = ContentProviderOperation.newUpdate(SeriesGuideContract.Shows.buildShowUri(show.show.ids.tvdb)).withValue(SeriesGuideContract.Shows.RATING_USER, show.rating.value).build();
batch.add(op);
}
// apply database updates
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "downloadShowRatings: database update failed");
return UpdateResult.INCOMPLETE;
}
// save last rated instant
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_SHOWS_RATED_AT, ratedAt.getMillis()).commit();
Timber.d("downloadShowRatings: success, last rated_at %tF %tT", ratedAt.getMillis(), ratedAt.getMillis());
return UpdateResult.SUCCESS;
}
use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class TraktTools method processTraktSeasons.
/**
* Sync the watched/collected episodes of the given trakt show with the local episodes. The
* given show has to be watched/collected on trakt.
*
* @param isInitialSync If {@code true}, will upload watched/collected episodes that are not
* watched/collected on trakt. If {@code false}, will set them not watched/collected (if not
* skipped) to mirror the trakt episode.
*/
public int processTraktSeasons(boolean isInitialSync, int localShow, @NonNull BaseShow traktShow, @NonNull Flag flag) {
HashMap<Integer, BaseSeason> traktSeasons = buildTraktSeasonsMap(traktShow.seasons);
Cursor localSeasonsQuery = context.getContentResolver().query(SeriesGuideContract.Seasons.buildSeasonsOfShowUri(localShow), new String[] { SeriesGuideContract.Seasons._ID, SeriesGuideContract.Seasons.COMBINED }, null, null, null);
if (localSeasonsQuery == null) {
return FAILED;
}
final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
List<SyncSeason> syncSeasons = new ArrayList<>();
while (localSeasonsQuery.moveToNext()) {
String seasonId = localSeasonsQuery.getString(0);
int seasonNumber = localSeasonsQuery.getInt(1);
if (traktSeasons.containsKey(seasonNumber)) {
// season watched/collected on trakt
if (!processTraktEpisodes(isInitialSync, seasonId, traktSeasons.get(seasonNumber), syncSeasons, flag)) {
return FAILED;
}
} else {
// season not watched/collected on trakt
if (isInitialSync) {
// schedule all watched/collected episodes of this season for upload
SyncSeason syncSeason = buildSyncSeason(seasonId, seasonNumber, flag);
if (syncSeason != null) {
syncSeasons.add(syncSeason);
}
} else {
// set all watched/collected episodes of season not watched/collected
batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId)).withSelection(flag.clearFlagSelection, null).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
}
}
}
localSeasonsQuery.close();
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "Setting seasons unwatched failed.");
}
if (syncSeasons.size() > 0) {
// upload watched/collected episodes for this show
Integer showTraktId = ShowTools.getShowTraktId(context, localShow);
if (showTraktId == null) {
// show should have a trakt id, give up
return FAILED;
}
return uploadEpisodes(showTraktId, syncSeasons, flag);
} else {
return SUCCESS;
}
}
use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class TraktTools method uploadEpisodes.
/**
* Uploads all watched/collected episodes for the given show to trakt.
*
* @return Any of the {@link TraktTools} result codes.
*/
private int uploadEpisodes(int showTvdbId, int showTraktId, Flag flag) {
// query for watched/collected episodes
Cursor localEpisodes = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodesOfShowUri(showTvdbId), EpisodesQuery.PROJECTION, flag.flagSelection, null, SeriesGuideContract.Episodes.SORT_SEASON_ASC);
if (localEpisodes == null) {
Timber.e("uploadEpisodes: query failed");
return FAILED;
}
// build a list of watched/collected episodes
List<SyncSeason> syncSeasons = new LinkedList<>();
buildEpisodeList(localEpisodes, syncSeasons);
localEpisodes.close();
if (syncSeasons.size() == 0) {
// nothing to upload for this show
return SUCCESS;
}
return uploadEpisodes(showTraktId, syncSeasons, flag);
}
use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class TraktTools method uploadEpisodes.
/**
* Uploads all the given watched/collected episodes of the given show to trakt.
*
* @return Any of the {@link TraktTools} result codes.
*/
private int uploadEpisodes(int showTraktId, List<SyncSeason> syncSeasons, Flag flag) {
SyncShow syncShow = new SyncShow();
syncShow.id(ShowIds.trakt(showTraktId));
syncShow.seasons = syncSeasons;
// upload
SyncItems syncItems = new SyncItems().shows(syncShow);
try {
Response<SyncResponse> response;
if (flag == Flag.WATCHED) {
// uploading watched episodes
response = traktSync.get().addItemsToWatchedHistory(syncItems).execute();
} else {
// uploading collected episodes
response = traktSync.get().addItemsToCollection(syncItems).execute();
}
if (response.isSuccessful()) {
return SUCCESS;
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return FAILED_CREDENTIALS;
}
SgTrakt.trackFailedRequest(context, "add episodes to " + flag.name, response);
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "add episodes to " + flag.name, e);
}
return FAILED_API;
}
use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class TraktRatingsTask method doInBackground.
@Override
protected Void doInBackground(Void... params) {
long ratingId = createUniqueId(showTvdbId, episodeTvdbId);
// avoid saving ratings too frequently
// (network requests are cached, but also avoiding database writes)
long currentTimeMillis = System.currentTimeMillis();
synchronized (sCache) {
Long lastUpdateMillis = sCache.get(ratingId);
// if the ratings were just updated, do nothing
if (lastUpdateMillis != null && lastUpdateMillis > currentTimeMillis - MAXIMUM_AGE) {
Timber.d("Just loaded rating for %s, skip.", ratingId);
return null;
}
}
if (isCancelled() || !AndroidUtils.isNetworkConnected(context)) {
return null;
}
// look up show trakt id
Integer showTraktId = ShowTools.getShowTraktId(context, showTvdbId);
if (showTraktId == null) {
Timber.d("Show %s has no trakt id, skip.", showTvdbId);
return null;
}
String showTraktIdString = String.valueOf(showTraktId);
boolean isShowNotEpisode = episodeTvdbId == 0;
Ratings ratings;
if (isShowNotEpisode) {
ratings = SgTrakt.executeCall(context, traktShows.get().ratings(showTraktIdString), "get show rating");
} else {
ratings = SgTrakt.executeCall(context, traktEpisodes.get().ratings(showTraktIdString, season, episode), "get episode rating");
}
if (ratings != null && ratings.rating != null && ratings.votes != null) {
if (isShowNotEpisode) {
saveShowRating(ratings);
} else {
saveEpisodeRating(ratings);
}
}
// cache download time to avoid saving ratings too frequently
synchronized (sCache) {
sCache.put(ratingId, currentTimeMillis);
}
return null;
}
Aggregations