Search in sources :

Example 1 with SyncResponse

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

the class TraktTools method uploadEpisodes.

/**
     * Uploads all the given watched/collected episodes of the given show to trakt.
     *
     * @return Any of the {@link TraktTools} result codes.
     */
private int uploadEpisodes(int showTraktId, List<SyncSeason> syncSeasons, Flag flag) {
    SyncShow syncShow = new SyncShow();
    syncShow.id(ShowIds.trakt(showTraktId));
    syncShow.seasons = syncSeasons;
    // upload
    SyncItems syncItems = new SyncItems().shows(syncShow);
    try {
        Response<SyncResponse> response;
        if (flag == Flag.WATCHED) {
            // uploading watched episodes
            response = traktSync.get().addItemsToWatchedHistory(syncItems).execute();
        } else {
            // uploading collected episodes
            response = traktSync.get().addItemsToCollection(syncItems).execute();
        }
        if (response.isSuccessful()) {
            return SUCCESS;
        } else {
            if (SgTrakt.isUnauthorized(context, response)) {
                return FAILED_CREDENTIALS;
            }
            SgTrakt.trackFailedRequest(context, "add episodes to " + flag.name, response);
        }
    } catch (IOException e) {
        SgTrakt.trackFailedRequest(context, "add episodes to " + flag.name, e);
    }
    return FAILED_API;
}
Also used : SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncResponse(com.uwetrottmann.trakt5.entities.SyncResponse) SyncShow(com.uwetrottmann.trakt5.entities.SyncShow) IOException(java.io.IOException)

Example 2 with SyncResponse

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

the class BaseMovieActionTask method doBackgroundAction.

@Override
protected Integer doBackgroundAction(Void... params) {
    // send to hexagon
    if (isSendingToHexagon()) {
        Movie movie = new Movie();
        movie.setTmdbId(movieTmdbId);
        setHexagonMovieProperties(movie);
        List<Movie> movies = new ArrayList<>();
        movies.add(movie);
        MovieList movieList = new MovieList();
        movieList.setMovies(movies);
        try {
            Movies moviesService = getContext().getHexagonTools().getMoviesService();
            if (moviesService == null) {
                return ERROR_HEXAGON_API;
            }
            moviesService.save(movieList).execute();
        } catch (IOException e) {
            HexagonTools.trackFailedRequest(getContext(), "save movie", e);
            return ERROR_HEXAGON_API;
        }
    }
    // send to trakt
    if (isSendingToTrakt()) {
        if (!TraktCredentials.get(getContext()).hasCredentials()) {
            return ERROR_TRAKT_AUTH;
        }
        SyncItems items = new SyncItems().movies(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)));
        try {
            Response<SyncResponse> response = doTraktAction(traktSync.get(), items).execute();
            if (response.isSuccessful()) {
                if (isMovieNotFound(response.body())) {
                    return ERROR_TRAKT_API_NOT_FOUND;
                }
            } else {
                if (SgTrakt.isUnauthorized(getContext(), response)) {
                    return ERROR_TRAKT_AUTH;
                }
                SgTrakt.trackFailedRequest(getContext(), getTraktAction(), response);
                return ERROR_TRAKT_API;
            }
        } catch (IOException e) {
            SgTrakt.trackFailedRequest(getContext(), getTraktAction(), e);
            return ERROR_TRAKT_API;
        }
    }
    // update local state
    if (!doDatabaseUpdate(getContext(), movieTmdbId)) {
        return ERROR_DATABASE;
    }
    return SUCCESS;
}
Also used : SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) Movie(com.uwetrottmann.seriesguide.backend.movies.model.Movie) SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncResponse(com.uwetrottmann.trakt5.entities.SyncResponse) ArrayList(java.util.ArrayList) MovieList(com.uwetrottmann.seriesguide.backend.movies.model.MovieList) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) Movies(com.uwetrottmann.seriesguide.backend.movies.Movies) IOException(java.io.IOException)

Example 3 with SyncResponse

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

the class MovieTools method toTrakt.

/**
     * Checks if the given movies are in the local collection or watchlist, then uploads them to the
     * appropriate list(s) on trakt.
     */
