use of com.uwetrottmann.trakt5.entities.Show 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.Show 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);
}
}
use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class BaseRateItemTask method doBackgroundAction.
@Override
protected Integer doBackgroundAction(Void... params) {
if (isSendingToTrakt()) {
if (!TraktCredentials.get(getContext()).hasCredentials()) {
return ERROR_TRAKT_AUTH;
}
SyncItems ratedItems = buildTraktSyncItems();
if (ratedItems == null) {
return ERROR_TRAKT_API;
}
SyncErrors notFound;
try {
Sync traktSync = SgApp.getServicesComponent(getContext()).traktSync();
Response<SyncResponse> response = traktSync.addRatings(ratedItems).execute();
if (response.isSuccessful()) {
notFound = response.body().not_found;
} else {
if (SgTrakt.isUnauthorized(getContext(), response)) {
return ERROR_TRAKT_AUTH;
}
Errors.logAndReport(getTraktAction(), response);
return ERROR_TRAKT_API;
}
} catch (Exception e) {
Errors.logAndReport(getTraktAction(), e);
return ERROR_TRAKT_API;
}
if (notFound != null) {
if ((notFound.movies != null && notFound.movies.size() != 0) || (notFound.shows != null && notFound.shows.size() != 0) || (notFound.episodes != null && notFound.episodes.size() != 0)) {
// movie, show or episode not found on trakt
return ERROR_TRAKT_API_NOT_FOUND;
}
}
}
if (!doDatabaseUpdate()) {
return ERROR_DATABASE;
}
return SUCCESS;
}
use of com.uwetrottmann.trakt5.entities.Show in project SeriesGuide by UweTrottmann.
the class TraktFriendsEpisodeHistoryLoader method loadInBackground.
@Override
public List<NowAdapter.NowItem> loadInBackground() {
if (!TraktCredentials.get(getContext()).hasCredentials()) {
return null;
}
// get all trakt friends
ServicesComponent services = SgApp.getServicesComponent(getContext());
Users traktUsers = services.traktUsers();
List<Friend> friends = SgTrakt.executeAuthenticatedCall(getContext(), traktUsers.friends(UserSlug.ME, Extended.FULL), "get friends");
if (friends == null) {
return null;
}
int size = friends.size();
if (size == 0) {
// no friends, done.
return null;
}
// estimate list size
List<NowAdapter.NowItem> items = new ArrayList<>(size + 1);
// add header
items.add(new NowAdapter.NowItem().header(getContext().getString(R.string.friends_recently)));
// add last watched episode for each friend
SparseArrayCompat<String> tmdbIdsToPoster = services.showTools().getTmdbIdsToPoster();
SgEpisode2Helper episodeHelper = SgRoomDatabase.getInstance(getContext()).sgEpisode2Helper();
boolean hideTitle = DisplaySettings.preventSpoilers(getContext());
for (int i = 0; i < size; i++) {
Friend friend = friends.get(i);
// at least need a userSlug
if (friend.user == null) {
continue;
}
String userSlug = friend.user.ids.slug;
if (TextUtils.isEmpty(userSlug)) {
continue;
}
// get last watched episode
List<HistoryEntry> history = SgTrakt.executeCall(traktUsers.history(new UserSlug(userSlug), HistoryType.EPISODES, 1, 1, null, null, null), "get friend episode history");
if (history == null || history.size() == 0) {
// no history
continue;
}
HistoryEntry entry = history.get(0);
if (entry.watched_at == null || entry.episode == null || entry.episode.season == null || entry.episode.number == null || entry.show == null) {
// missing required values
continue;
}
// 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 avatar = (friend.user.images == null || friend.user.images.avatar == null) ? null : friend.user.images.avatar.full;
String episodeString = TextTools.getNextEpisodeString(getContext(), entry.episode.season, entry.episode.number, hideTitle ? null : entry.episode.title);
Integer episodeTmdbIdOrNull = entry.episode.ids != null ? entry.episode.ids.tmdb : null;
long localEpisodeIdOrZero = episodeTmdbIdOrNull != null ? episodeHelper.getEpisodeIdByTmdbId(episodeTmdbIdOrNull) : 0;
NowAdapter.NowItem nowItem = new NowAdapter.NowItem().displayData(entry.watched_at.toInstant().toEpochMilli(), entry.show.title, episodeString, posterUrl).episodeIds(localEpisodeIdOrZero, showTmdbId != null ? showTmdbId : 0).friend(friend.user.username, avatar, entry.action);
items.add(nowItem);
}
// only have a header? return nothing
if (items.size() == 1) {
return Collections.emptyList();
}
return items;
}
Aggregations