private List <PlexSearch> CachedLibraries(PlexSettings plexSettings, bool setCache)
        {
            var results = new List <PlexSearch>();

            if (!ValidateSettings(plexSettings))
            {
                Log.Warn("The settings are not configured");
                return(results); // don't error out here, just let it go! let it goo!!!
            }

            try
            {
                // TODO what the f**k was I thinking
                if (setCache)
                {
                    results = GetLibraries(plexSettings);
                    if (plexSettings.AdvancedSearch)
                    {
                        foreach (PlexSearch t in results)
                        {
                            foreach (Directory1 t1 in t.Directory)
                            {
                                var currentItem = t1;
                                var metaData    = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                      currentItem.RatingKey);

                                // Get the seasons for each show
                                if (currentItem.Type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
                                {
                                    var seasons = PlexApi.GetSeasons(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                     currentItem.RatingKey);

                                    // We do not want "all episodes" this as a season
                                    var filtered = seasons.Directory.Where(x => !x.Title.Equals("All episodes", StringComparison.CurrentCultureIgnoreCase));

                                    t1.Seasons.AddRange(filtered);
                                }

                                var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Directory.Guid);
                                t1.ProviderId = providerId;
                            }
                            foreach (Video t1 in t.Video)
                            {
                                var currentItem = t1;
                                var metaData    = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                      currentItem.RatingKey);
                                var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Video.Guid);
                                t1.ProviderId = providerId;
                            }
                        }
                    }
                    if (results != null)
                    {
                        Cache.Set(CacheKeys.PlexLibaries, results, CacheKeys.TimeFrameMinutes.SchedulerCaching);
                    }
                }
                else
                {
                    results = Cache.GetOrSet(CacheKeys.PlexLibaries, () =>
                                             GetLibraries(plexSettings), CacheKeys.TimeFrameMinutes.SchedulerCaching);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to obtain Plex libraries");
            }

            return(results);
        }
