Search in sources :

Example 1 with Show

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

the class TraktRecentEpisodeHistoryLoader method addItems.

protected void addItems(List<NowAdapter.NowItem> items, List<HistoryEntry> history) {
    SparseArrayCompat<String> localShows = ShowTools.getShowTvdbIdsAndPosters(getContext());
    long timeDayAgo = System.currentTimeMillis() - DateUtils.DAY_IN_MILLIS;
    for (int i = 0, size = history.size(); i < size; i++) {
        HistoryEntry entry = history.get(i);
        if (entry.episode == null || entry.episode.ids == null || entry.episode.ids.tvdb == null || entry.show == null || entry.watched_at == null) {
            // missing required values
            continue;
        }
        // however, include at least one older episode if there are none, yet
        if (entry.watched_at.isBefore(timeDayAgo) && items.size() > 1) {
            break;
        }
        // look for a TVDB poster
        String posterUrl;
        Integer showTvdbId = entry.show.ids == null ? null : entry.show.ids.tvdb;
        if (showTvdbId != null && localShows != null) {
            // prefer poster of already added show, fall back to first uploaded poster
            posterUrl = TvdbImageTools.smallSizeOrFirstUrl(localShows.get(showTvdbId), showTvdbId);
        } else {
            posterUrl = null;
        }
        String description = (entry.episode.season == null || entry.episode.number == null) ? entry.episode.title : TextTools.getNextEpisodeString(getContext(), entry.episode.season, entry.episode.number, entry.episode.title);
        NowAdapter.NowItem item = new NowAdapter.NowItem().displayData(entry.watched_at.getMillis(), entry.show.title, description, posterUrl).tvdbIds(entry.episode.ids.tvdb, showTvdbId).recentlyWatchedTrakt(entry.action);
        items.add(item);
    }
}
Also used : NowAdapter(com.battlelancer.seriesguide.adapters.NowAdapter) HistoryEntry(com.uwetrottmann.trakt5.entities.HistoryEntry)

Example 2 with Show

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

the class TvdbAddLoader method loadInBackground.

@Override
public Result loadInBackground() {
    List<SearchResult> results;
    if (TextUtils.isEmpty(query)) {
        // no query? load a list of trending shows from trakt
        List<TrendingShow> trendingShows = SgTrakt.executeCall(app, traktShows.get().trending(1, 35, Extended.FULL), "get trending shows");
        if (trendingShows != null) {
            List<Show> shows = new LinkedList<>();
            for (TrendingShow show : trendingShows) {
                if (show.show == null || show.show.ids == null || show.show.ids.tvdb == null) {
                    // skip if required values are missing
                    continue;
                }
                shows.add(show.show);
            }
            // manually set the language to the current search language
            results = TraktAddLoader.parseTraktShowsToSearchResults(getContext(), shows, language);
            return buildResultSuccess(results, R.string.add_empty);
        } else {
            return buildResultFailure(R.string.trakt);
        }
    } else {
        // use TheTVDB search for all other (or any) languages
        if (DisplaySettings.LANGUAGE_EN.equals(language)) {
            List<com.uwetrottmann.trakt5.entities.SearchResult> searchResults = SgTrakt.executeCall(app, traktSearch.get().textQueryShow(query, null, null, null, null, null, null, null, null, null, Extended.FULL, 1, 30), "search shows");
            if (searchResults != null) {
                List<Show> shows = new LinkedList<>();
                for (com.uwetrottmann.trakt5.entities.SearchResult result : searchResults) {
                    if (result.show == null || result.show.ids == null || result.show.ids.tvdb == null) {
                        // skip, TVDB id required
                        continue;
                    }
                    shows.add(result.show);
                }
                // manually set the language to English
                results = TraktAddLoader.parseTraktShowsToSearchResults(getContext(), shows, DisplaySettings.LANGUAGE_EN);
                return buildResultSuccess(results, R.string.no_results);
            } else {
                return buildResultFailure(R.string.trakt);
            }
        } else {
            try {
                if (TextUtils.isEmpty(language)) {
                    // use the v1 API to do an any language search not supported by v2
                    results = TvdbTools.getInstance(app).searchShow(query, null);
                } else {
                    results = TvdbTools.getInstance(app).searchSeries(query, language);
                }
                markLocalShows(results);
                return buildResultSuccess(results, R.string.no_results);
            } catch (TvdbException e) {
                Timber.e(e, "Searching show failed");
            }
            return buildResultFailure(R.string.tvdb);
        }
    }
}
Also used : SearchResult(com.battlelancer.seriesguide.items.SearchResult) TrendingShow(com.uwetrottmann.trakt5.entities.TrendingShow) LinkedList(java.util.LinkedList) Show(com.uwetrottmann.trakt5.entities.Show) TrendingShow(com.uwetrottmann.trakt5.entities.TrendingShow) TvdbException(com.battlelancer.seriesguide.thetvdbapi.TvdbException)