public UpdateResult toTrakt(Set<Integer> moviesNotOnTraktCollection, Set<Integer> moviesNotOnTraktWatchlist) {
    if (moviesNotOnTraktCollection.size() == 0 && moviesNotOnTraktWatchlist.size() == 0) {
        // nothing to upload
        Timber.d("toTrakt: nothing to upload");
        return UpdateResult.SUCCESS;
    }
    // return if connectivity is lost
    if (!AndroidUtils.isNetworkConnected(context)) {
        return UpdateResult.INCOMPLETE;
    }
    // query for movies in lists (excluding movies that are only watched)
    Cursor moviesInLists = context.getContentResolver().query(SeriesGuideContract.Movies.CONTENT_URI, PROJECTION_MOVIES_IN_LISTS, SeriesGuideContract.Movies.SELECTION_IN_LIST, null, null);
    if (moviesInLists == null) {
        Timber.e("toTrakt: query failed");
        return UpdateResult.INCOMPLETE;
    }
    // build list of collected, watchlisted movies to upload
    List<SyncMovie> moviesToCollect = new LinkedList<>();
    List<SyncMovie> moviesToWatchlist = new LinkedList<>();
    while (moviesInLists.moveToNext()) {
        int tmdbId = moviesInLists.getInt(0);
        // in local collection, but not on trakt?
        if (moviesInLists.getInt(1) == 1 && moviesNotOnTraktCollection.contains(tmdbId)) {
            moviesToCollect.add(new SyncMovie().id(MovieIds.tmdb(tmdbId)));
        }
        // in local watchlist, but not on trakt?
        if (moviesInLists.getInt(2) == 1 && moviesNotOnTraktWatchlist.contains(tmdbId)) {
            moviesToWatchlist.add(new SyncMovie().id(MovieIds.tmdb(tmdbId)));
        }
    }
    // clean up
    moviesInLists.close();
    // upload
    String action = null;
    SyncItems items = new SyncItems();
    Response<SyncResponse> response = null;
    try {
        if (moviesToCollect.size() > 0) {
            action = "add movies to collection";
            items.movies(moviesToCollect);
            response = traktSync.get().addItemsToCollection(items).execute();
        }
        if (response == null || response.isSuccessful()) {
            if (moviesToWatchlist.size() > 0) {
                action = "add movies to watchlist";
                items.movies(moviesToWatchlist);
                response = traktSync.get().addItemsToWatchlist(items).execute();
            }
        }
    } catch (IOException e) {
        SgTrakt.trackFailedRequest(context, action, e);
        return UpdateResult.INCOMPLETE;
    }
    if (response != null && !response.isSuccessful()) {
        if (SgTrakt.isUnauthorized(context, response)) {
            return UpdateResult.INCOMPLETE;
        }
        SgTrakt.trackFailedRequest(context, action, response);
        return UpdateResult.INCOMPLETE;
    }
    Timber.d("toTrakt: success, uploaded %s to collection, %s to watchlist", moviesToCollect.size(), moviesToWatchlist.size());
    return UpdateResult.SUCCESS;
}
Also used : SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncResponse(com.uwetrottmann.trakt5.entities.SyncResponse) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) IOException(java.io.IOException) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) SuppressLint(android.annotation.SuppressLint)

Example 4 with SyncResponse

use of com.uwetrottmann.trakt5.entities.SyncResponse 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 {
            Response<SyncResponse> response = traktSync.get().addRatings(ratedItems).execute();
            if (response.isSuccessful()) {
                notFound = response.body().not_found;
            } else {
                if (SgTrakt.isUnauthorized(getContext(), response)) {
                    return ERROR_TRAKT_AUTH;
                }
                SgTrakt.trackFailedRequest(getContext(), getTraktAction(), response);
                return ERROR_TRAKT_API;
            }
        } catch (IOException e) {
            SgTrakt.trackFailedRequest(getContext(), "rate movie", 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) IOException(java.io.IOException)

Aggregations

SyncItems (com.uwetrottmann.trakt5.entities.SyncItems)4 SyncResponse (com.uwetrottmann.trakt5.entities.SyncResponse)4 IOException (java.io.IOException)4 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)2 SuppressLint (android.annotation.SuppressLint)1 Cursor (android.database.Cursor)1 Movies (com.uwetrottmann.seriesguide.backend.movies.Movies)1 Movie (com.uwetrottmann.seriesguide.backend.movies.model.Movie)1 MovieList (com.uwetrottmann.seriesguide.backend.movies.model.MovieList)1 SyncErrors (com.uwetrottmann.trakt5.entities.SyncErrors)1 SyncShow (com.uwetrottmann.trakt5.entities.SyncShow)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1