use of com.uwetrottmann.trakt5.enums.Rating 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.enums.Rating 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;
}
use of com.uwetrottmann.trakt5.enums.Rating 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.uwetrottmann.trakt5.enums.Rating in project SeriesGuide by UweTrottmann.
the class TraktRatingsSync method downloadForEpisodes.
/**
* 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 boolean downloadForEpisodes(@Nullable OffsetDateTime ratedAt) {
if (ratedAt == null) {
Timber.e("downloadForEpisodes: null rated_at");
return false;
}
long lastRatedAt = TraktSettings.getLastEpisodesRatedAt(context);
if (!TimeTools.isAfterMillis(ratedAt, lastRatedAt)) {
// not initial sync, no ratings have changed
Timber.d("downloadForEpisodes: no changes since %tF %tT", lastRatedAt, lastRatedAt);
return true;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return false;
}
// download rated episodes
List<RatedEpisode> ratedEpisodes;
try {
Response<List<RatedEpisode>> response = traktSync.ratingsEpisodes(RatingsFilter.ALL, null, null, null).execute();
if (response.isSuccessful()) {
ratedEpisodes = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return false;
}
Errors.logAndReport("get episode ratings", response);
return false;
}
} catch (Exception e) {
Errors.logAndReport("get episode ratings", e);
return false;
}
if (ratedEpisodes == null) {
Timber.e("downloadForEpisodes: null response");
return false;
}
if (ratedEpisodes.isEmpty()) {
Timber.d("downloadForEpisodes: no ratings on trakt");
return true;
}
// 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;
Map<Integer, Integer> tmdbIdsToRatings = new HashMap<>();
for (RatedEpisode episode : ratedEpisodes) {
if (episode.rating == null || episode.episode == null || episode.episode.ids == null || episode.episode.ids.tmdb == null) {
// skip, can't handle
continue;
}
if (episode.rated_at != null && TimeTools.isBeforeMillis(episode.rated_at, ratedAtThreshold)) {
// no need to apply older ratings again
break;
}
// if an episode does not exist, this update will do nothing
tmdbIdsToRatings.put(episode.episode.ids.tmdb, episode.rating.value);
}
// apply database updates
SgRoomDatabase.getInstance(context).sgEpisode2Helper().updateUserRatings(tmdbIdsToRatings);
// save last rated instant
long ratedAtTime = ratedAt.toInstant().toEpochMilli();
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_EPISODES_RATED_AT, ratedAtTime).apply();
Timber.d("downloadForEpisodes: success, last rated_at %tF %tT", ratedAtTime, ratedAtTime);
return true;
}
use of com.uwetrottmann.trakt5.enums.Rating in project SeriesGuide by UweTrottmann.
the class TraktRatingsSync method downloadForShows.
/**
* 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 boolean downloadForShows(@Nullable OffsetDateTime ratedAt) {
if (ratedAt == null) {
Timber.e("downloadForShows: null rated_at");
return false;
}
long lastRatedAt = TraktSettings.getLastShowsRatedAt(context);
if (!TimeTools.isAfterMillis(ratedAt, lastRatedAt)) {
// not initial sync, no ratings have changed
Timber.d("downloadForShows: no changes since %tF %tT", lastRatedAt, lastRatedAt);
return true;
}
if (!TraktCredentials.get(context).hasCredentials()) {
return false;
}
// download rated shows
List<RatedShow> ratedShows;
try {
Response<List<RatedShow>> response = traktSync.ratingsShows(RatingsFilter.ALL, null, null, null).execute();
if (response.isSuccessful()) {
ratedShows = response.body();
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return false;
}
Errors.logAndReport("get show ratings", response);
return false;
}
} catch (Exception e) {
Errors.logAndReport("get show ratings", e);
return false;
}
if (ratedShows == null) {
Timber.e("downloadForShows: null response");
return false;
}
if (ratedShows.isEmpty()) {
Timber.d("downloadForShows: no ratings on trakt");
return true;
}
// 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)
Map<Integer, Integer> tmdbIdsToRatings = new HashMap<>();
for (RatedShow show : ratedShows) {
Rating rating = show.rating;
if (rating == null || show.show == null || show.show.ids == null) {
continue;
}
Integer showTmdbId = show.show.ids.tmdb;
if (showTmdbId == null) {
continue;
}
if (show.rated_at != null && TimeTools.isBeforeMillis(show.rated_at, ratedAtThreshold)) {
// no need to apply older ratings again
break;
}
// if a show does not exist, this update will do nothing
tmdbIdsToRatings.put(showTmdbId, rating.value);
}
// apply database updates
SgRoomDatabase.getInstance(context).sgShow2Helper().updateUserRatings(tmdbIdsToRatings);
// save last rated instant
long ratedAtTime = ratedAt.toInstant().toEpochMilli();
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(TraktSettings.KEY_LAST_SHOWS_RATED_AT, ratedAtTime).apply();
Timber.d("downloadForShows: success, last rated_at %tF %tT", ratedAtTime, ratedAtTime);
return true;
}
Aggregations