Example 3 with Show

use of com.uwetrottmann.trakt5.entities.Show 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
    List<Friend> friends = SgTrakt.executeAuthenticatedCall(getContext(), traktUsers.get().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> localShows = ShowTools.getShowTvdbIdsAndPosters(getContext());
    boolean preventSpoilers = 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(getContext(), traktUsers.get().history(new UserSlug(userSlug), HistoryType.EPISODES, 1, 1, Extended.DEFAULT_MIN, 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 showTvdbId = entry.show.ids == null ? null : entry.show.ids.tvdb;
        if (showTvdbId != null && localShows != null) {
            // prefer poster of already added show, fall back to first uploaded poster
            posterUrl = TvdbImageTools.smallSizeOrFirstUrl(localShows.get(showTvdbId), showTvdbId);
        } else {
            posterUrl = null;
        }
        String avatar = (friend.user.images == null || friend.user.images.avatar == null) ? null : friend.user.images.avatar.full;
        String episodeString;
        if (preventSpoilers) {
            // just display the number
            episodeString = TextTools.getEpisodeNumber(getContext(), entry.episode.season, entry.episode.number);
        } else {
            // display number and title
            episodeString = TextTools.getNextEpisodeString(getContext(), entry.episode.season, entry.episode.number, entry.episode.title);
        }
        NowAdapter.NowItem nowItem = new NowAdapter.NowItem().displayData(entry.watched_at.getMillis(), entry.show.title, episodeString, posterUrl).tvdbIds(entry.episode.ids == null ? null : entry.episode.ids.tvdb, showTvdbId).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.adapters.NowAdapter) ArrayList(java.util.ArrayList) Friend(com.uwetrottmann.trakt5.entities.Friend) HistoryEntry(com.uwetrottmann.trakt5.entities.HistoryEntry)

Example 4 with Show

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

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

the class TvdbTools method updateShow.

/**
     * Updates a show. Adds new, updates changed and removes orphaned episodes.
     */
public void updateShow(int showTvdbId) throws TvdbException {
    // determine which translation to get
    String language = getShowLanguage(app, showTvdbId);
    if (language == null) {
        return;
    }
    final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
    Show show = getShowDetails(showTvdbId, language);
    batch.add(DBUtils.buildShowOp(app, show, false));
    // get episodes in the language as returned in the TVDB show entry
    // the show might not be available in the desired language
    getEpisodesAndUpdateDatabase(batch, show, show.language);
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) BaseShow(com.uwetrottmann.trakt5.entities.BaseShow) Show(com.battlelancer.seriesguide.dataliberation.model.Show)

Aggregations

BaseShow (com.uwetrottmann.trakt5.entities.BaseShow)10 ArrayList (java.util.ArrayList)10 HistoryEntry (com.uwetrottmann.trakt5.entities.HistoryEntry)6 Show (com.uwetrottmann.trakt5.entities.Show)6 ContentProviderOperation (android.content.ContentProviderOperation)5 Show (com.battlelancer.seriesguide.dataliberation.model.Show)5 OperationApplicationException (android.content.OperationApplicationException)4 NonNull (android.support.annotation.NonNull)4 SuppressLint (android.annotation.SuppressLint)3 Cursor (android.database.Cursor)3 SearchResult (com.battlelancer.seriesguide.items.SearchResult)3 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)3 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)3 LinkedList (java.util.LinkedList)3 NowAdapter (com.battlelancer.seriesguide.adapters.NowAdapter)2 SgEpisode2Helper (com.battlelancer.seriesguide.provider.SgEpisode2Helper)2 TvdbException (com.battlelancer.seriesguide.thetvdbapi.TvdbException)2 TraktTools (com.battlelancer.seriesguide.util.TraktTools)2 Comment (com.uwetrottmann.trakt5.entities.Comment)2 Episode (com.uwetrottmann.trakt5.entities.Episode)2