Exemplo n.º 1
0
        public void CacheEpisodes(PlexSettings settings)
        {
            var videoHashset = new HashSet <Video>();

            // Ensure Plex is setup correctly
            if (string.IsNullOrEmpty(settings.PlexAuthToken))
            {
                return;
            }

            // Get the librarys and then get the tv section
            var sections    = PlexApi.GetLibrarySections(settings.PlexAuthToken, settings.FullUri);
            var tvSection   = sections.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase));
            var tvSectionId = tvSection?.Key;

            var currentPosition = 0;
            int totalSize;

            // Get the first 25 episodes (Paged)
            var episodes = PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount);

            // Parse the total amount of episodes
            int.TryParse(episodes.TotalSize, out totalSize);

            // Get all of the episodes in batches until we them all (Got'a catch 'em all!)
            while (currentPosition < totalSize)
            {
                videoHashset.UnionWith(PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount).Video
                                       .Where(x => x.Type.Equals(PlexType, StringComparison.InvariantCultureIgnoreCase)));
                currentPosition += ResultCount;
            }

            var entities = new ConcurrentDictionary <PlexEpisodes, byte>();

            Parallel.ForEach(videoHashset, video =>
            {
                // Get the individual episode Metadata (This is for us to get the TheTVDBId which also includes the episode number and season number)
                var metadata = PlexApi.GetEpisodeMetaData(settings.PlexAuthToken, settings.FullUri, video.RatingKey);

                // Loop through the metadata and create the model to insert into the DB
                foreach (var metadataVideo in metadata.Video)
                {
                    if (string.IsNullOrEmpty(metadataVideo.GrandparentTitle))
                    {
                        continue;
                    }
                    var epInfo = PlexHelper.GetSeasonsAndEpisodesFromPlexGuid(metadataVideo.Guid);
                    entities.TryAdd(
                        new PlexEpisodes
                    {
                        EpisodeNumber = epInfo.EpisodeNumber,
                        EpisodeTitle  = metadataVideo.Title,
                        ProviderId    = epInfo.ProviderId,
                        RatingKey     = metadataVideo.RatingKey,
                        SeasonNumber  = epInfo.SeasonNumber,
                        ShowTitle     = metadataVideo.GrandparentTitle
                    },
                        1);
                }
            });

            // Delete all of the current items
            Repo.DeleteAll(TableName);

            // Insert the new items
            var result = Repo.BatchInsert(entities.Select(x => x.Key).ToList(), TableName, typeof(PlexEpisodes).GetPropertyNames());

            if (!result)
            {
                Log.Error("Saving the Plex episodes to the DB Failed");
            }
        }