Search in sources :

Example 21 with Episode

use of com.uwetrottmann.trakt5.entities.Episode 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;
}
Also used : SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncResponse(com.uwetrottmann.trakt5.entities.SyncResponse) SyncErrors(com.uwetrottmann.trakt5.entities.SyncErrors) Sync(com.uwetrottmann.trakt5.services.Sync)

Example 22 with Episode

use of com.uwetrottmann.trakt5.entities.Episode 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;
}
Also used : UserSlug(com.uwetrottmann.trakt5.entities.UserSlug) ServicesComponent(com.battlelancer.seriesguide.modules.ServicesComponent) ArrayList(java.util.ArrayList) Users(com.uwetrottmann.trakt5.services.Users) Friend(com.uwetrottmann.trakt5.entities.Friend) HistoryEntry(com.uwetrottmann.trakt5.entities.HistoryEntry) SgEpisode2Helper(com.battlelancer.seriesguide.provider.SgEpisode2Helper)

Aggregations

ArrayList (java.util.ArrayList)9 HistoryEntry (com.uwetrottmann.trakt5.entities.HistoryEntry)8 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)6 ContentProviderOperation (android.content.ContentProviderOperation)4 OperationApplicationException (android.content.OperationApplicationException)4 Friend (com.uwetrottmann.trakt5.entities.Friend)4 SyncSeason (com.uwetrottmann.trakt5.entities.SyncSeason)4 UserSlug (com.uwetrottmann.trakt5.entities.UserSlug)4 Cursor (android.database.Cursor)3 NowAdapter (com.battlelancer.seriesguide.adapters.NowAdapter)3 Show (com.uwetrottmann.trakt5.entities.Show)3 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)3 SuppressLint (android.annotation.SuppressLint)2 Nullable (androidx.annotation.Nullable)2 SgEpisode2Helper (com.battlelancer.seriesguide.provider.SgEpisode2Helper)2 SgEpisode2Numbers (com.battlelancer.seriesguide.provider.SgEpisode2Numbers)2 SgRoomDatabase (com.battlelancer.seriesguide.provider.SgRoomDatabase)2 TraktTools (com.battlelancer.seriesguide.util.TraktTools)2 Comment (com.uwetrottmann.trakt5.entities.Comment)2 Episode (com.uwetrottmann.trakt5.entities.Episode)2