Exemple #2
0
        private async Task ProcessTvShow(PlexServers servers, Metadata show, HashSet <PlexServerContent> contentToAdd, Dictionary <int, int> contentProcessed)
        {
            var seasonList = await PlexApi.GetSeasons(servers.PlexAuthToken, servers.FullUri,
                                                      show.ratingKey);

            var seasonsContent = new List <PlexSeasonsContent>();

            foreach (var season in seasonList.MediaContainer.Metadata)
            {
                seasonsContent.Add(new PlexSeasonsContent
                {
                    ParentKey     = season.parentRatingKey,
                    SeasonKey     = season.ratingKey,
                    SeasonNumber  = season.index,
                    PlexContentId = show.ratingKey
                });
            }

            // Do we already have this item?
            // Let's try and match
            var existingContent = await Repo.GetFirstContentByCustom(x => x.Title == show.title &&
                                                                     x.ReleaseYear == show.year.ToString() &&
                                                                     x.Type == PlexMediaTypeEntity.Show);

            // Just double check the rating key, since this is our unique constraint
            var existingKey = await Repo.GetByKey(show.ratingKey);

            if (existingKey != null)
            {
                // Damn son.
                // Let's check if they match up
                var doesMatch = show.title.Equals(existingKey.Title,
                                                  StringComparison.CurrentCulture);
                if (!doesMatch)
                {
                    // Something f****d up on Plex at somepoint... Damn, rebuild of lib maybe?
                    // Lets delete the matching key
                    await Repo.Delete(existingKey);

                    existingKey = null;
                }
                else if (existingContent == null)
                {
                    existingContent = await Repo.GetFirstContentByCustom(x => x.Key == show.ratingKey);
                }
            }

            if (existingContent != null)
            {
                // Let's make sure that we have some sort of ID e.g. Imdbid for this,
                // Looks like it's possible to not have an Id for a show
                // I suspect we cached that show just as it was added to Plex.

                if (!existingContent.HasImdb && !existingContent.HasTheMovieDb && !existingContent.HasTvDb)
                {
                    var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                 existingContent.Key);

                    GetProviderIds(showMetadata, existingContent);

                    await Repo.Update(existingContent);
                }

                // Just check the key
                if (existingKey != null)
                {
                    // The rating key is all good!
                }
                else
                {
                    // This means the rating key has changed somehow.
                    // Should probably delete this and get the new one
                    var oldKey = existingContent.Key;
                    Repo.DeleteWithoutSave(existingContent);

                    // Because we have changed the rating key, we need to change all children too
                    var episodeToChange = Repo.GetAllEpisodes().Where(x => x.GrandparentKey == oldKey);
                    if (episodeToChange.Any())
                    {
                        foreach (var e in episodeToChange)
                        {
                            Repo.DeleteWithoutSave(e);
                        }
                    }

                    await Repo.SaveChangesAsync();

                    existingContent = null;
                }
            }

            // Also make sure it's not already being processed...
            var alreadyProcessed = contentProcessed.Select(x => x.Value).Any(x => x == show.ratingKey);

            if (alreadyProcessed)
            {
                return;
            }

            // The ratingKey keeps changing...
            //var existingContent = await Repo.GetByKey(show.ratingKey);
            if (existingContent != null)
            {
                try
                {
                    Logger.LogDebug("We already have show {0} checking for new seasons",
                                    existingContent.Title);
                    // Ok so we have it, let's check if there are any new seasons
                    var itemAdded = false;
                    foreach (var season in seasonsContent)
                    {
                        var seasonExists =
                            existingContent.Seasons.FirstOrDefault(x => x.SeasonKey == season.SeasonKey);

                        if (seasonExists != null)
                        {
                            // We already have this season
                            // check if we have the episode
                            //if (episode != null)
                            //{
                            //    var existing = existingContent.Episodes.Any(x =>
                            //        x.SeasonNumber == episode.parentIndex && x.EpisodeNumber == episode.index);
                            //    if (!existing)
                            //    {
                            //        // We don't have this episode, lets add it
                            //        existingContent.Episodes.Add(new PlexEpisode
                            //        {
                            //            EpisodeNumber = episode.index,
                            //            SeasonNumber = episode.parentIndex,
                            //            GrandparentKey = episode.grandparentRatingKey,
                            //            ParentKey = episode.parentRatingKey,
                            //            Key = episode.ratingKey,
                            //            Title = episode.title
                            //        });
                            //        itemAdded = true;
                            //    }
                            //}
                            continue;
                        }

                        existingContent.Seasons.Add(season);
                        itemAdded = true;
                    }

                    if (itemAdded)
                    {
                        await Repo.Update(existingContent);
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError(LoggingEvents.PlexContentCacher, e,
                                    "Exception when adding new seasons to title {0}", existingContent.Title);
                }
            }
            else
            {
                try
                {
                    Logger.LogDebug("New show {0}, so add it", show.title);

                    // Get the show metadata... This sucks since the `metadata` var contains all information about the show
                    // But it does not contain the `guid` property that we need to pull out thetvdb id...
                    var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                 show.ratingKey);

                    var item = new PlexServerContent
                    {
                        AddedAt     = DateTime.Now,
                        Key         = show.ratingKey,
                        ReleaseYear = show.year.ToString(),
                        Type        = PlexMediaTypeEntity.Show,
                        Title       = show.title,
                        Url         = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, show.ratingKey),
                        Seasons     = new List <PlexSeasonsContent>()
                    };
                    GetProviderIds(showMetadata, item);

                    // Let's just double check to make sure we do not have it now we have some id's
                    var existingImdb      = false;
                    var existingMovieDbId = false;
                    var existingTvDbId    = false;
                    if (item.ImdbId.HasValue())
                    {
                        existingImdb = await Repo.GetAll().AnyAsync(x =>
                                                                    x.ImdbId == item.ImdbId && x.Type == PlexMediaTypeEntity.Show);
                    }

                    if (item.TheMovieDbId.HasValue())
                    {
                        existingMovieDbId = await Repo.GetAll().AnyAsync(x =>
                                                                         x.TheMovieDbId == item.TheMovieDbId && x.Type == PlexMediaTypeEntity.Show);
                    }

                    if (item.TvDbId.HasValue())
                    {
                        existingTvDbId = await Repo.GetAll().AnyAsync(x =>
                                                                      x.TvDbId == item.TvDbId && x.Type == PlexMediaTypeEntity.Show);
                    }

                    if (existingImdb || existingTvDbId || existingMovieDbId)
                    {
                        // We already have it!
                        return;
                    }

                    item.Seasons.ToList().AddRange(seasonsContent);

                    contentToAdd.Add(item);
                }
                catch (Exception e)
                {
                    Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding tv show {0}",
                                    show.title);
                }
            }
        }
