Пример #1
0
  private SyncMovie toSyncMovie(Movie tmmMovie, boolean watched) {
    SyncMovie movie = null;

    MovieIds ids = new MovieIds();
    if (!tmmMovie.getIdAsString(Constants.IMDBID).isEmpty()) {
      ids.imdb = tmmMovie.getIdAsString(Constants.IMDBID);
    }
    if (tmmMovie.getIdAsInt(Constants.TMDBID) != 0) {
      ids.tmdb = tmmMovie.getIdAsInt(Constants.TMDBID);
    }
    if (tmmMovie.getIdAsInt(Constants.TRAKTID) != 0) {
      ids.trakt = tmmMovie.getIdAsInt(Constants.TRAKTID);
    }

    // we have to decide what we send; trakt behaves differenty when sending data to
    // sync collection and sync history.
    if (watched) {
      // sync history
      if (tmmMovie.isWatched() && tmmMovie.getLastWatched() == null) {
        // watched in tmm and not in trakt -> sync
        movie = new SyncMovie().id(ids).watchedAt(new DateTime(tmmMovie.getLastWatched()));
      }
    } else {
      // sync collection
      movie = new SyncMovie().id(ids).collectedAt(new DateTime(tmmMovie.getDateAdded()));
    }

    return movie;
  }
Пример #2
0
  /**
   * Syncs Trakt.tv "seen" flag (all gives you have already marked as watched)<br>
   * Gets all watched movies from Trakt, and sets the "watched" flag on TMM movies.<br>
   * Then update the remaining TMM movies on Trakt as 'seen'.
   */
  public void syncTraktMovieWatched(List<Movie> moviesInTmm) {
    if (!isEnabled()) {
      return;
    }

    // create a local copy of the list
    List<Movie> tmmMovies = new ArrayList<Movie>(moviesInTmm);

    // *****************************************************************************
    // 1) get all Trakt watched movies and update our "watched" status
    // *****************************************************************************
    List<BaseMovie> traktMovies = new ArrayList<BaseMovie>();
    try {
      traktMovies = TRAKT.sync().watchedMovies(Extended.DEFAULT_MIN);
      // Extended.DEFAULT adds url, poster, fanart, banner, genres
      // Extended.MAX adds certs, runtime, and other stuff (useful for scraper!)
    } 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 {
          traktMovies = TRAKT.sync().watchedMovies(Extended.DEFAULT_MIN);
        } catch (UnauthorizedException e1) {
          return;
        }
      } else {
        handleRetrofitError((RetrofitError) e.getCause());
        return;
      }
    }
    LOGGER.info(
        "You have "
            + traktMovies.size()
            + " movies marked as 'watched' in your Trakt.tv collection");

    // loop over all watched movies on trakt
    for (BaseMovie traktWatched : traktMovies) {

      // loop over TMM movies, and check if IMDBID match
      for (Movie tmmMovie : tmmMovies) {

        if (matches(tmmMovie, traktWatched.movie.ids)) {
          // we have a movie match

          // update missing IDs (we get them for free :)
          boolean dirty = updateIDs(tmmMovie, traktWatched.movie.ids);

          if (!tmmMovie.isWatched()) {
            // save Trakt watched status
            LOGGER.info("Marking movie '" + tmmMovie.getTitle() + "' as watched");
            tmmMovie.setWatched(true);
            dirty = true;
          }
          if (traktWatched.last_watched_at != null
              && !(traktWatched.last_watched_at.toDate().equals(tmmMovie.getLastWatched()))) {
            // always set from trakt, if not matched (Trakt = master)
            LOGGER.trace(
                "Marking movie '"
                    + tmmMovie.getTitle()
                    + "' as watched on "
                    + traktWatched.last_watched_at.toDate()
                    + " (was "
                    + tmmMovie.getLastWatched()
                    + ")");
            tmmMovie.setLastWatched(traktWatched.last_watched_at.toDate());
            dirty = true;
          }

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

    // *****************************************************************************
    // 2) mark additionally "watched" movies as 'seen' on Trakt
    // *****************************************************************************
    // Now get all TMM watched movies...
    List<Movie> tmmWatchedMovies = new ArrayList<Movie>();
    for (Movie movie : tmmMovies) {
      if (movie.isWatched()) {
        tmmWatchedMovies.add(movie);
      }
    }
    LOGGER.info(
        "You have now "
            + tmmWatchedMovies.size()
            + " movies marked as 'watched' in your TMM database");

    // ...and subtract the already watched from Trakt
    for (int i = tmmWatchedMovies.size() - 1; i >= 0; i--) {
      for (BaseMovie traktWatched : traktMovies) {
        Movie tmmMovie = tmmWatchedMovies.get(i);
        if (matches(tmmMovie, traktWatched.movie.ids)) {
          tmmWatchedMovies.remove(i);
          break;
        }
      }
    }

    if (tmmWatchedMovies.size() == 0) {
      LOGGER.info("no new watched movies for Trakt sync found.");
      return;
    }

    LOGGER.debug("prepare " + tmmWatchedMovies.size() + " movies for Trakt.tv sync");
    List<SyncMovie> movies = new ArrayList<SyncMovie>();
    int nosync = 0;
    for (Movie tmmMovie : tmmWatchedMovies) {
      if (tmmMovie.getIdAsInt(Constants.TRAKTID) != 0
          || !tmmMovie.getIdAsString(Constants.IMDBID).isEmpty()
          || tmmMovie.getIdAsInt(Constants.TMDBID) != 0) {
        movies.add(toSyncMovie(tmmMovie, true));
      } else {
        // do not add to Trakt if we do not have at least one ID
        nosync++;
        continue;
      }
    }
    if (nosync > 0) {
      LOGGER.debug("skipping " + nosync + " movies, because they have not been scraped yet!");
    }

    if (movies.size() == 0) {
      LOGGER.info("no new watched movies for Trakt sync found.");
      return;
    }

    try {
      LOGGER.info("Marking " + movies.size() + " movies as 'watched' to Trakt.tv collection");
      SyncItems items = new SyncItems().movies(movies);
      response = TRAKT.sync().addItemsToWatchedHistory(items);
      LOGGER.info("Trakt mark-as-watched status:");
      printStatus(response);
    } catch (RetrofitError e) {
      handleRetrofitError(e);
      return;
    } catch (UnauthorizedException e) {
      handleRetrofitError((RetrofitError) e.getCause());
      return;
    }
  }