Search in sources :

Example 1 with SgCloudShow

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

the class HexagonShowSync method uploadAll.

/**
 * Uploads all local shows to Hexagon.
 */
public boolean uploadAll() {
    Timber.d("uploadAll: uploading all shows");
    List<SgShow2CloudUpdate> forCloudUpdate = SgRoomDatabase.getInstance(context).sgShow2Helper().getForCloudUpdate();
    List<SgCloudShow> shows = new LinkedList<>();
    for (SgShow2CloudUpdate localShow : forCloudUpdate) {
        if (localShow.getTmdbId() == null)
            continue;
        SgCloudShow show = new SgCloudShow();
        show.setTmdbId(localShow.getTmdbId());
        show.setIsFavorite(localShow.getFavorite());
        show.setNotify(localShow.getNotify());
        show.setIsHidden(localShow.getHidden());
        show.setLanguage(localShow.getLanguage());
        shows.add(show);
    }
    if (shows.size() == 0) {
        Timber.d("uploadAll: no shows to upload");
        // nothing to upload
        return true;
    }
    return upload(shows);
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) SgShow2CloudUpdate(com.battlelancer.seriesguide.provider.SgShow2CloudUpdate) LinkedList(java.util.LinkedList)

Example 2 with SgCloudShow

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

the class HexagonShowSync method appendShowUpdates.

private void appendShowUpdates(List<SgShow2CloudUpdate> updates, Set<Long> toUpdate, Map<Integer, SearchResult> toAdd, List<SgCloudShow> shows, Map<Integer, Long> tmdbIdsToShowIds, boolean mergeValues) {
    for (SgCloudShow show : shows) {
        // schedule to add shows not in local database
        Integer showTmdbId = show.getTmdbId();
        // Invalid data.
        if (showTmdbId == null)
            continue;
        Long showIdOrNull = tmdbIdsToShowIds.get(showTmdbId);
        if (showIdOrNull == null) {
            // ...but do NOT add shows marked as removed
            if (show.getIsRemoved() != null && show.getIsRemoved()) {
                continue;
            }
            if (!toAdd.containsKey(showTmdbId)) {
                SearchResult item = new SearchResult();
                item.setTmdbId(showTmdbId);
                item.setLanguage(show.getLanguage());
                item.setTitle("");
                toAdd.put(showTmdbId, item);
            }
        } else if (!toUpdate.contains(showIdOrNull)) {
            // Create update if there isn't already one.
            SgShow2CloudUpdate update = SgRoomDatabase.getInstance(context).sgShow2Helper().getForCloudUpdate(showIdOrNull);
            if (update != null) {
                boolean hasUpdates = false;
                if (show.getIsFavorite() != null) {
                    // when merging, favorite shows, but never unfavorite them
                    if (!mergeValues || show.getIsFavorite()) {
                        update.setFavorite(show.getIsFavorite());
                        hasUpdates = true;
                    }
                }
                if (show.getNotify() != null) {
                    // when merging, enable notifications, but never disable them
                    if (!mergeValues || show.getNotify()) {
                        update.setNotify(show.getNotify());
                        hasUpdates = true;
                    }
                }
                if (show.getIsHidden() != null) {
                    // when merging, un-hide shows, but never hide them
                    if (!mergeValues || !show.getIsHidden()) {
                        update.setHidden(show.getIsHidden());
                        hasUpdates = true;
                    }
                }
                if (!TextUtils.isEmpty(show.getLanguage())) {
                    // always overwrite with hexagon language value
                    update.setLanguage(show.getLanguage());
                    hasUpdates = true;
                }
                if (hasUpdates) {
                    updates.add(update);
                    toUpdate.add(showIdOrNull);
                }
            }
        }
    }
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) SearchResult(com.battlelancer.seriesguide.ui.search.SearchResult) SgShow2CloudUpdate(com.battlelancer.seriesguide.provider.SgShow2CloudUpdate)

Example 3 with SgCloudShow

use of com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow 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 4 with SgCloudShow

use of com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow 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 5 with SgCloudShow

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

the class HexagonShowSync method mapLegacyShows.

/**
 * Returns null on network error while looking up TMDB ID.
 */
@Nullable
private List<SgCloudShow> mapLegacyShows(List<Show> legacyShows) {
    List<SgCloudShow> shows = new ArrayList<>();
    for (Show legacyShow : legacyShows) {
        Integer showTvdbId = legacyShow.getTvdbId();
        if (showTvdbId == null || showTvdbId <= 0) {
            continue;
        }
        Integer showTmdbIdOrNull = new TmdbTools2().findShowTmdbId(context, showTvdbId);
        if (showTmdbIdOrNull == null) {
            // Network error, abort.
            return null;
        }
        // Only add if TMDB id found
        if (showTmdbIdOrNull != -1) {
            SgCloudShow show = new SgCloudShow();
            show.setTmdbId(showTmdbIdOrNull);
            show.setIsRemoved(legacyShow.getIsRemoved());
            show.setIsFavorite(legacyShow.getIsFavorite());
            show.setNotify(legacyShow.getNotify());
            show.setIsHidden(legacyShow.getIsHidden());
            show.setLanguage(legacyShow.getLanguage());
            shows.add(show);
        }
    }
    return shows;
}
Also used : SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) TmdbTools2(com.battlelancer.seriesguide.tmdbapi.TmdbTools2) ArrayList(java.util.ArrayList) SgCloudShow(com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow) Show(com.uwetrottmann.seriesguide.backend.shows.model.Show) Nullable(androidx.annotation.Nullable)

Aggregations

SgCloudShow (com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow)5 SgShow2CloudUpdate (com.battlelancer.seriesguide.provider.SgShow2CloudUpdate)2 Shows (com.uwetrottmann.seriesguide.backend.shows.Shows)2 SgCloudShowList (com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShowList)2 Show (com.uwetrottmann.seriesguide.backend.shows.model.Show)2 IOException (java.io.IOException)2 Nullable (androidx.annotation.Nullable)1 TmdbTools2 (com.battlelancer.seriesguide.tmdbapi.TmdbTools2)1 SearchResult (com.battlelancer.seriesguide.ui.search.SearchResult)1 ShowList (com.uwetrottmann.seriesguide.backend.shows.model.ShowList)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1