use of com.uwetrottmann.trakt5.entities.SyncSeason in project SeriesGuide by UweTrottmann.
the class TraktTools method buildEpisodeList.
/**
* @param episodesCursor Cursor of episodes sorted by season (ascending).
* @param seasons Empty list.
*/
private static void buildEpisodeList(Cursor episodesCursor, List<SyncSeason> seasons) {
SyncSeason currentSeason = null;
while (episodesCursor.moveToNext()) {
int season = episodesCursor.getInt(EpisodesQuery.SEASON);
int episode = episodesCursor.getInt(EpisodesQuery.EPISODE);
// create new season if none exists or number has changed
if (currentSeason == null || currentSeason.number != season) {
currentSeason = new SyncSeason().number(season);
currentSeason.episodes = new LinkedList<>();
seasons.add(currentSeason);
}
// add episode
currentSeason.episodes.add(new SyncEpisode().number(episode));
}
}
use of com.uwetrottmann.trakt5.entities.SyncSeason in project SeriesGuide by UweTrottmann.
the class TraktTools method processTraktSeasons.
/**
* Sync the watched/collected episodes of the given trakt show with the local episodes. The
* given show has to be watched/collected on trakt.
*
* @param isInitialSync If {@code true}, will upload watched/collected episodes that are not
* watched/collected on trakt. If {@code false}, will set them not watched/collected (if not
* skipped) to mirror the trakt episode.
*/
public int processTraktSeasons(boolean isInitialSync, int localShow, @NonNull BaseShow traktShow, @NonNull Flag flag) {
HashMap<Integer, BaseSeason> traktSeasons = buildTraktSeasonsMap(traktShow.seasons);
Cursor localSeasonsQuery = context.getContentResolver().query(SeriesGuideContract.Seasons.buildSeasonsOfShowUri(localShow), new String[] { SeriesGuideContract.Seasons._ID, SeriesGuideContract.Seasons.COMBINED }, null, null, null);
if (localSeasonsQuery == null) {
return FAILED;
}
final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
List<SyncSeason> syncSeasons = new ArrayList<>();
while (localSeasonsQuery.moveToNext()) {
String seasonId = localSeasonsQuery.getString(0);
int seasonNumber = localSeasonsQuery.getInt(1);
if (traktSeasons.containsKey(seasonNumber)) {
// season watched/collected on trakt
if (!processTraktEpisodes(isInitialSync, seasonId, traktSeasons.get(seasonNumber), syncSeasons, flag)) {
return FAILED;
}
} else {
// season not watched/collected on trakt
if (isInitialSync) {
// schedule all watched/collected episodes of this season for upload
SyncSeason syncSeason = buildSyncSeason(seasonId, seasonNumber, flag);
if (syncSeason != null) {
syncSeasons.add(syncSeason);
}
} else {
// set all watched/collected episodes of season not watched/collected
batch.add(ContentProviderOperation.newUpdate(SeriesGuideContract.Episodes.buildEpisodesOfSeasonUri(seasonId)).withSelection(flag.clearFlagSelection, null).withValue(flag.databaseColumn, flag.notFlaggedValue).build());
}
}
}
localSeasonsQuery.close();
try {
DBUtils.applyInSmallBatches(context, batch);
} catch (OperationApplicationException e) {
Timber.e(e, "Setting seasons unwatched failed.");
}
if (syncSeasons.size() > 0) {
// upload watched/collected episodes for this show
Integer showTraktId = ShowTools.getShowTraktId(context, localShow);
if (showTraktId == null) {
// show should have a trakt id, give up
return FAILED;
}
return uploadEpisodes(showTraktId, syncSeasons, flag);
} else {
return SUCCESS;
}
}
use of com.uwetrottmann.trakt5.entities.SyncSeason in project SeriesGuide by UweTrottmann.
the class TraktTools method uploadEpisodes.
/**
* Uploads all watched/collected episodes for the given show to trakt.
*
* @return Any of the {@link TraktTools} result codes.
*/
private int uploadEpisodes(int showTvdbId, int showTraktId, Flag flag) {
// query for watched/collected episodes
Cursor localEpisodes = context.getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodesOfShowUri(showTvdbId), EpisodesQuery.PROJECTION, flag.flagSelection, null, SeriesGuideContract.Episodes.SORT_SEASON_ASC);
if (localEpisodes == null) {
Timber.e("uploadEpisodes: query failed");
return FAILED;
}
// build a list of watched/collected episodes
List<SyncSeason> syncSeasons = new LinkedList<>();
buildEpisodeList(localEpisodes, syncSeasons);
localEpisodes.close();
if (syncSeasons.size() == 0) {
// nothing to upload for this show
return SUCCESS;
}
return uploadEpisodes(showTraktId, syncSeasons, flag);
}
use of com.uwetrottmann.trakt5.entities.SyncSeason in project SeriesGuide by UweTrottmann.
the class TraktTools method uploadEpisodes.
/**
* Uploads all the given watched/collected episodes of the given show to trakt.
*
* @return Any of the {@link TraktTools} result codes.
*/
private int uploadEpisodes(int showTraktId, List<SyncSeason> syncSeasons, Flag flag) {
SyncShow syncShow = new SyncShow();
syncShow.id(ShowIds.trakt(showTraktId));
syncShow.seasons = syncSeasons;
// upload
SyncItems syncItems = new SyncItems().shows(syncShow);
try {
Response<SyncResponse> response;
if (flag == Flag.WATCHED) {
// uploading watched episodes
response = traktSync.get().addItemsToWatchedHistory(syncItems).execute();
} else {
// uploading collected episodes
response = traktSync.get().addItemsToCollection(syncItems).execute();
}
if (response.isSuccessful()) {
return SUCCESS;
} else {
if (SgTrakt.isUnauthorized(context, response)) {
return FAILED_CREDENTIALS;
}
SgTrakt.trackFailedRequest(context, "add episodes to " + flag.name, response);
}
} catch (IOException e) {
SgTrakt.trackFailedRequest(context, "add episodes to " + flag.name, e);
}
return FAILED_API;
}
use of com.uwetrottmann.trakt5.entities.SyncSeason in project SeriesGuide by UweTrottmann.
the class RateEpisodeTask method buildTraktSyncItems.
@Nullable
@Override
protected SyncItems buildTraktSyncItems() {
int season = -1;
int episode = -1;
int showTvdbId = -1;
Cursor query = getContext().getContentResolver().query(SeriesGuideContract.Episodes.buildEpisodeUri(episodeTvdbId), new String[] { SeriesGuideContract.Episodes.SEASON, SeriesGuideContract.Episodes.NUMBER, SeriesGuideContract.Shows.REF_SHOW_ID }, null, null, null);
if (query != null) {
if (query.moveToFirst()) {
season = query.getInt(0);
episode = query.getInt(1);
showTvdbId = query.getInt(2);
}
query.close();
}
if (season == -1 || episode == -1 || showTvdbId == -1) {
return null;
}
return new SyncItems().shows(new SyncShow().id(ShowIds.tvdb(showTvdbId)).seasons(new SyncSeason().number(season).episodes(new SyncEpisode().number(episode).rating(getRating()))));
}
Aggregations