Exemplo n.º 1
0
  private SyncShow toSyncShow(TvShow tmmShow, boolean watched) {
    SyncShow show = null;
    ShowIds ids = new ShowIds();
    if (!tmmShow.getIdAsString(Constants.IMDBID).isEmpty()) {
      ids.imdb = tmmShow.getIdAsString(Constants.IMDBID);
    }
    if (tmmShow.getIdAsInt(Constants.TMDBID) != 0) {
      ids.tmdb = tmmShow.getIdAsInt(Constants.TMDBID);
    }
    if (tmmShow.getIdAsInt(Constants.TVDBID) != 0) {
      ids.tvdb = tmmShow.getIdAsInt(Constants.TVDBID);
    }
    if (tmmShow.getIdAsInt(Constants.TRAKTID) != 0) {
      ids.trakt = tmmShow.getIdAsInt(Constants.TRAKTID);
    }
    if (tmmShow.getIdAsInt(Constants.TVRAGEID) != 0) {
      ids.tvrage = tmmShow.getIdAsInt(Constants.TVRAGEID);
    }

    ArrayList<SyncSeason> ss = new ArrayList<SyncSeason>();
    boolean foundS = false;
    for (TvShowSeason tmmSeason : tmmShow.getSeasons()) {
      boolean foundEP = false;
      ArrayList<SyncEpisode> se = new ArrayList<SyncEpisode>();
      for (TvShowEpisode tmmEp : tmmSeason.getEpisodes()) {
        // we have to decide what we send; trakt behaves differenty when sending data to
        // sync collection and sync history.
        if (watched) {
          // sync history
          if (tmmEp.isWatched() && tmmEp.getLastWatched() == null) {
            // watched in tmm and not in trakt -> sync
            se.add(
                new SyncEpisode()
                    .number(tmmEp.getEpisode())
                    .watchedAt(new DateTime(tmmEp.getLastWatched())));
            foundEP = true;
          }
        } else {
          // sync collection
          se.add(
              new SyncEpisode()
                  .number(tmmEp.getEpisode())
                  .collectedAt(new DateTime(tmmEp.getDateAdded())));
          foundEP = true;
        }
      }
      if (foundEP) {
        // do not send empty seasons
        foundS = true;
        ss.add(new SyncSeason().number(tmmSeason.getSeason()).episodes(se));
      }
    }

    if (foundS) {
      // we have at least one season/episode, so add it
      show = new SyncShow().id(ids).collectedAt(new DateTime(tmmShow.getDateAdded())).seasons(ss);
    }

    // if nothing added, do NOT send an empty show (to add all)
    return show;
  }
Exemplo n.º 2
0
  public void syncTraktTvShowWatched(List<TvShow> tvShowsInTmm) {
    if (!isEnabled()) {
      return;
    }

    // create a local copy of the list
    List<TvShow> tvShows = new ArrayList<TvShow>(tvShowsInTmm);

    List<BaseShow> traktShows = new ArrayList<BaseShow>();
    try {
      traktShows = TRAKT.sync().watchedShows(Extended.DEFAULT_MIN);
    } catch (RetrofitError e) {
      handleRetrofitError(e);
      return;
    } catch (UnauthorizedException e) {
      // not authorized - maybe access token revoked - relogin
      if (this.Login()) {
        // ok, it worked, lets try once again :)
        try {
          traktShows = TRAKT.sync().watchedShows(Extended.DEFAULT_MIN);
        } catch (UnauthorizedException e1) {
          return;
        }
      } else {
        handleRetrofitError((RetrofitError) e.getCause());
        return;
      }
    }
    LOGGER.info("You have " + traktShows.size() + " TvShows marked as watched on Trakt.tv");
    for (BaseShow traktShow : traktShows) {
      for (TvShow tmmShow : tvShows) {

        if (matches(tmmShow, traktShow.show.ids)) {
          // ok, we have a show match

          // update show IDs from trakt
          boolean dirty = updateIDs(tmmShow, traktShow.show.ids);

          // update watched date from trakt (show)
          if (traktShow.last_watched_at != null
              && !(traktShow.last_watched_at.toDate().equals(tmmShow.getLastWatched()))) {
            // always set from trakt, if not matched (Trakt = master)
            LOGGER.trace(
                "Marking TvShow '"
                    + tmmShow.getTitle()
                    + "' as watched on "
                    + traktShow.last_watched_at.toDate()
                    + " (was "
                    + tmmShow.getLastWatched()
                    + ")");
            tmmShow.setLastWatched(traktShow.last_watched_at.toDate());
            dirty = true;
          }

          // update collection date from trakt (episodes)
          for (BaseSeason bs : traktShow.seasons) {
            for (BaseEpisode be : bs.episodes) {
              TvShowEpisode tmmEP = tmmShow.getEpisode(bs.number, be.number);
              // update ep IDs - NOT YET POSSIBLE
              // boolean dirty = updateIDs(tmmEP, be.ids);

              if (tmmEP != null
                  && be.last_watched_at != null
                  && !(be.last_watched_at.toDate().equals(tmmEP.getLastWatched()))) {
                tmmEP.setLastWatched(be.last_watched_at.toDate());
                tmmEP.setWatched(true);
                dirty = true;
              }
            }
          }

          if (dirty) {
            tmmShow.saveToDb();
          }
        }
      }
    }

    // *****************************************************************************
    // 2) add all our shows to Trakt watched
    // *****************************************************************************
    LOGGER.info("Adding " + tvShows.size() + " TvShows as watched on Trakt.tv");
    // send show per show; sending all together may result too often in a timeout
    for (TvShow show : tvShows) {
      // get items to sync
      SyncShow sync = toSyncShow(show, true);
      if (sync == null) {
        continue;
      }

      try {
        SyncItems items = new SyncItems().shows(sync);
        response = TRAKT.sync().addItemsToWatchedHistory(items);

        LOGGER.debug("Trakt add-to-library status: " + show.getTitle());
        printStatus(response);
      } catch (RetrofitError e) {
        handleRetrofitError(e);
      } catch (UnauthorizedException e) {
        handleRetrofitError((RetrofitError) e.getCause());
      }
    }
  }