Search in sources :

Example 1 with Episode

use of com.uwetrottmann.seriesguide.backend.episodes.model.Episode in project SeriesGuide by UweTrottmann.

the class HexagonEpisodeSync method downloadFlagsByTvdbId.

private DownloadFlagsResult downloadFlagsByTvdbId(long showId, int showTvdbId) {
    List<Episode> episodes;
    boolean hasMoreEpisodes = true;
    String cursor = null;
    Long lastWatchedMs = null;
    while (hasMoreEpisodes) {
        // abort if connection is lost
        if (!AndroidUtils.isNetworkConnected(context)) {
            Timber.e("downloadFlags: no network connection");
            return DownloadFlagsResult.FAILED;
        }
        try {
            // get service each time to check if auth was removed
            Episodes episodesService = hexagonTools.getEpisodesService();
            if (episodesService == null) {
                return DownloadFlagsResult.FAILED;
            }
            // build request
            Episodes.Get request = episodesService.get().setShowTvdbId(// use default server limit
            showTvdbId);
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            // execute request
            EpisodeList response = request.execute();
            if (response == null) {
                // If empty should send status 200 and empty list, so no body is a failure.
                return DownloadFlagsResult.FAILED;
            }
            episodes = response.getEpisodes();
            // check for more items
            if (response.getCursor() != null) {
                cursor = response.getCursor();
            } else {
                hasMoreEpisodes = false;
            }
        } catch (IOException | IllegalArgumentException e) {
            // Note: JSON parser may throw IllegalArgumentException.
            Errors.logAndReportHexagon("get episodes of show", e);
            return DownloadFlagsResult.FAILED;
        }
        if (episodes == null || episodes.size() == 0) {
            // nothing to do here
            break;
        }
        // build batch of episode flag updates
        ArrayList<SgEpisode2UpdateByNumber> batch = new ArrayList<>();
        for (Episode episode : episodes) {
            Pair<SgEpisode2UpdateByNumber, Long> update = buildSgEpisodeUpdate(episode.getWatchedFlag(), episode.getPlays(), episode.getIsInCollection(), episode.getUpdatedAt(), episode.getEpisodeNumber(), episode.getSeasonNumber(), showId, lastWatchedMs);
            if (update != null) {
                batch.add(update.first);
                lastWatchedMs = update.second;
            }
        }
        // execute database update
        SgRoomDatabase.getInstance(context).sgEpisode2Helper().updateWatchedAndCollectedByNumber(batch);
    }
    return new DownloadFlagsResult(true, false, lastWatchedMs);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SgCloudEpisode(com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisode) Episode(com.uwetrottmann.seriesguide.backend.episodes.model.Episode) EpisodeList(com.uwetrottmann.seriesguide.backend.episodes.model.EpisodeList) SgCloudEpisodeList(com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisodeList) SgEpisode2UpdateByNumber(com.battlelancer.seriesguide.provider.SgEpisode2UpdateByNumber) Episodes(com.uwetrottmann.seriesguide.backend.episodes.Episodes)

Aggregations

SgEpisode2UpdateByNumber (com.battlelancer.seriesguide.provider.SgEpisode2UpdateByNumber)1 Episodes (com.uwetrottmann.seriesguide.backend.episodes.Episodes)1 Episode (com.uwetrottmann.seriesguide.backend.episodes.model.Episode)1 EpisodeList (com.uwetrottmann.seriesguide.backend.episodes.model.EpisodeList)1 SgCloudEpisode (com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisode)1 SgCloudEpisodeList (com.uwetrottmann.seriesguide.backend.episodes.model.SgCloudEpisodeList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1