Exemple #3
0
        private List <PlexSearch> CachedLibraries(PlexSettings plexSettings)
        {
            var results = new List <PlexSearch>();

            if (!ValidateSettings(plexSettings))
            {
                Log.Warn("The settings are not configured");
                return(results); // don't error out here, just let it go! let it goo!!!
            }

            try
            {
                results = GetLibraries(plexSettings);
                if (plexSettings.AdvancedSearch)
                {
                    Log.Debug("Going through all the items now");
                    Log.Debug($"Item count {results.Count}");
                    foreach (PlexSearch t in results)
                    {
                        foreach (Directory1 t1 in t.Directory)
                        {
                            var currentItem = t1;
                            var metaData    = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                  currentItem.RatingKey);

                            // Get the seasons for each show
                            if (currentItem.Type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
                            {
                                var seasons = PlexApi.GetSeasons(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                 currentItem.RatingKey);

                                // We do not want "all episodes" this as a season
                                var filtered = seasons.Directory.Where(x => !x.Title.Equals("All episodes", StringComparison.CurrentCultureIgnoreCase));

                                t1.Seasons.AddRange(filtered);
                            }

                            var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Directory.Guid);
                            t1.ProviderId = providerId;
                        }
                        foreach (Video t1 in t.Video)
                        {
                            var currentItem = t1;
                            var metaData    = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                  currentItem.RatingKey);
                            var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Video.Guid);
                            t1.ProviderId = providerId;
                        }
                    }
                }
                if (results != null)
                {
                    Log.Debug("done all that, moving onto the DB now");
                    var movies = GetPlexMovies(results);

                    // Time to destroy the plex movies from the DB
                    PlexContent.Custom(connection =>
                    {
                        connection.Open();
                        connection.Query("delete from PlexContent where type = @type", new { type = 0 });
                        return(new List <PlexContent>());
                    });

                    foreach (var m in movies)
                    {
                        if (string.IsNullOrEmpty(m.ProviderId))
                        {
                            Log.Error("Provider Id on movie {0} is null", m.Title);
                            continue;
                        }

                        // Check if it exists
                        var item = PlexContent.Custom(connection =>
                        {
                            connection.Open();
                            var media = connection.QueryFirstOrDefault <PlexContent>("select * from PlexContent where ProviderId = @ProviderId and type = @type", new { m.ProviderId, type = 0 });
                            connection.Dispose();
                            return(media);
                        });

                        if (item == null && !string.IsNullOrEmpty(m.ItemId))
                        {
                            // Doesn't exist, insert it
                            PlexContent.Insert(new PlexContent
                            {
                                ProviderId  = m.ProviderId,
                                ReleaseYear = m.ReleaseYear ?? string.Empty,
                                Title       = m.Title,
                                Type        = Store.Models.Plex.PlexMediaType.Movie,
                                Url         = m.Url,
                                ItemId      = m.ItemId,
                                AddedAt     = DateTime.UtcNow,
                            });
                        }
                    }

                    Log.Debug("Done movies");
                    var tv = GetPlexTvShows(results);
                    // Time to destroy the plex tv from the DB
                    PlexContent.Custom(connection =>
                    {
                        connection.Open();
                        connection.Query("delete from PlexContent where type = @type", new { type = 1 });
                        return(new List <PlexContent>());
                    });
                    foreach (var t in tv)
                    {
                        if (string.IsNullOrEmpty(t.ProviderId))
                        {
                            Log.Error("Provider Id on tv {0} is null", t.Title);
                            continue;
                        }


                        // Check if it exists
                        var item = PlexContent.Custom(connection =>
                        {
                            connection.Open();
                            var media = connection.QueryFirstOrDefault <PlexContent>("select * from PlexContent where ProviderId = @ProviderId and type = @type", new { t.ProviderId, type = 1 });
                            connection.Dispose();
                            return(media);
                        });

                        if (item == null && !string.IsNullOrEmpty(t.ItemId))
                        {
                            PlexContent.Insert(new PlexContent
                            {
                                ProviderId  = t.ProviderId,
                                ReleaseYear = t.ReleaseYear ?? string.Empty,
                                Title       = t.Title,
                                Type        = Store.Models.Plex.PlexMediaType.Show,
                                Url         = t.Url,
                                Seasons     = ByteConverterHelper.ReturnBytes(t.Seasons),
                                ItemId      = t.ItemId,
                                AddedAt     = DateTime.UtcNow,
                            });
                        }
                    }
                    Log.Debug("Done TV");
                    var albums = GetPlexAlbums(results);
                    // Time to destroy the plex movies from the DB
                    PlexContent.Custom(connection =>
                    {
                        connection.Open();
                        connection.Query("delete from PlexContent where type = @type", new { type = 2 });
                        return(new List <PlexContent>());
                    });

                    foreach (var a in albums)
                    {
                        if (string.IsNullOrEmpty(a.ProviderId))
                        {
                            Log.Error("Provider Id on album {0} is null", a.Title);
                            continue;
                        }


                        // Check if it exists
                        var item = PlexContent.Custom(connection =>
                        {
                            connection.Open();
                            var media = connection.QueryFirstOrDefault <PlexContent>("select * from PlexContent where ProviderId = @ProviderId and type = @type", new { a.ProviderId, type = 2 });
                            connection.Dispose();
                            return(media);
                        });

                        if (item == null)
                        {
                            PlexContent.Insert(new PlexContent
                            {
                                ProviderId  = a.ProviderId,
                                ReleaseYear = a.ReleaseYear ?? string.Empty,
                                Title       = a.Title,
                                Type        = Store.Models.Plex.PlexMediaType.Artist,
                                Url         = a.Url,
                                ItemId      = "album",
                                AddedAt     = DateTime.UtcNow,
                            });
                        }
                    }
                    Log.Debug("Done albums");
                }
            }
            catch (Exception ex)
            {
                Log.Debug("Exception:");
                Log.Debug(ex);
                Log.Error(ex, "Failed to obtain Plex libraries");
            }

            return(results);
        }
