Search in sources :

Example 1 with SyncMovie

use of com.uwetrottmann.trakt5.entities.SyncMovie 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 2 with SyncMovie

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

the class TraktMovieSync method uploadFlagsNotOnTrakt.

/**
 * Uploads the given movies to the appropriate list(s)/history on Trakt.
 */
private boolean uploadFlagsNotOnTrakt(Set<Integer> toCollectOnTrakt, Set<Integer> toWatchlistOnTrakt, Set<Integer> toSetWatchedOnTrakt) {
    if (toCollectOnTrakt.size() == 0 && toWatchlistOnTrakt.size() == 0 && toSetWatchedOnTrakt.size() == 0) {
        Timber.d("uploadLists: nothing to upload");
        return true;
    }
    if (!AndroidUtils.isNetworkConnected(context)) {
        // Fail, no connection is available.
        return false;
    }
    // Upload.
    String action = "";
    SyncItems items = new SyncItems();
    Response<SyncResponse> response = null;
    try {
        if (toCollectOnTrakt.size() > 0) {
            List<SyncMovie> moviesToCollect = convertToSyncMovieList(toCollectOnTrakt);
            action = "add movies to collection";
            items.movies(moviesToCollect);
            response = traktSync.addItemsToCollection(items).execute();
        }
        if (response == null || response.isSuccessful()) {
            if (toWatchlistOnTrakt.size() > 0) {
                List<SyncMovie> moviesToWatchlist = convertToSyncMovieList(toWatchlistOnTrakt);
                action = "add movies to watchlist";
                items.movies(moviesToWatchlist);
                response = traktSync.addItemsToWatchlist(items).execute();
            }
        }
        if (response == null || response.isSuccessful()) {
            if (toSetWatchedOnTrakt.size() > 0) {
                List<SyncMovie> moviesToSetWatched = convertToSyncMovieList(toSetWatchedOnTrakt);
                // Note: not setting a watched date (because not having one),
                // so Trakt will use the movie release date.
                action = "add movies to watched history";
                items.movies(moviesToSetWatched);
                response = traktSync.addItemsToWatchedHistory(items).execute();
            }
        }
    } catch (Exception e) {
        Errors.logAndReport(action, e);
        return false;
    }
    if (response != null && !response.isSuccessful()) {
        if (SgTrakt.isUnauthorized(context, response)) {
            return false;
        }
        Errors.logAndReport(action, response);
        return false;
    }
    Timber.d("uploadLists: success, uploaded %s to collection, %s to watchlist, %s set watched", toCollectOnTrakt.size(), toWatchlistOnTrakt.size(), toSetWatchedOnTrakt.size());
    return true;
}
Also used : SyncItems(com.uwetrottmann.trakt5.entities.SyncItems) SyncResponse(com.uwetrottmann.trakt5.entities.SyncResponse) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) OperationApplicationException(android.content.OperationApplicationException)

Example 3 with SyncMovie

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

the class TraktTask method doCheckInAction.

private TraktResponse doCheckInAction() {
    try {
        retrofit2.Response response;
        String message = mArgs.getString(InitBundle.MESSAGE);
        switch(mAction) {
            case CHECKIN_EPISODE:
                {
                    int episodeTvdbId = mArgs.getInt(InitBundle.EPISODE_TVDBID);
                    EpisodeCheckin checkin = new EpisodeCheckin.Builder(new SyncEpisode().id(EpisodeIds.tvdb(episodeTvdbId)), APP_VERSION, null).message(message).build();
                    response = traktCheckin.get().checkin(checkin).execute();
                    break;
                }
            case CHECKIN_MOVIE:
                {
                    int movieTmdbId = mArgs.getInt(InitBundle.MOVIE_TMDB_ID);
                    MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
                    response = traktCheckin.get().checkin(checkin).execute();
                    break;
                }
            default:
                throw new IllegalArgumentException("check-in action unknown.");
        }
        if (response.isSuccessful()) {
            return new TraktResponse(true, mContext.getString(R.string.checkin_success_trakt, mArgs.getString(InitBundle.TITLE)));
        } else {
            // check if the user wants to check-in, but there is already a check-in in progress
            CheckinError checkinError = trakt.get().checkForCheckinError(response);
            if (checkinError != null) {
                DateTime expiresAt = checkinError.expires_at;
                int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.getMillis() - System.currentTimeMillis()) / 1000);
                return new CheckinBlockedResponse(waitTimeMin);
            } else // check if item does not exist on trakt (yet)
            if (response.code() == 404) {
                return new TraktResponse(false, mContext.getString(R.string.trakt_error_not_exists));
            } else if (SgTrakt.isUnauthorized(mContext, response)) {
                return new TraktResponse(false, mContext.getString(R.string.trakt_error_credentials));
            } else {
                SgTrakt.trackFailedRequest(mContext, "check-in", response);
            }
        }
    } catch (IOException e) {
        SgTrakt.trackFailedRequest(mContext, "check-in", e);
    }
    // return generic failure message
    return buildErrorResponse();
}
Also used : CheckinError(com.uwetrottmann.trakt5.entities.CheckinError) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) EpisodeCheckin(com.uwetrottmann.trakt5.entities.EpisodeCheckin) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MovieCheckin(com.uwetrottmann.trakt5.entities.MovieCheckin) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie)

