Search in sources :

Example 1 with Shows

use of com.uwetrottmann.seriesguide.backend.shows.Shows in project SeriesGuide by UweTrottmann.

the class HexagonShowSync method downloadShows.

private boolean downloadShows(List<SgShow2CloudUpdate> updates, Set<Long> toUpdate, HashMap<Integer, SearchResult> toAdd, Map<Integer, Long> tmdbIdsToShowIds, boolean hasMergedShows, DateTime lastSyncTime) {
    String cursor = null;
    boolean hasMoreShows = true;
    while (hasMoreShows) {
        // abort if connection is lost
        if (!AndroidUtils.isNetworkConnected(context)) {
            Timber.e("download: no network connection");
            return false;
        }
        List<SgCloudShow> shows;
        try {
            // get service each time to check if auth was removed
            Shows showsService = hexagonTools.getShowsService();
            if (showsService == null) {
                return false;
            }
            // use default server limit
            Shows.GetSgShows request = showsService.getSgShows();
            if (hasMergedShows) {
                // only get changed shows (otherwise returns all)
                request.setUpdatedSince(lastSyncTime);
            }
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            SgCloudShowList response = request.execute();
            if (response == null) {
                // If empty should send status 200 and empty list, so no body is a failure.
                Timber.e("download: response was null");
                return false;
            }
            shows = response.getShows();
            // check for more items
            if (response.getCursor() != null) {
                cursor = response.getCursor();
            } else {
                hasMoreShows = false;
            }
        } catch (IOException | IllegalArgumentException e) {
            // Note: JSON parser may throw IllegalArgumentException.
            Errors.logAndReportHexagon("get shows", e);
            return false;
        }
        if (shows == null || shows.size() == 0) {
            // nothing to do here
            break;
        }
        // append updates for received shows if there isn't one,
        // or appends shows not added locally
        appendShowUpdates(updates, toUpdate, toAdd, shows, tmdbIdsToShowIds, !hasMergedShows);
    }
    return true;
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) Shows(com.uwetrottmann.seriesguide.backend.shows.Shows) SgCloudShowList(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShowList) IOException(java.io.IOException)

Example 2 with Shows

use of com.uwetrottmann.seriesguide.backend.shows.Shows in project SeriesGuide by UweTrottmann.

the class HexagonTools method getShowsService.

/**
     * Returns the instance for this hexagon service or null if not signed in.
     */
@Nullable
public synchronized Shows getShowsService() {
    GoogleAccountCredential credential = getAccountCredential(true);
    if (credential.getSelectedAccount() == null) {
        return null;
    }
    if (showsService == null) {
        Shows.Builder builder = new Shows.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential);
        showsService = CloudEndpointUtils.updateBuilder(app, builder).build();
    }
    return showsService;
}
Also used : GoogleAccountCredential(com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential) Shows(com.uwetrottmann.seriesguide.backend.shows.Shows) Nullable(android.support.annotation.Nullable)

Example 3 with Shows

use of com.uwetrottmann.seriesguide.backend.shows.Shows in project SeriesGuide by UweTrottmann.

the class HexagonShowSync method downloadLegacyShows.

private boolean downloadLegacyShows(List<SgShow2CloudUpdate> updates, Set<Long> toUpdate, HashMap<Integer, SearchResult> toAdd, Map<Integer, Long> tmdbIdsToShowIds) {
    String cursor = null;
    boolean hasMoreShows = true;
    while (hasMoreShows) {
        // abort if connection is lost
        if (!AndroidUtils.isNetworkConnected(context)) {
            Timber.e("download: no network connection");
            return false;
        }
        List<Show> legacyShows;
        try {
            // get service each time to check if auth was removed
            Shows showsService = hexagonTools.getShowsService();
            if (showsService == null) {
                return false;
            }
            // use default server limit
            Shows.Get request = showsService.get();
            if (!TextUtils.isEmpty(cursor)) {
                request.setCursor(cursor);
            }
            ShowList response = request.execute();
            if (response == null) {
                // If empty should send status 200 and empty list, so no body is a failure.
                Timber.e("download: response was null");
                return false;
            }
            legacyShows = response.getShows();
            // check for more items
            if (response.getCursor() != null) {
                cursor = response.getCursor();
            } else {
                hasMoreShows = false;
            }
        } catch (IOException | IllegalArgumentException e) {
            // Note: JSON parser may throw IllegalArgumentException.
            Errors.logAndReportHexagon("get legacy shows", e);
            return false;
        }
        if (legacyShows == null || legacyShows.size() == 0) {
            // nothing to do here
            break;
        }
        List<SgCloudShow> shows = mapLegacyShows(legacyShows);
        if (shows == null) {
            return false;
        }
        // append updates for received shows if there isn't one,
        // or appends shows not added locally
        appendShowUpdates(updates, toUpdate, toAdd, shows, tmdbIdsToShowIds, true);
    }
    return true;
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) Shows(com.uwetrottmann.seriesguide.backend.shows.Shows) SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) Show(com.uwetrottmann.seriesguide.backend.shows.model.Show) IOException(java.io.IOException) SgCloudShowList(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShowList) ShowList(com.uwetrottmann.seriesguide.backend.shows.model.ShowList)

Example 4 with Shows

use of com.uwetrottmann.seriesguide.backend.shows.Shows in project SeriesGuide by UweTrottmann.

the class HexagonShowSync method upload.

/**
 * Uploads the given list of shows to Hexagon.
 */
public boolean upload(List<SgCloudShow> shows) {
    if (shows.isEmpty()) {
        Timber.d("upload: no shows to upload");
        return true;
    }
    // Issues with some requests failing at Cloud due to
    // EOFException: Unexpected end of ZLIB input stream
    // Using info log to report sizes that are uploaded to determine
    // if there is need for batching.
    // https://github.com/UweTrottmann/SeriesGuide/issues/781
    Timber.i("upload: %d shows", shows.size());
    // wrap into helper object
    SgCloudShowList showList = new SgCloudShowList();
    showList.setShows(shows);
    // upload shows
    try {
        // get service each time to check if auth was removed
        Shows showsService = hexagonTools.getShowsService();
        if (showsService == null) {
            return false;
        }
        showsService.saveSgShows(showList).execute();
    } catch (IOException e) {
        Errors.logAndReportHexagon("save shows", e);
        return false;
    }
    return true;
}
Also used : Shows(com.uwetrottmann.seriesguide.backend.shows.Shows) SgCloudShowList(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShowList) IOException(java.io.IOException)

Aggregations

Shows (com.uwetrottmann.seriesguide.backend.shows.Shows)4 SgCloudShowList (com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShowList)3 IOException (java.io.IOException)3 SgCloudShow (com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow)2 Nullable (android.support.annotation.Nullable)1 GoogleAccountCredential (com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential)1 Show (com.uwetrottmann.seriesguide.backend.shows.model.Show)1 ShowList (com.uwetrottmann.seriesguide.backend.shows.model.ShowList)1