Exemple #4
0
        private async Task ProcessServer(PlexServers servers, bool recentlyAddedSearch)
        {
            Logger.LogInformation("Getting all content from server {0}", servers.Name);
            var allContent = await GetAllContent(servers, recentlyAddedSearch);

            Logger.LogInformation("We found {0} items", allContent.Count);

            // Let's now process this.
            var contentToAdd = new HashSet <PlexServerContent>();

            foreach (var content in allContent)
            {
                if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    // Process Shows
                    Logger.LogInformation("Processing TV Shows");
                    foreach (var show in content.Metadata ?? new Metadata[] { })
                    {
                        var seasonList = await PlexApi.GetSeasons(servers.PlexAuthToken, servers.FullUri,
                                                                  show.ratingKey);

                        var seasonsContent = new List <PlexSeasonsContent>();
                        foreach (var season in seasonList.MediaContainer.Metadata)
                        {
                            seasonsContent.Add(new PlexSeasonsContent
                            {
                                ParentKey     = season.parentRatingKey,
                                SeasonKey     = season.ratingKey,
                                SeasonNumber  = season.index,
                                PlexContentId = show.ratingKey
                            });
                        }

                        // Do we already have this item?
                        // Let's try and match
                        var existingContent = await Repo.GetFirstContentByCustom(x => x.Title == show.title &&
                                                                                 x.ReleaseYear == show.year.ToString() &&
                                                                                 x.Type == PlexMediaTypeEntity.Show);

                        // Just double check the rating key, since this is our unique constraint
                        var existingKey = await Repo.GetByKey(show.ratingKey);

                        if (existingKey != null)
                        {
                            // Damn son.
                            // Let's check if they match up
                            var doesMatch = show.title.Equals(existingKey.Title,
                                                              StringComparison.CurrentCulture);
                            if (!doesMatch)
                            {
                                // Something f****d up on Plex at somepoint... Damn, rebuild of lib maybe?
                                // Lets delete the matching key
                                await Repo.Delete(existingKey);

                                existingKey = null;
                            }
                        }

                        if (existingContent != null)
                        {
                            // Just check the key
                            if (existingKey != null)
                            {
                                // The rating key is all good!
                            }
                            else
                            {
                                // This means the rating key has changed somehow.
                                // Should probably delete this and get the new one
                                var oldKey = existingContent.Key;
                                Repo.DeleteWithoutSave(existingContent);

                                // Because we have changed the rating key, we need to change all children too
                                var episodeToChange = Repo.GetAllEpisodes().Where(x => x.GrandparentKey == oldKey);
                                if (episodeToChange.Any())
                                {
                                    foreach (var e in episodeToChange)
                                    {
                                        Repo.DeleteWithoutSave(e);
                                    }
                                }
                                await Repo.SaveChangesAsync();

                                existingContent = null;
                            }
                        }
                        // The ratingKey keeps changing...
                        //var existingContent = await Repo.GetByKey(show.ratingKey);
                        if (existingContent != null)
                        {
                            try
                            {
                                Logger.LogInformation("We already have show {0} checking for new seasons",
                                                      existingContent.Title);
                                // Ok so we have it, let's check if there are any new seasons
                                var itemAdded = false;
                                foreach (var season in seasonsContent)
                                {
                                    var seasonExists =
                                        existingContent.Seasons.FirstOrDefault(x => x.SeasonKey == season.SeasonKey);

                                    if (seasonExists != null)
                                    {
                                        // We already have this season
                                        continue;
                                    }

                                    existingContent.Seasons.Add(season);
                                    itemAdded = true;
                                }

                                if (itemAdded)
                                {
                                    await Repo.Update(existingContent);
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.LogError(LoggingEvents.PlexContentCacher, e,
                                                "Exception when adding new seasons to title {0}", existingContent.Title);
                            }
                        }
                        else
                        {
                            try
                            {
                                Logger.LogInformation("New show {0}, so add it", show.title);

                                // Get the show metadata... This sucks since the `metadata` var contains all information about the show
                                // But it does not contain the `guid` property that we need to pull out thetvdb id...
                                var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                             show.ratingKey);

                                var providerIds =
                                    PlexHelper.GetProviderIdFromPlexGuid(showMetadata.MediaContainer.Metadata.FirstOrDefault()
                                                                         .guid);

                                var item = new PlexServerContent
                                {
                                    AddedAt     = DateTime.Now,
                                    Key         = show.ratingKey,
                                    ReleaseYear = show.year.ToString(),
                                    Type        = PlexMediaTypeEntity.Show,
                                    Title       = show.title,
                                    Url         = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, show.ratingKey),
                                    Seasons     = new List <PlexSeasonsContent>()
                                };
                                if (providerIds.Type == ProviderType.ImdbId)
                                {
                                    item.ImdbId = providerIds.ImdbId;
                                }
                                if (providerIds.Type == ProviderType.TheMovieDbId)
                                {
                                    item.TheMovieDbId = providerIds.TheMovieDb;
                                }
                                if (providerIds.Type == ProviderType.TvDbId)
                                {
                                    item.TvDbId = providerIds.TheTvDb;
                                }

                                // Let's just double check to make sure we do not have it now we have some id's
                                var existingImdb      = false;
                                var existingMovieDbId = false;
                                var existingTvDbId    = false;
                                if (item.ImdbId.HasValue())
                                {
                                    existingImdb = await Repo.GetAll().AnyAsync(x =>
                                                                                x.ImdbId == item.ImdbId && x.Type == PlexMediaTypeEntity.Show);
                                }
                                if (item.TheMovieDbId.HasValue())
                                {
                                    existingMovieDbId = await Repo.GetAll().AnyAsync(x =>
                                                                                     x.TheMovieDbId == item.TheMovieDbId && x.Type == PlexMediaTypeEntity.Show);
                                }
                                if (item.TvDbId.HasValue())
                                {
                                    existingTvDbId = await Repo.GetAll().AnyAsync(x =>
                                                                                  x.TvDbId == item.TvDbId && x.Type == PlexMediaTypeEntity.Show);
                                }
                                if (existingImdb || existingTvDbId || existingMovieDbId)
                                {
                                    // We already have it!
                                    continue;
                                }

                                item.Seasons.ToList().AddRange(seasonsContent);

                                contentToAdd.Add(item);
                            }
                            catch (Exception e)
                            {
                                Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding tv show {0}",
                                                show.title);
                            }
                        }
                        if (contentToAdd.Count > 500)
                        {
                            await Repo.AddRange(contentToAdd);

                            contentToAdd.Clear();
                        }
                    }
                }
                if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    Logger.LogInformation("Processing Movies");
                    foreach (var movie in content?.Metadata ?? new Metadata[] { })
                    {
                        // Let's check if we have this movie

                        try
                        {
                            var existing = await Repo.GetFirstContentByCustom(x => x.Title == movie.title &&
                                                                              x.ReleaseYear == movie.year.ToString() &&
                                                                              x.Type == PlexMediaTypeEntity.Movie);

                            // The rating key keeps changing
                            //var existing = await Repo.GetByKey(movie.ratingKey);
                            if (existing != null)
                            {
                                Logger.LogInformation("We already have movie {0}", movie.title);
                                continue;
                            }

                            var hasSameKey = await Repo.GetByKey(movie.ratingKey);

                            if (hasSameKey != null)
                            {
                                await Repo.Delete(hasSameKey);
                            }

                            Logger.LogInformation("Adding movie {0}", movie.title);
                            var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                     movie.ratingKey);

                            var providerIds = PlexHelper.GetProviderIdFromPlexGuid(metaData.MediaContainer.Metadata
                                                                                   .FirstOrDefault()
                                                                                   .guid);

                            var item = new PlexServerContent
                            {
                                AddedAt     = DateTime.Now,
                                Key         = movie.ratingKey,
                                ReleaseYear = movie.year.ToString(),
                                Type        = PlexMediaTypeEntity.Movie,
                                Title       = movie.title,
                                Url         = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, movie.ratingKey),
                                Seasons     = new List <PlexSeasonsContent>(),
                                Quality     = movie.Media?.FirstOrDefault()?.videoResolution ?? string.Empty
                            };
                            if (providerIds.Type == ProviderType.ImdbId)
                            {
                                item.ImdbId = providerIds.ImdbId;
                            }
                            if (providerIds.Type == ProviderType.TheMovieDbId)
                            {
                                item.TheMovieDbId = providerIds.TheMovieDb;
                            }
                            if (providerIds.Type == ProviderType.TvDbId)
                            {
                                item.TvDbId = providerIds.TheTvDb;
                            }
                            contentToAdd.Add(item);
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new Movie {0}",
                                            movie.title);
                        }

                        if (contentToAdd.Count > 500)
                        {
                            await Repo.AddRange(contentToAdd);

                            contentToAdd.Clear();
                        }
                    }
                }
                if (contentToAdd.Count > 500)
                {
                    await Repo.AddRange(contentToAdd);

                    contentToAdd.Clear();
                }
            }

            if (contentToAdd.Any())
            {
                await Repo.AddRange(contentToAdd);
            }
        }
