Пример #1
0
    internal static void AddEpisodesToWatchHistory(TraktSyncShowWatchedEx show)
    {
      var watchedEpisodes = (_WatchedEpisodes ?? new List<EpisodeWatched>()).ToList();
      var episodesToAdd = new List<EpisodeWatched>();

      foreach (var season in show.Seasons)
      {
        foreach (var episode in season.Episodes)
        {
          episodesToAdd.Add(new EpisodeWatched
          {
            Number = episode.Number,
            Season = season.Number,
            ShowId = show.Ids.Trakt,
            ShowImdbId = show.Ids.Imdb,
            ShowTvdbId = show.Ids.Tvdb,
            ShowTitle = show.Title,
            ShowYear = show.Year,
            Plays = 1,
            WatchedAt = episode.WatchedAt ?? DateTime.UtcNow.ToISO8601()
          });
        }
      }

      watchedEpisodes.AddRange(episodesToAdd);

      _WatchedEpisodes = watchedEpisodes;
    }
Пример #2
0
    /// <summary>
    /// Returns a list of shows for watched history sync as show objects with season / episode hierarchy
    /// </summary>
    private TraktSyncShowsWatchedEx GetWatchedShowsForSyncEx(IList<MediaItem> localWatchedEpisodes, List<TraktCache.EpisodeWatched> traktEpisodesWatched)
    {
      TraktLogger.Info("Finding local episodes to add to trakt.tv watched history");

      // prepare new sync object
      var syncWatchedEpisodes = new TraktSyncShowsWatchedEx();
      syncWatchedEpisodes.Shows = new List<TraktSyncShowWatchedEx>();

      // create a unique key to lookup and search for faster
      var onlineEpisodes = traktEpisodesWatched.ToLookup(twe => CreateLookupKey(twe), twe => twe);

      foreach (var episode in localWatchedEpisodes)
      {
        string tvdbKey = CreateLookupKey(episode);

        var traktEpisode = onlineEpisodes[tvdbKey].FirstOrDefault();

        // check if not watched on trakt and add it to sync list
        if (traktEpisode == null)
        {
          // check if we already have the show added to our sync object
          var syncShow = syncWatchedEpisodes.Shows.FirstOrDefault(swe => swe.Ids != null && swe.Ids.Tvdb == GetSeriesTvdbId(episode));
          if (syncShow == null)
          {
            // get show data from episode
            var show = GetSeriesTvdbId(episode);
            if (show == 0) continue;

            // create new show
            syncShow = new TraktSyncShowWatchedEx
            {
              Ids = new TraktShowId
              {
                Tvdb = GetSeriesTvdbId(episode),
                Imdb = GetSeriesImdbId(episode)
              },
              Title = GetSeriesTitle(episode),
              //Year = show.Year.ToNullableInt32()
            };

            // add a new season collection to show object
            syncShow.Seasons = new List<TraktSyncShowWatchedEx.Season>();

            // add show to the collection
            syncWatchedEpisodes.Shows.Add(syncShow);
          }

          // check if season exists in show sync object
          var syncSeason = syncShow.Seasons.FirstOrDefault(ss => ss.Number == GetSeasonIndex(episode));
          if (syncSeason == null)
          {
            // create new season
            syncSeason = new TraktSyncShowWatchedEx.Season
            {
              Number = GetSeasonIndex(episode)
            };

            // add a new episode collection to season object
            syncSeason.Episodes = new List<TraktSyncShowWatchedEx.Season.Episode>();

            // add season to the show
            syncShow.Seasons.Add(syncSeason);
          }

          // add episode to season
          syncSeason.Episodes.Add(new TraktSyncShowWatchedEx.Season.Episode
          {
            Number = GetEpisodeIndex(episode),
            WatchedAt = GetLastPlayedDate(episode)
          });
        }
      }

      return syncWatchedEpisodes;
    }