Search in sources :

Example 6 with Episode

use of com.uwetrottmann.trakt5.entities.Episode 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;
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) BaseSeason(com.uwetrottmann.trakt5.entities.BaseSeason) OperationApplicationException(android.content.OperationApplicationException) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason)

Example 7 with Episode

use of com.uwetrottmann.trakt5.entities.Episode 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;
}
Also used : Ratings(com.uwetrottmann.trakt5.entities.Ratings)

Example 8 with Episode

use of com.uwetrottmann.trakt5.entities.Episode in project SeriesGuide by UweTrottmann.

the class TraktTask method buildComment.

private Comment buildComment() {
    Comment comment = new Comment();
    comment.comment = mArgs.getString(InitBundle.MESSAGE);
    comment.spoiler = mArgs.getBoolean(InitBundle.ISSPOILER);
    // as determined by "science", episode comments are most likely, so check for them first
    // episode?
    int episodeTvdbId = mArgs.getInt(InitBundle.EPISODE_TVDBID);
    if (episodeTvdbId != 0) {
        comment.episode = new Episode();
        comment.episode.ids = EpisodeIds.tvdb(episodeTvdbId);
        return comment;
    }
    // show?
    int showTvdbId = mArgs.getInt(InitBundle.SHOW_TVDBID);
    if (showTvdbId != 0) {
        comment.show = new Show();
        comment.show.ids = ShowIds.tvdb(showTvdbId);
        return comment;
    }
    // movie!
    int movieTmdbId = mArgs.getInt(InitBundle.MOVIE_TMDB_ID);
    comment.movie = new Movie();
    comment.movie.ids = MovieIds.tmdb(movieTmdbId);
    return comment;
}
Also used : Comment(com.uwetrottmann.trakt5.entities.Comment) Episode(com.uwetrottmann.trakt5.entities.Episode) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) Movie(com.uwetrottmann.trakt5.entities.Movie) Show(com.uwetrottmann.trakt5.entities.Show)

Example 9 with Episode

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

Example 10 with Episode

use of com.uwetrottmann.trakt5.entities.Episode 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()))));
}
Also used : SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SgRoomDatabase(com.battlelancer.seriesguide.provider.SgRoomDatabase) SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) SyncShow(com.uwetrottmann.trakt5.entities.SyncShow) SyncSeason(com.uwetrottmann.trakt5.entities.SyncSeason) Nullable(androidx.annotation.Nullable)

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