Exemple #5
0
        private List <PlexSearch> CachedLibraries(PlexSettings plexSettings)
        {
            var results = new List <PlexSearch>();

            if (!ValidateSettings(plexSettings))
            {
                Log.Warn("The settings are not configured");
                return(results); // don't error out here, just let it go! let it goo!!!
            }

            try
            {
                results = GetLibraries(plexSettings);
                if (plexSettings.AdvancedSearch)
                {
                    foreach (PlexSearch t in results)
                    {
                        foreach (Directory1 t1 in t.Directory)
                        {
                            var currentItem = t1;
                            var metaData    = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                  currentItem.RatingKey);

                            // Get the seasons for each show
                            if (currentItem.Type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
                            {
                                var seasons = PlexApi.GetSeasons(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                 currentItem.RatingKey);

                                // We do not want "all episodes" this as a season
                                var filtered = seasons.Directory.Where(x => !x.Title.Equals("All episodes", StringComparison.CurrentCultureIgnoreCase));

                                t1.Seasons.AddRange(filtered);
                            }

                            var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Directory.Guid);
                            t1.ProviderId = providerId;
                        }
                        foreach (Video t1 in t.Video)
                        {
                            var currentItem = t1;
                            var metaData    = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                  currentItem.RatingKey);
                            var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Video.Guid);
                            t1.ProviderId = providerId;
                        }
                    }
                }
                if (results != null)
                {
                    var movies = GetPlexMovies(results);

                    // Time to destroy the plex movies from the DB
                    PlexContent.Custom(connection =>
                    {
                        connection.Open();
                        connection.Query("delete from PlexContent where type = @type", new { type = 0 });
                        return(new List <PlexContent>());
                    });

                    foreach (var m in movies)
                    {
                        PlexContent.Insert(new PlexContent
                        {
                            ProviderId  = m.ProviderId,
                            ReleaseYear = m.ReleaseYear ?? string.Empty,
                            Title       = m.Title,
                            Type        = Store.Models.Plex.PlexMediaType.Movie,
                            Url         = m.Url
                        });
                    }
                    var tv = GetPlexTvShows(results);
                    // Time to destroy the plex tv from the DB
                    PlexContent.Custom(connection =>
                    {
                        connection.Open();
                        connection.Query("delete from PlexContent where type = @type", new { type = 1 });
                        return(new List <PlexContent>());
                    });
                    foreach (var t in tv)
                    {
                        PlexContent.Insert(new PlexContent
                        {
                            ProviderId  = t.ProviderId,
                            ReleaseYear = t.ReleaseYear ?? string.Empty,
                            Title       = t.Title,
                            Type        = Store.Models.Plex.PlexMediaType.Show,
                            Url         = t.Url,
                            Seasons     = ByteConverterHelper.ReturnBytes(t.Seasons)
                        });
                    }

                    var albums = GetPlexAlbums(results);
                    // Time to destroy the plex movies from the DB
                    PlexContent.Custom(connection =>
                    {
                        connection.Open();
                        connection.Query("delete from PlexContent where type = @type", new { type = 2 });
                        return(new List <PlexContent>());
                    });
                    foreach (var a in albums)
                    {
                        PlexContent.Insert(new PlexContent
                        {
                            ProviderId  = a.ProviderId,
                            ReleaseYear = a.ReleaseYear ?? string.Empty,
                            Title       = a.Title,
                            Type        = Store.Models.Plex.PlexMediaType.Artist,
                            Url         = a.Url
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to obtain Plex libraries");
            }

            return(results);
        }
Exemple #6
0
        private async Task StartTheCache(PlexSettings plexSettings)
        {
            foreach (var servers in plexSettings.Servers ?? new List <PlexServers>())
            {
                Logger.LogInformation("Getting all content from server {0}", servers.Name);
                var allContent = await GetAllContent(servers);

                Logger.LogInformation("We found {0} items", allContent.Count);

                // Let's now process this.
                var contentToAdd = new List <PlexServerContent>();
                foreach (var content in allContent)
                {
                    if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Process Shows
                        Logger.LogInformation("Processing TV Shows");
                        foreach (var show in content.Metadata ?? new Metadata[] { })
                        {
                            var seasonList = await PlexApi.GetSeasons(servers.PlexAuthToken, servers.FullUri,
                                                                      show.ratingKey);

                            var seasonsContent = new List <PlexSeasonsContent>();
                            foreach (var season in seasonList.MediaContainer.Metadata)
                            {
                                seasonsContent.Add(new PlexSeasonsContent
                                {
                                    ParentKey     = season.parentRatingKey,
                                    SeasonKey     = season.ratingKey,
                                    SeasonNumber  = season.index,
                                    PlexContentId = show.ratingKey
                                });
                            }

                            // Do we already have this item?
                            // Let's try and match
                            var existingContent = await Repo.GetFirstContentByCustom(x => x.Title == show.title &&
                                                                                     x.ReleaseYear == show.year.ToString() &&
                                                                                     x.Type == PlexMediaTypeEntity.Show);

                            if (existingContent == null)
                            {
                                // Just check the key
                                var hasSameKey = await Repo.GetByKey(show.ratingKey);

                                if (hasSameKey != null)
                                {
                                    existingContent = hasSameKey;
                                }
                            }
                            // The ratingKey keeps changing...
                            //var existingContent = await Repo.GetByKey(show.ratingKey);
                            if (existingContent != null)
                            {
                                try
                                {
                                    Logger.LogInformation("We already have show {0} checking for new seasons", existingContent.Title);
                                    // Ok so we have it, let's check if there are any new seasons
                                    var itemAdded = false;
                                    foreach (var season in seasonsContent)
                                    {
                                        var seasonExists = existingContent.Seasons.FirstOrDefault(x => x.SeasonKey == season.SeasonKey);

                                        if (seasonExists != null)
                                        {
                                            // We already have this season
                                            continue;
                                        }

                                        existingContent.Seasons.Add(season);
                                        itemAdded = true;
                                    }

                                    if (itemAdded)
                                    {
                                        await Repo.Update(existingContent);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.LogError(LoggingEvents.PlexContentCacher, e, "Exception when adding new seasons to title {0}", existingContent.Title);
                                }
                            }
                            else
                            {
                                Logger.LogInformation("New show {0}, so add it", show.title);

                                // Get the show metadata... This sucks since the `metadata` var contains all information about the show
                                // But it does not contain the `guid` property that we need to pull out thetvdb id...
                                var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                             show.ratingKey);

                                var providerIds = PlexHelper.GetProviderIdFromPlexGuid(showMetadata.MediaContainer.Metadata.FirstOrDefault().guid);

                                var item = new PlexServerContent
                                {
                                    AddedAt     = DateTime.Now,
                                    Key         = show.ratingKey,
                                    ReleaseYear = show.year.ToString(),
                                    Type        = PlexMediaTypeEntity.Show,
                                    Title       = show.title,
                                    Url         = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, show.ratingKey),
                                    Seasons     = new List <PlexSeasonsContent>()
                                };
                                if (providerIds.Type == ProviderType.ImdbId)
                                {
                                    item.ImdbId = providerIds.ImdbId;
                                }
                                if (providerIds.Type == ProviderType.TheMovieDbId)
                                {
                                    item.TheMovieDbId = providerIds.TheMovieDb;
                                }
                                if (providerIds.Type == ProviderType.TvDbId)
                                {
                                    item.TvDbId = providerIds.TheTvDb;
                                }

                                item.Seasons.ToList().AddRange(seasonsContent);

                                contentToAdd.Add(item);
                            }
                        }
                    }
                    if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        Logger.LogInformation("Processing Movies");
                        foreach (var movie in content?.Metadata ?? new Metadata[] { })
                        {
                            // Let's check if we have this movie

                            var existing = await Repo.GetFirstContentByCustom(x => x.Title == movie.title &&
                                                                              x.ReleaseYear == movie.year.ToString() &&
                                                                              x.Type == PlexMediaTypeEntity.Movie);

                            // The rating key keeps changing
                            //var existing = await Repo.GetByKey(movie.ratingKey);
                            if (existing != null)
                            {
                                Logger.LogInformation("We already have movie {0}", movie.title);
                                continue;
                            }

                            var hasSameKey = await Repo.GetByKey(movie.ratingKey);

                            if (hasSameKey != null)
                            {
                                await Repo.Delete(hasSameKey);
                            }

                            Logger.LogInformation("Adding movie {0}", movie.title);
                            var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
                                                                     movie.ratingKey);

                            var providerIds = PlexHelper.GetProviderIdFromPlexGuid(metaData.MediaContainer.Metadata
                                                                                   .FirstOrDefault()
                                                                                   .guid);

                            var item = new PlexServerContent
                            {
                                AddedAt     = DateTime.Now,
                                Key         = movie.ratingKey,
                                ReleaseYear = movie.year.ToString(),
                                Type        = PlexMediaTypeEntity.Movie,
                                Title       = movie.title,
                                Url         = PlexHelper.GetPlexMediaUrl(servers.MachineIdentifier, movie.ratingKey),
                                Seasons     = new List <PlexSeasonsContent>(),
                                Quality     = movie.Media?.FirstOrDefault()?.videoResolution ?? string.Empty
                            };
                            if (providerIds.Type == ProviderType.ImdbId)
                            {
                                item.ImdbId = providerIds.ImdbId;
                            }
                            if (providerIds.Type == ProviderType.TheMovieDbId)
                            {
                                item.TheMovieDbId = providerIds.TheMovieDb;
                            }
                            if (providerIds.Type == ProviderType.TvDbId)
                            {
                                item.TvDbId = providerIds.TheTvDb;
                            }
                            contentToAdd.Add(item);
                        }
                    }
                    if (contentToAdd.Count > 500)
                    {
                        await Repo.AddRange(contentToAdd);

                        contentToAdd = new List <PlexServerContent>();
                    }
                }

                if (contentToAdd.Any())
                {
                    await Repo.AddRange(contentToAdd);
                }
            }
        }