Example 4 with SyncMovie

use of com.uwetrottmann.trakt5.entities.SyncMovie 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 5 with SyncMovie

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

the class TraktTask method doCheckInAction.

private TraktResponse doCheckInAction() {
    TraktV2 trakt = SgApp.getServicesComponent(context).trakt();
    try {
        retrofit2.Response response;
        String message = args.getString(InitBundle.MESSAGE);
        switch(action) {
            case CHECKIN_EPISODE:
                {
                    // Check in using show Trakt ID
                    // and season and episode number (likely most reliable).
                    long episodeId = args.getLong(InitBundle.EPISODE_ID);
                    SgRoomDatabase database = SgRoomDatabase.getInstance(context);
                    SgEpisode2Numbers episode = database.sgEpisode2Helper().getEpisodeNumbers(episodeId);
                    if (episode == null) {
                        Timber.e("Failed to get episode %d", episodeId);
                        return buildErrorResponse();
                    }
                    Integer showTraktId = ShowTools.getShowTraktId(context, episode.getShowId());
                    if (showTraktId == null) {
                        Timber.e("Failed to get show %d", episode.getShowId());
                        return buildErrorResponse();
                    }
                    SyncEpisode traktEpisode = new SyncEpisode().season(episode.getSeason()).number(episode.getEpisodenumber());
                    Show traktShow = new Show();
                    traktShow.ids = ShowIds.trakt(showTraktId);
                    EpisodeCheckin checkin = new EpisodeCheckin.Builder(traktEpisode, APP_VERSION, null).show(traktShow).message(message).build();
                    response = trakt.checkin().checkin(checkin).execute();
                    break;
                }
            case CHECKIN_MOVIE:
                {
                    int movieTmdbId = args.getInt(InitBundle.MOVIE_TMDB_ID);
                    MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
                    response = trakt.checkin().checkin(checkin).execute();
                    break;
                }
            default:
                throw new IllegalArgumentException("check-in action unknown.");
        }
        if (response.isSuccessful()) {
            return new TraktResponse(true, context.getString(R.string.checkin_success_trakt, args.getString(InitBundle.TITLE)));
        } else {
            // check if the user wants to check-in, but there is already a check-in in progress
            CheckinError checkinError = trakt.checkForCheckinError(response);
            if (checkinError != null) {
                OffsetDateTime expiresAt = checkinError.expires_at;
                int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.toInstant().toEpochMilli() - System.currentTimeMillis()) / 1000);
                return new CheckinBlockedResponse(waitTimeMin);
            } else // check if item does not exist on trakt (yet)
            if (response.code() == 404) {
                return new TraktResponse(false, context.getString(R.string.trakt_error_not_exists));
            } else if (SgTrakt.isUnauthorized(context, response)) {
                return new TraktResponse(false, context.getString(R.string.trakt_error_credentials));
            } else {
                Errors.logAndReport("check-in", response);
            }
        }
    } catch (Exception e) {
        Errors.logAndReport("check-in", e);
    }
    // return generic failure message
    return buildErrorResponse();
}
Also used : CheckinError(com.uwetrottmann.trakt5.entities.CheckinError) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SgRoomDatabase(com.battlelancer.seriesguide.provider.SgRoomDatabase) EpisodeCheckin(com.uwetrottmann.trakt5.entities.EpisodeCheckin) SuppressLint(android.annotation.SuppressLint) MovieCheckin(com.uwetrottmann.trakt5.entities.MovieCheckin) TraktV2(com.uwetrottmann.trakt5.TraktV2) OffsetDateTime(org.threeten.bp.OffsetDateTime) SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) Show(com.uwetrottmann.trakt5.entities.Show)

Aggregations

SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)5 SyncItems (com.uwetrottmann.trakt5.entities.SyncItems)3 SyncResponse (com.uwetrottmann.trakt5.entities.SyncResponse)3 IOException (java.io.IOException)3 SuppressLint (android.annotation.SuppressLint)2 CheckinError (com.uwetrottmann.trakt5.entities.CheckinError)2 EpisodeCheckin (com.uwetrottmann.trakt5.entities.EpisodeCheckin)2 MovieCheckin (com.uwetrottmann.trakt5.entities.MovieCheckin)2 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)2 OperationApplicationException (android.content.OperationApplicationException)1 Cursor (android.database.Cursor)1 SgEpisode2Numbers (com.battlelancer.seriesguide.provider.SgEpisode2Numbers)1 SgRoomDatabase (com.battlelancer.seriesguide.provider.SgRoomDatabase)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 TraktV2 (com.uwetrottmann.trakt5.TraktV2)1 Show (com.uwetrottmann.trakt5.entities.Show)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1