Пример #1
0
        /// <summary>
        /// Gets all the library sections.
        /// If the user has specified only certain libraries then we will only look for those
        /// If they have not set the settings then we will monitor them all
        /// </summary>
        /// <param name="plexSettings">The plex settings.</param>
        /// <param name="recentlyAddedSearch"></param>
        /// <returns></returns>
        private async Task <List <Mediacontainer> > GetAllContent(PlexServers plexSettings, bool recentlyAddedSearch)
        {
            var sections = await PlexApi.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri);

            var libs = new List <Mediacontainer>();

            if (sections != null)
            {
                foreach (var dir in sections.MediaContainer.Directory ?? new List <Directory>())
                {
                    if (plexSettings.PlexSelectedLibraries.Any())
                    {
                        if (plexSettings.PlexSelectedLibraries.Any(x => x.Enabled))
                        {
                            // Only get the enabled libs
                            var keys = plexSettings.PlexSelectedLibraries.Where(x => x.Enabled)
                                       .Select(x => x.Key.ToString()).ToList();
                            if (!keys.Contains(dir.key))
                            {
                                Logger.LogDebug("Lib {0} is not monitored, so skipping", dir.key);
                                // We are not monitoring this lib
                                continue;
                            }
                        }
                    }

                    if (recentlyAddedSearch)
                    {
                        var container = await PlexApi.GetRecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                                       dir.key);

                        if (container != null)
                        {
                            libs.Add(container.MediaContainer);
                        }
                    }
                    else
                    {
                        var lib = await PlexApi.GetLibrary(plexSettings.PlexAuthToken, plexSettings.FullUri, dir.key);

                        if (lib != null)
                        {
                            libs.Add(lib.MediaContainer);
                        }
                    }
                }
            }

            return(libs);
        }
Пример #2
0
        private List <PlexSearch> GetLibraries(PlexSettings plexSettings)
        {
            var sections = PlexApi.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri);

            var libs = new List <PlexSearch>();

            if (sections != null)
            {
                foreach (var dir in sections.Directories ?? new List <Directory>())
                {
                    var lib = PlexApi.GetLibrary(plexSettings.PlexAuthToken, plexSettings.FullUri, dir.Key);
                    if (lib != null)
                    {
                        libs.Add(lib);
                    }
                }
            }

            return(libs);
        }
        private List<PlexSearch> GetLibraries(AuthenticationSettings authSettings, PlexSettings plexSettings)
        {
            var sections = PlexApi.GetLibrarySections(authSettings.PlexAuthToken, plexSettings.FullUri);

            List<PlexSearch> libs = new List<PlexSearch>();
            if (sections != null)
            {
                foreach (var dir in sections.Directories)
                {
                    Log.Trace("Obtaining results from Plex for the following library section: {0}", dir.Title);
                    var lib = PlexApi.GetLibrary(authSettings.PlexAuthToken, plexSettings.FullUri, dir.Key);
                    if (lib != null)
                    {
                        libs.Add(lib);
                    }
                }
            }

            Log.Trace("Returning Plex Libs");
            return libs;
        }
Пример #4
0
        private List <PlexSearch> GetLibraries(PlexSettings plexSettings)
        {
            Log.Debug("Getting Lib sections");
            var sections = PlexApi.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri);

            Log.Debug("Going through sections now");
            var libs = new List <PlexSearch>();

            if (sections != null)
            {
                foreach (var dir in sections.Directories ?? new List <Directory>())
                {
                    var lib = PlexApi.GetLibrary(plexSettings.PlexAuthToken, plexSettings.FullUri, dir.Key);
                    if (lib != null)
                    {
                        Log.Debug("adding lib");
                        libs.Add(lib);
                    }
                }
            }

            return(libs);
        }
Пример #5
0
        public async Task <PlexLibrariesResponse> GetPlexLibraries([FromBody] PlexServers settings)
        {
            try
            {
                var libs = await PlexApi.GetLibrarySections(settings.PlexAuthToken, settings.FullUri);

                return(new PlexLibrariesResponse
                {
                    Successful = true,
                    Data = libs
                });
            }
            catch (Exception e)
            {
                _log.LogWarning(e, "Error thrown when attempting to obtain the plex libs");

                var message = e.InnerException != null ? $"{e.Message} - {e.InnerException.Message}" : e.Message;
                return(new PlexLibrariesResponse
                {
                    Successful = false,
                    Message = message
                });
            }
        }
Пример #6
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");
            }
        }