use of com.uwetrottmann.trakt5.services.Users in project SeriesGuide by UweTrottmann.
the class TraktFriendsMovieHistoryLoader method loadInBackground.
@Override
public List<NowAdapter.NowItem> loadInBackground() {
if (!TraktCredentials.get(getContext()).hasCredentials()) {
return null;
}
// get all trakt friends
Users traktUsers = SgApp.getServicesComponent(getContext()).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 movie for each friend
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.MOVIES, 1, 1, null, null, null), "get friend movie history");
if (history == null || history.size() == 0) {
// no history
continue;
}
HistoryEntry entry = history.get(0);
if (entry.watched_at == null || entry.movie == null) {
// missing required values
continue;
}
String avatar = (friend.user.images == null || friend.user.images.avatar == null) ? null : friend.user.images.avatar.full;
// trakt has removed image support: currently displaying no image
NowAdapter.NowItem nowItem = new NowAdapter.NowItem().displayData(entry.watched_at.toInstant().toEpochMilli(), entry.movie.title, null, null).tmdbId(entry.movie.ids == null ? null : entry.movie.ids.tmdb).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;
}
use of com.uwetrottmann.trakt5.services.Users in project SeriesGuide by UweTrottmann.
the class TraktTask method doCommentAction.
private TraktResponse doCommentAction() {
Comment commentOrNull = buildComment();
if (commentOrNull == null)
return buildErrorResponse();
TraktV2 trakt = SgApp.getServicesComponent(context).trakt();
try {
// post comment
retrofit2.Response<Comment> response = trakt.comments().post(commentOrNull).execute();
if (response.isSuccessful()) {
Comment postedComment = response.body();
if (postedComment.id != null) {
return new TraktResponse(true, null);
}
} else {
// check if comment failed validation or item does not exist on trakt
if (response.code() == 422) {
return new TraktResponse(false, context.getString(R.string.shout_invalid));
} else if (response.code() == 404) {
return new TraktResponse(false, context.getString(R.string.shout_invalid));
} else if (SgTrakt.isUnauthorized(response)) {
// for users banned from posting comments requests also return 401
// so do not sign out if an error header does not indicate the token is invalid
String authHeader = response.headers().get("Www-Authenticate");
if (authHeader != null && !authHeader.contains("invalid_token")) {
Pattern pattern = Pattern.compile("error_description=\"(.*)\"");
Matcher matcher = pattern.matcher(authHeader);
String message;
if (matcher.find()) {
message = matcher.group(1);
} else {
message = context.getString(R.string.trakt_error_credentials);
}
return new TraktResponse(false, message);
} else {
TraktCredentials.get(context).setCredentialsInvalid();
return new TraktResponse(false, context.getString(R.string.trakt_error_credentials));
}
} else {
Errors.logAndReport("post comment", response);
}
}
} catch (Exception e) {
Errors.logAndReport("post comment", e);
}
// return generic failure message
return buildErrorResponse();
}
use of com.uwetrottmann.trakt5.services.Users in project SeriesGuide by UweTrottmann.
the class TvdbTools method getShowDetailsWithHexagon.
/**
* Like {@link #getShowDetails(int, String)}, but if signed in and available adds properties
* stored on Hexagon.
*/
@NonNull
private Show getShowDetailsWithHexagon(int showTvdbId, @Nullable String language) throws TvdbException {
// check for show on hexagon
com.uwetrottmann.seriesguide.backend.shows.model.Show hexagonShow;
try {
hexagonShow = ShowTools.Download.showFromHexagon(app, showTvdbId);
} catch (IOException e) {
HexagonTools.trackFailedRequest(app, "get show details", e);
throw new TvdbCloudException("getShowDetailsWithHexagon: " + e.getMessage(), e);
}
// if no language is given, try to get the language stored on hexagon
if (language == null && hexagonShow != null) {
language = hexagonShow.getLanguage();
}
// if we still have no language, use the users default language
if (TextUtils.isEmpty(language)) {
language = DisplaySettings.getContentLanguage(app);
}
// get show info from TVDb and trakt
Show show = getShowDetails(showTvdbId, language);
if (hexagonShow != null) {
// restore properties from hexagon
if (hexagonShow.getIsFavorite() != null) {
show.favorite = hexagonShow.getIsFavorite();
}
if (hexagonShow.getIsHidden() != null) {
show.hidden = hexagonShow.getIsHidden();
}
}
return show;
}
use of com.uwetrottmann.trakt5.services.Users 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