Search in sources :

Example 1 with TraktTools

use of com.battlelancer.seriesguide.util.TraktTools in project SeriesGuide by UweTrottmann.

the class TvdbTools method addShow.

/**
     * Adds a show and its episodes to the database. If the show already exists, does nothing.
     *
     * <p> If signed in to Hexagon, gets show properties and episode flags.
     *
     * <p> If connected to trakt, but not signed in to Hexagon, gets episode flags from trakt
     * instead.
     *
     * @return True, if the show and its episodes were added to the database.
     */
public boolean addShow(int showTvdbId, @Nullable String language, @Nullable HashMap<Integer, BaseShow> traktCollection, @Nullable HashMap<Integer, BaseShow> traktWatched) throws TvdbException {
    boolean isShowExists = DBUtils.isShowExists(app, showTvdbId);
    if (isShowExists) {
        return false;
    }
    // get show and determine the language to use
    Show show = getShowDetailsWithHexagon(showTvdbId, language);
    language = show.language;
    // get episodes and store everything to the database
    final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    batch.add(DBUtils.buildShowOp(app, show, true));
    getEpisodesAndUpdateDatabase(batch, show, language);
    // restore episode flags...
    if (HexagonSettings.isEnabled(app)) {
        // ...from Hexagon
        boolean success = EpisodeTools.Download.flagsFromHexagon(app, showTvdbId);
        if (!success) {
            // failed to download episode flags
            // flag show as needing an episode merge
            ContentValues values = new ContentValues();
            values.put(Shows.HEXAGON_MERGE_COMPLETE, false);
            app.getContentResolver().update(Shows.buildShowUri(showTvdbId), values, null, null);
        }
        // flag show to be auto-added (again), send (new) language to Hexagon
        app.getShowTools().sendIsAdded(showTvdbId, language);
    } else {
        // ...from trakt
        TraktTools traktTools = app.getTraktTools();
        if (!traktTools.storeEpisodeFlags(traktWatched, showTvdbId, TraktTools.Flag.WATCHED)) {
            throw new TvdbDataException("addShow: storing trakt watched episodes failed.");
        }
        if (!traktTools.storeEpisodeFlags(traktCollection, showTvdbId, TraktTools.Flag.COLLECTED)) {
            throw new TvdbDataException("addShow: storing trakt collected episodes failed.");
        }
    }
    // calculate next episode
    DBUtils.updateLatestEpisode(app, showTvdbId);
    return true;
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) BaseShow(com.uwetrottmann.trakt5.entities.BaseShow) Show(com.battlelancer.seriesguide.dataliberation.model.Show) TraktTools(com.battlelancer.seriesguide.util.TraktTools)

Example 2 with TraktTools

use of com.battlelancer.seriesguide.util.TraktTools in project SeriesGuide by UweTrottmann.

the class SgSyncAdapter method performTraktSync.

private UpdateResult performTraktSync(HashSet<Integer> localShows, long currentTime) {
    if (!TraktCredentials.get(getContext()).hasCredentials()) {
        Timber.d("performTraktSync: no auth, skip");
        return UpdateResult.SUCCESS;
    }
    if (!AndroidUtils.isNetworkConnected(getContext())) {
        return UpdateResult.INCOMPLETE;
    }
    // get last activity timestamps
    TraktTools traktTools = app.getTraktTools();
    LastActivities lastActivity = traktTools.getLastActivity();
    if (lastActivity == null) {
        // trakt is likely offline or busy, try later
        Timber.e("performTraktSync: last activity download failed");
        return UpdateResult.INCOMPLETE;
    }
    if (!AndroidUtils.isNetworkConnected(getContext())) {
        return UpdateResult.INCOMPLETE;
    }
    if (localShows.size() == 0) {
        Timber.d("performTraktSync: no local shows, skip shows");
    } else {
        // download and upload episode watched and collected flags
        if (performTraktEpisodeSync(localShows, lastActivity.episodes, currentTime) != UpdateResult.SUCCESS) {
            return UpdateResult.INCOMPLETE;
        }
        if (!AndroidUtils.isNetworkConnected(getContext())) {
            return UpdateResult.INCOMPLETE;
        }
        // download show ratings
        if (traktTools.downloadShowRatings(lastActivity.shows.rated_at) != UpdateResult.SUCCESS) {
            return UpdateResult.INCOMPLETE;
        }
        if (!AndroidUtils.isNetworkConnected(getContext())) {
            return UpdateResult.INCOMPLETE;
        }
        // download episode ratings
        if (traktTools.downloadEpisodeRatings(lastActivity.episodes.rated_at) != UpdateResult.SUCCESS) {
            return UpdateResult.INCOMPLETE;
        }
        if (!AndroidUtils.isNetworkConnected(getContext())) {
            return UpdateResult.INCOMPLETE;
        }
    }
    // sync watchlist and collection with trakt
    if (app.getMovieTools().syncMovieListsWithTrakt(lastActivity.movies) != UpdateResult.SUCCESS) {
        return UpdateResult.INCOMPLETE;
    }
    if (!AndroidUtils.isNetworkConnected(getContext())) {
        return UpdateResult.INCOMPLETE;
    }
    // download watched movies
    if (traktTools.downloadWatchedMovies(lastActivity.movies.watched_at) != UpdateResult.SUCCESS) {
        return UpdateResult.INCOMPLETE;
    }
    // clean up any useless movies (not watched or not in any list)
    MovieTools.deleteUnusedMovies(getContext());
    if (!AndroidUtils.isNetworkConnected(getContext())) {
        return UpdateResult.INCOMPLETE;
    }
    // download movie ratings
    return traktTools.downloadMovieRatings(lastActivity.movies.rated_at);
}
Also used : TraktTools(com.battlelancer.seriesguide.util.TraktTools) LastActivities(com.uwetrottmann.trakt5.entities.LastActivities)

Aggregations

TraktTools (com.battlelancer.seriesguide.util.TraktTools)2 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 Show (com.battlelancer.seriesguide.dataliberation.model.Show)1 BaseShow (com.uwetrottmann.trakt5.entities.BaseShow)1 LastActivities (com.uwetrottmann.trakt5.entities.LastActivities)1 ArrayList (java.util.ArrayList)1