private async Task <ProcessedContent> StartTheCache(PlexSettings plexSettings, bool recentlyAddedSearch) { var processedContent = new ProcessedContent(); foreach (var servers in plexSettings.Servers ?? new List <PlexServers>()) { try { Logger.LogInformation("Starting to cache the content on server {0}", servers.Name); if (recentlyAddedSearch) { // If it's recently added search then we want the results to pass to the metadata job // This way the metadata job is smaller in size to process, it only need to look at newly added shit return(await ProcessServer(servers, true)); } else { await ProcessServer(servers, false); } } catch (Exception e) { Logger.LogWarning(LoggingEvents.PlexContentCacher, e, "Exception thrown when attempting to cache the Plex Content in server {0}", servers.Name); } } return(processedContent); }
public static bool SaveContent(string file, ProcessedContent content) { ContentTypes type = ContentTypes.Unknown; if (content is Game) { type = ContentTypes.Game; } return new SGDEProcessor().Write(new Content(null, type, SGDEImport.MAX_VERSION), content, file); }
public async Task Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; var recentlyAddedSearch = dataMap.GetBooleanValueFromString("recentlyAddedSearch"); var plexSettings = await Plex.GetSettingsAsync(); if (!plexSettings.Enable) { return; } if (!ValidateSettings(plexSettings)) { Logger.LogError("Plex Settings are not valid"); return; } var processedContent = new ProcessedContent(); Logger.LogInformation("Starting Plex Content Cacher"); try { if (recentlyAddedSearch) { processedContent = await StartTheCache(plexSettings, true); } else { await StartTheCache(plexSettings, false); } } catch (Exception e) { Logger.LogWarning(LoggingEvents.PlexContentCacher, e, "Exception thrown when attempting to cache the Plex Content"); } if (!recentlyAddedSearch) { Logger.LogInformation("Starting EP Cacher"); await OmbiQuartz.TriggerJob(nameof(IPlexEpisodeSync), "Plex"); } if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) { // Just check what we send it await OmbiQuartz.TriggerJob(nameof(IRefreshMetadata), "System"); } if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) { await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex"); } Logger.LogInformation("Finished Plex Content Cacher, with processed content: {0}, episodes: {0}", processedContent.Content.Count(), processedContent.Episodes.Count()); }
public async Task CacheContent(bool recentlyAddedSearch = false) { var plexSettings = await Plex.GetSettingsAsync(); if (!plexSettings.Enable) { return; } if (!ValidateSettings(plexSettings)) { Logger.LogError("Plex Settings are not valid"); return; } var processedContent = new ProcessedContent(); Logger.LogInformation("Starting Plex Content Cacher"); try { if (recentlyAddedSearch) { processedContent = await StartTheCache(plexSettings, true); } else { await StartTheCache(plexSettings, false); } } catch (Exception e) { Logger.LogWarning(LoggingEvents.PlexContentCacher, e, "Exception thrown when attempting to cache the Plex Content"); } if (!recentlyAddedSearch) { Logger.LogInformation("Starting EP Cacher"); BackgroundJob.Enqueue(() => EpisodeSync.Start()); } if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) { // Just check what we send it BackgroundJob.Enqueue(() => Metadata.ProcessPlexServerContent(processedContent.Content)); } if ((processedContent?.HasProcessedEpisodes ?? false) && recentlyAddedSearch) { BackgroundJob.Enqueue(() => Checker.Start()); } }
private async Task <ProcessedContent> ProcessServer(PlexServers servers, bool recentlyAddedSearch) { var retVal = new ProcessedContent(); var contentProcessed = new Dictionary <int, int>(); var episodesProcessed = new List <int>(); Logger.LogDebug("Getting all content from server {0}", servers.Name); var allContent = await GetAllContent(servers, recentlyAddedSearch); Logger.LogDebug("We found {0} items", allContent.Count); // Let's now process this. var contentToAdd = new HashSet <PlexServerContent>(); var allEps = Repo.GetAllEpisodes(); foreach (var content in allContent) { if (content.viewGroup.Equals(PlexMediaType.Episode.ToString(), StringComparison.CurrentCultureIgnoreCase)) { Logger.LogDebug("Found some episodes, this must be a recently added sync"); var count = 0; foreach (var epInfo in content.Metadata ?? new Metadata[] {}) { count++; var grandParentKey = epInfo.grandparentRatingKey; // Lookup the rating key var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri, grandParentKey); var show = showMetadata.MediaContainer.Metadata.FirstOrDefault(); if (show == null) { continue; } await ProcessTvShow(servers, show, contentToAdd, contentProcessed); if (contentToAdd.Any()) { await Repo.AddRange(contentToAdd, false); if (recentlyAddedSearch) { foreach (var plexServerContent in contentToAdd) { contentProcessed.Add(plexServerContent.Id, plexServerContent.Key); } } contentToAdd.Clear(); } if (count > 200) { await Repo.SaveChangesAsync(); } } // Save just to make sure we don't leave anything hanging await Repo.SaveChangesAsync(); if (content.Metadata != null) { var episodesAdded = await EpisodeSync.ProcessEpsiodes(content.Metadata, allEps); episodesProcessed.AddRange(episodesAdded.Select(x => x.Id)); } } if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase)) { // Process Shows Logger.LogDebug("Processing TV Shows"); var count = 0; foreach (var show in content.Metadata ?? new Metadata[] { }) { count++; await ProcessTvShow(servers, show, contentToAdd, contentProcessed); if (contentToAdd.Any()) { await Repo.AddRange(contentToAdd, false); if (recentlyAddedSearch) { foreach (var plexServerContent in contentToAdd) { contentProcessed.Add(plexServerContent.Id, plexServerContent.Key); } } contentToAdd.Clear(); } if (count > 200) { await Repo.SaveChangesAsync(); } } await Repo.SaveChangesAsync(); } if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase)) { Logger.LogDebug("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.LogDebug("We already have movie {0}", movie.title); continue; } var hasSameKey = await Repo.GetByKey(movie.ratingKey); if (hasSameKey != null) { await Repo.Delete(hasSameKey); } Logger.LogDebug("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); foreach (var c in contentToAdd) { contentProcessed.Add(c.Id, c.Key); } contentToAdd.Clear(); } } } if (contentToAdd.Count > 500) { await Repo.AddRange(contentToAdd); foreach (var c in contentToAdd) { contentProcessed.Add(c.Id, c.Key); } contentToAdd.Clear(); } } if (contentToAdd.Any()) { await Repo.AddRange(contentToAdd); foreach (var c in contentToAdd) { contentProcessed.Add(c.Id, c.Key); } } retVal.Content = contentProcessed.Values; retVal.Episodes = episodesProcessed; return(retVal); }
public async Task Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; var recentlyAddedSearch = dataMap.GetBooleanValueFromString(JobDataKeys.RecentlyAddedSearch); var plexSettings = await Plex.GetSettingsAsync(); if (!plexSettings.Enable) { return; } await NotifyClient(recentlyAddedSearch? "Plex Recently Added Sync Started" : "Plex Content Sync Started"); if (!ValidateSettings(plexSettings)) { Logger.LogError("Plex Settings are not valid"); await NotifyClient(recentlyAddedSearch? "Plex Recently Added Sync, Settings Not Valid" : "Plex Content, Settings Not Valid"); return; } var processedContent = new ProcessedContent(); Logger.LogInformation(recentlyAddedSearch ? "Starting Plex Content Cacher Recently Added Scan" : "Starting Plex Content Cacher"); try { if (recentlyAddedSearch) { processedContent = await StartTheCache(plexSettings, true); } else { await StartTheCache(plexSettings, false); } } catch (Exception e) { await NotifyClient(recentlyAddedSearch? "Plex Recently Added Sync Errored" : "Plex Content Sync Errored"); Logger.LogWarning(LoggingEvents.PlexContentCacher, e, "Exception thrown when attempting to cache the Plex Content"); } if (!recentlyAddedSearch) { await NotifyClient("Plex Sync - Starting Episode Sync"); Logger.LogInformation("Starting EP Cacher"); await OmbiQuartz.TriggerJob(nameof(IPlexEpisodeSync), "Plex"); } if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) { await NotifyClient("Plex Sync - Checking if any requests are now available"); Logger.LogInformation("Kicking off Plex Availability Checker"); await _mediaCacheService.Purge(); await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex"); } var processedCont = processedContent?.Content?.Count() ?? 0; var processedEp = processedContent?.Episodes?.Count() ?? 0; Logger.LogInformation("Finished Plex Content Cacher, with processed content: {0}, episodes: {1}. Recently Added Scan: {2}", processedCont, processedEp, recentlyAddedSearch); await NotifyClient(recentlyAddedSearch?$"Plex Recently Added Sync Finished, We processed {processedCont}, and {processedEp} Episodes" : "Plex Content Sync Finished"); }
private async Task <ProcessedContent> ProcessServer(PlexServers servers, bool recentlyAddedSearch) { var retVal = new ProcessedContent(); var contentProcessed = new Dictionary <int, int>(); var episodesProcessed = new List <int>(); Logger.LogDebug("Getting all content from server {0}", servers.Name); var allContent = await GetAllContent(servers, recentlyAddedSearch); Logger.LogDebug("We found {0} items", allContent.Count); // Let's now process this. var contentToAdd = new HashSet <PlexServerContent>(); var allEps = Repo.GetAllEpisodes(); foreach (var content in allContent.OrderByDescending(x => x.viewGroup)) { Logger.LogDebug($"Got type '{content.viewGroup}' to process"); if (content.viewGroup.Equals(PlexMediaType.Episode.ToString(), StringComparison.InvariantCultureIgnoreCase)) { Logger.LogDebug("Found some episodes, this must be a recently added sync"); var count = 0; foreach (var epInfo in content.Metadata ?? new Metadata[] { }) { count++; var grandParentKey = epInfo.grandparentRatingKey; // Lookup the rating key var showMetadata = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri, grandParentKey); var show = showMetadata.MediaContainer.Metadata.FirstOrDefault(); if (show == null) { continue; } await ProcessTvShow(servers, show, contentToAdd, contentProcessed); if (contentToAdd.Any()) { await Repo.AddRange(contentToAdd, recentlyAddedSearch?true : false); if (recentlyAddedSearch) { foreach (var plexServerContent in contentToAdd) { if (plexServerContent.Id <= 0) { Logger.LogInformation($"Item '{plexServerContent.Title}' has an Plex ID of {plexServerContent.Id} and a Plex Key of {plexServerContent.Key}"); } contentProcessed.Add(plexServerContent.Id, plexServerContent.Key); } } contentToAdd.Clear(); } if (count > 30) { await Repo.SaveChangesAsync(); count = 0; } } // Save just to make sure we don't leave anything hanging await Repo.SaveChangesAsync(); if (content.Metadata != null) { var episodesAdded = await EpisodeSync.ProcessEpsiodes(content.Metadata, allEps); episodesProcessed.AddRange(episodesAdded.Select(x => x.Id)); } } if (content.viewGroup.Equals(PlexMediaType.Show.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // Process Shows Logger.LogDebug("Processing TV Shows"); var count = 0; foreach (var show in content.Metadata ?? new Metadata[] { }) { count++; await ProcessTvShow(servers, show, contentToAdd, contentProcessed); if (contentToAdd.Any()) { await Repo.AddRange(contentToAdd, false); if (recentlyAddedSearch) { foreach (var plexServerContent in contentToAdd) { contentProcessed.Add(plexServerContent.Id, plexServerContent.Key); } } contentToAdd.Clear(); } if (count > 30) { await Repo.SaveChangesAsync(); } } await Repo.SaveChangesAsync(); } if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.InvariantCultureIgnoreCase)) { await MovieLoop(servers, content, contentToAdd, contentProcessed); } if (contentToAdd.Count > 500) { await Repo.AddRange(contentToAdd); foreach (var c in contentToAdd) { contentProcessed.Add(c.Id, c.Key); } contentToAdd.Clear(); } } if (contentToAdd.Any()) { await Repo.AddRange(contentToAdd); foreach (var c in contentToAdd) { contentProcessed.Add(c.Id, c.Key); } } retVal.Content = contentProcessed.Values; retVal.Episodes = episodesProcessed; return(retVal); }
public static bool Write(double version, XmlNode parent, ProcessedContent content) { SGDeContent.DataTypes.Game game = content as SGDeContent.DataTypes.Game; if (game == null) { return false; } //Write out the game resources XmlElement element1 = parent.OwnerDocument.CreateElement(ContentTagManager.GetTagValue("IMPORT_GAME_ELEMENT", version)); parent.AppendChild(element1); //TODO //Write out the game settings element1 = parent.OwnerDocument.CreateElement(ContentTagManager.GetTagValue("GAME_SETTINGS", version)); parent.AppendChild(element1); //TODO return true; }