Exemple #1
0
        public async Task ProcessEpsiodes(Metadata[] episodes, IQueryable <PlexEpisode> currentEpisodes)
        {
            var ep = new HashSet <PlexEpisode>();

            try
            {
                foreach (var episode in episodes)
                {
                    // I don't think we need to get the metadata, we only need to get the metadata if we need the provider id (TheTvDbid). Why do we need it for episodes?
                    // We have the parent and grandparent rating keys to link up to the season and series
                    //var metadata = _api.GetEpisodeMetaData(server.PlexAuthToken, server.FullUri, episode.ratingKey);

                    // This does seem to work, it looks like we can somehow get different rating, grandparent and parent keys with episodes. Not sure how.
                    var epExists = currentEpisodes.Any(x => episode.ratingKey == x.Key &&
                                                       episode.grandparentRatingKey == x.GrandparentKey);
                    if (epExists)
                    {
                        continue;
                    }

                    // Let's check if we have the parent
                    var seriesExists = await _repo.GetByKey(episode.grandparentRatingKey);

                    if (seriesExists == null)
                    {
                        // Ok let's try and match it to a title. TODO (This is experimental)
                        seriesExists = await _repo.GetAll().FirstOrDefaultAsync(x =>
                                                                                x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase));

                        if (seriesExists == null)
                        {
                            _log.LogWarning(
                                "The episode title {0} we cannot find the parent series. The episode grandparentKey = {1}, grandparentTitle = {2}",
                                episode.title, episode.grandparentRatingKey, episode.grandparentTitle);
                            continue;
                        }

                        // Set the rating key to the correct one
                        episode.grandparentRatingKey = seriesExists.Key;
                    }

                    ep.Add(new PlexEpisode
                    {
                        EpisodeNumber  = episode.index,
                        SeasonNumber   = episode.parentIndex,
                        GrandparentKey = episode.grandparentRatingKey,
                        ParentKey      = episode.parentRatingKey,
                        Key            = episode.ratingKey,
                        Title          = episode.title
                    });
                }

                await _repo.AddRange(ep);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }