Пример #1
0
        public void CheckAndUpdateAll(long check)
        {
            Log.Trace("This is check no. {0}", check);
            Log.Trace("Getting the settings");
            var plexSettings = Plex.GetSettings();
            var authSettings = Auth.GetSettings();

            Log.Trace("Getting all the requests");

            if (!ValidateSettings(plexSettings, authSettings))
            {
                Log.Info("Validation of the plex settings failed.");
                return;
            }

            var libraries = CachedLibraries(authSettings, plexSettings, true); //force setting the cache (10 min intervals via scheduler)
            var movies    = GetPlexMovies().ToArray();
            var shows     = GetPlexTvShows().ToArray();
            var albums    = GetPlexAlbums().ToArray();

            var requests        = RequestService.GetAll();
            var requestedModels = requests as RequestedModel[] ?? requests.Where(x => !x.Available).ToArray();

            Log.Trace("Requests Count {0}", requestedModels.Length);

            if (!requestedModels.Any())
            {
                Log.Info("There are no requests to check.");
                return;
            }

            var modifiedModel = new List <RequestedModel>();

            foreach (var r in requestedModels)
            {
                Log.Trace("We are going to see if Plex has the following title: {0}", r.Title);

                if (libraries == null)
                {
                    libraries = new List <PlexSearch>()
                    {
                        PlexApi.SearchContent(authSettings.PlexAuthToken, r.Title, plexSettings.FullUri)
                    };
                    if (libraries == null)
                    {
                        Log.Trace("Could not find any matching result for this title.");
                        continue;
                    }
                }

                Log.Trace("Search results from Plex for the following request: {0}", r.Title);
                //Log.Trace(results.DumpJson());

                var releaseDate = r.ReleaseDate == DateTime.MinValue ? string.Empty : r.ReleaseDate.ToString("yyyy");

                bool matchResult;
                switch (r.Type)
                {
                case RequestType.Movie:
                    matchResult = IsMovieAvailable(movies, r.Title, releaseDate);
                    break;

                case RequestType.TvShow:
                    matchResult = IsTvShowAvailable(shows, r.Title, releaseDate);
                    break;

                case RequestType.Album:
                    matchResult = IsAlbumAvailable(albums, r.Title, r.ReleaseDate.Year.ToString(), r.ArtistName);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (matchResult)
                {
                    r.Available = true;
                    modifiedModel.Add(r);
                    continue;
                }

                Log.Trace("The result from Plex where the title's match was null, so that means the content is not yet in Plex.");
            }

            Log.Trace("Updating the requests now");
            Log.Trace("Requests that will be updates:");
            Log.Trace(modifiedModel.SelectMany(x => x.Title).DumpJson());

            if (modifiedModel.Any())
            {
                RequestService.BatchUpdate(modifiedModel);
            }
        }