Example #1
0
        private List <UsenetRelease> GetLatestReleases()
        {
            var knownVideosOnDisk = renameService.GetKnownVideoQualityResults();
            var favoriteSites     = options.DownloadFavoriteSites ? pdbApiService.GetFavoriteSites() : null;
            var favoriteActors    = options.DownloadFavoriteActors ? pdbApiService.GetFavoriteActors() : null;
            var releases          = pdbApiService.GetReleases(1, 1000, 0, 0, "");
            var myIndexers        = pdbApiService.GetMyIndexer();

            if (releases == null || !releases.Any())
            {
                logger.LogError("No releases found");
                return(null);
            }

            var resultList = new List <UsenetRelease>();

            foreach (var release in releases)
            {
                release.VideoQuality = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(release.Title));
                bool matchActor = favoriteActors != null && options.DownloadFavoriteActors && favoriteActors.Any(x => x.Id == release.Actor1 || x.Id == release.Actor2);
                bool matchSite  = favoriteSites != null && options.DownloadFavoriteSites && favoriteSites.Any(x => x.Id == release.Site);

                if (myIndexers.Any(x => x.Id == release.Indexer) && (matchActor || matchSite) && !IsDuplicate(release, knownVideosOnDisk, resultList))
                {
                    logger.LogDebug($"Add {release.Title} to resultlist.");
                    resultList.Add(release);
                }
            }
            logger.LogInformation($"{resultList.Count} releases found.");

            return(resultList);
        }
Example #2
0
        public List <KnownVideoQualityResult> GetKnownVideoQualityResults()
        {
            List <KnownVideoQualityResult> result = new List <KnownVideoQualityResult>();
            string targetFolder = renameServiceOptions.TargetFolder;

            if (!Directory.Exists(targetFolder))
            {
                logger.LogError($"Target directory {targetFolder} does not exist.");
                return(new List <KnownVideoQualityResult>());
            }

            var files = Directory.EnumerateFiles(targetFolder, "*.*", SearchOption.AllDirectories).ToList();

            foreach (var file in files)
            {
                Match match = Regex.Match(file, @"P([0-9]{7})", RegexOptions.IgnoreCase);

                if (match.Success)
                {
                    string pdbIdString = match.Groups[1].Value;
                    bool   parsed      = int.TryParse(pdbIdString, out var pdbId);
                    if (parsed)
                    {
                        FileInfo fi = new FileInfo(file);
                        result.Add(new KnownVideoQualityResult()
                        {
                            VideoId        = pdbId,
                            VideoQualityId = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(fi.Name))?.Id ?? 0
                        });
                    }
                }
            }

            return(result);
        }
Example #3
0
        private Video GetVideoByNormalization(List <Video> videos, string name)
        {
            var prefixString = StringExtractor.ExtractPrefixBeforeQualityAndReleasegroup(name);

            if (!string.IsNullOrEmpty(prefixString))
            {
                var n = StringNormalizer.Normalize(prefixString);
                var foundByNormalizationVideos = new List <Video>();
                foreach (var videoFound in videos)
                {
                    var nFound = StringNormalizer.Normalize(videoFound.FullTitle);

                    if (n.Equals(nFound))
                    {
                        foundByNormalizationVideos.Add(videoFound);
                    }
                }

                if (foundByNormalizationVideos.Count == 1)
                {
                    return(foundByNormalizationVideos[0]);
                }
            }

            return(null);
        }
        private void ProcessBySite(Site site, List <Video> videos, string searchFolder, bool dryRun)
        {
            if (!Directory.Exists(searchFolder))
            {
                logger.LogError($"Directory {searchFolder} does not exist.");
                return;
            }

            var files = Directory.EnumerateFiles(searchFolder, "*.*", SearchOption.TopDirectoryOnly).ToList();

            foreach (var file in files)
            {
                Match match = Regex.Match(file, @"P([0-9]{7})", RegexOptions.IgnoreCase);

                if (!match.Success)
                {
                    continue;
                }

                int  videoQualityId = 0;
                bool parsed         = int.TryParse(match.Groups[1].Value, out var pdbId);
                if (!parsed)
                {
                    continue;
                }

                FileInfo fi = new FileInfo(file);
                videoQualityId = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(fi.Name))?.Id ?? 0;

                var video = videos.SingleOrDefault(x => x.Id == pdbId);
                if (video == null)
                {
                    logger.LogInformation($"Video info for id {pdbId} not found (in videos for site {site.Sitename}).");
                    continue;
                }

                string newFilename = renameService.ReplaceTemplatePlaceholders(renameServiceOptions.FilenameTemplate, new RenamerResult()
                {
                    Video          = video,
                    VideoQualityId = videoQualityId,
                    FileExtension  = fi.Extension,
                    Filename       = fi.Name,
                    Foldername     = searchFolder,
                    Source         = new SourceFile()
                    {
                        Filename      = fi.Name,
                        FileExtension = fi.Extension
                    }
                });

                logger.LogInformation($"Rename {fi.Name} to {newFilename}");
                if (!dryRun)
                {
                    File.Move(Path.Combine(searchFolder, fi.Name), Path.Combine(searchFolder, newFilename));
                }
            }
        }
Example #5
0
        private List <UsenetRelease> GetBackfillingActors(int backFillingActors, List <KnownVideoQualityResult> knownVideosOnDisk, int daysBack)
        {
            var dateDaysBack   = DateTime.Now.AddDays(-1 * daysBack);
            var favoriteActors = new List <Actor>();

            if (backFillingActors == 0)
            {
                favoriteActors.AddRange(pdbApiService.GetFavoriteActors());
            }
            else
            {
                favoriteActors.Add(new Actor()
                {
                    Id = backFillingActors
                });
            }

            var resultList     = new List <UsenetRelease>();
            var myIndexers     = pdbApiService.GetMyIndexer();
            var myIndexersInts = myIndexers.Select(x => x.Id).ToList();

            foreach (var actor in favoriteActors)
            {
                var releases = pdbApiService.GetReleases(1, 5000, actor.Id, 0, "");

                if (releases == null || !releases.Any())
                {
                    logger.LogError("No releases found");
                    continue;
                }

                var foundReleasess = releases.Where(x => (x.Actor1 == actor.Id || x.Actor2 == actor.Id) && myIndexersInts.Contains(x.Indexer) && x.Created >= dateDaysBack);
                foreach (var release in foundReleasess)
                {
                    release.VideoQuality = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(release.Title));

                    if (!IsDuplicate(release, knownVideosOnDisk, resultList))
                    {
                        logger.LogDebug($"Add {release.Title} to resultlist.");
                        resultList.Add(release);
                    }
                }
                logger.LogInformation($"{resultList.Count} releases added for actor id: {actor.Id}.");
            }

            return(resultList);
        }
Example #6
0
        private List <UsenetRelease> GetReleasesToExcludeFromSabnzbd(List <UsenetRelease> releases)
        {
            // All releases found in queue must be excluded from releases list
            List <UsenetRelease> excludeReleases = new List <UsenetRelease>();

            var slots = sabnzbdService.GetQueue(0, 1000).Slots;

            foreach (var slot in slots)
            {
                var videoFound = videoMatching.GetVideoByName(slot.Filename);
                if (videoFound != null)
                {
                    var quality = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(slot.Filename));

                    var releasesFound = releases
                                        .Where(x => x.Video == videoFound.Id && x.VideoQuality.Id == quality.Id).ToList();
                    if (releasesFound != null && releasesFound.Any())
                    {
                        excludeReleases.AddRange(releasesFound);
                    }
                }
            }

            var slotsHistory = sabnzbdService.GetHistory(0, 1000).Slots;

            foreach (var slot in slotsHistory)
            {
                var videoFound = videoMatching.GetVideoByName(slot.Name);
                if (videoFound != null)
                {
                    var quality = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(slot.Name));

                    var releasesFound = releases
                                        .Where(x => x.Video == videoFound.Id && x.VideoQuality.Id == quality.Id).ToList();
                    if (releasesFound != null && releasesFound.Any())
                    {
                        excludeReleases.AddRange(releasesFound);
                    }
                }
            }

            return(excludeReleases);
        }
Example #7
0
        public Video GetVideoByEpisode(List <Video> videos, string name)
        {
            var episodeExtracted = StringExtractor.ExtractEpisode(name);

            if (episodeExtracted != null)
            {
                int foundVideosByEpisodeCount = videos.Count(x => x.FullTitle.Contains(" " + episodeExtracted));
                if (foundVideosByEpisodeCount == 1)
                {
                    return(videos.Single(x => x.FullTitle.Contains(" " + episodeExtracted)));
                }

                var videosFound      = videos.Where(x => x.FullTitle.Contains(" " + episodeExtracted)).ToList();
                var videoFoundResult = GetVideoByNormalization(videosFound, name);
                if (videoFoundResult != null)
                {
                    return(videoFoundResult);
                }
            }

            return(null);
        }
Example #8
0
        public Video GetVideoByDate(List <Video> videos, string name)
        {
            var dateExtracted = StringExtractor.ExtractDate(name);

            if (dateExtracted != null)
            {
                int foundVideosByDateCount = videos.Count(x => x.Releasedate == dateExtracted);
                if (foundVideosByDateCount == 1)
                {
                    return(videos.Single(x => x.Releasedate == dateExtracted));
                }

                var videosFound      = videos.Where(x => x.Releasedate == dateExtracted).ToList();
                var videoFoundResult = GetVideoByNormalization(videosFound, name);
                if (videoFoundResult != null)
                {
                    return(videoFoundResult);
                }
            }

            return(null);
        }
Example #9
0
        public List <RenamerResult> ProcessFiles(List <SourceFile> sourceFiles)
        {
            List <RenamerResult> resultList = new List <RenamerResult>();

            foreach (var sourceFile in sourceFiles)
            {
                var v = new RenamerResult()
                {
                    Source         = sourceFile,
                    Video          = null,
                    VideoQualityId = 0
                };

                if (!sourceFile.IsNoVideoExtension && !sourceFile.IsToSmall)
                {
                    var titleAndSite = GetRelevantTitleAndSite(sourceFile.Filename, sourceFile.Foldername);
                    if (titleAndSite != null)
                    {
                        var title = titleAndSite.Item1;
                        var site  = titleAndSite.Item2;

                        logger.LogDebug($"Next GetVideoByFilename for {title} - Site: {site.Sitename}");
                        Video video = GetVideoByFilename(title, site);
                        logger.LogDebug($"Result GetVideoByFilename: {video?.Title ?? ""}");
                        if (video != null)
                        {
                            v.Video          = video;
                            v.VideoQualityId = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(title))?.Id ?? 0;
                        }
                    }
                }

                resultList.Add(v);
            }

            return(resultList);
        }
Example #10
0
        public Video GetVideoByName(string name)
        {
            if (interalCache.ContainsKey(name))
            {
                return(interalCache[name]);
            }

            var site = FindSiteByFirstWord(StringExtractor.GetSeparator(name), name);

            if (site == null)
            {
                return(null);
            }

            var videos = pdbApi.GetVideosBySite(site);

            var byDate = GetVideoByDate(videos, name);

            if (byDate != null)
            {
                interalCache.Add(name, byDate);
                return(byDate);
            }

            var byEpisode = GetVideoByEpisode(videos, name);

            if (byEpisode != null)
            {
                interalCache.Add(name, byEpisode);
                return(byEpisode);
            }

            var byNormalization = GetVideoByNormalization(videos, name);

            interalCache.Add(name, byNormalization);
            return(byNormalization);
        }
Example #11
0
        private Video GetVideoByFilename(string title, Site site)
        {
            DateTime?releaseDate = StringExtractor.ExtractDate(title);
            string   episode     = StringExtractor.ExtractEpisode(title);

            List <Video> videos = GetVideosBySiteWithCached(site);

            if (releaseDate != null)
            {
                var foundMatchingVideos = videos.Where(x => x.Releasedate == releaseDate.Value).ToList();
                logger.LogDebug("Found videos (by ReleaseDate): " + foundMatchingVideos.Count + " Date: " + releaseDate.Value.ToString("yyyyMMdd"));
                if (!foundMatchingVideos.Any() || foundMatchingVideos.Count >= 6)
                {
                    return(null);
                }
                foreach (var foundMatchingVideo in foundMatchingVideos)
                {
                    logger.LogDebug("Video: " + foundMatchingVideo.Title + " matching: " + title + " ?");

                    if (foundMatchingVideo.Title.Contains(" "))
                    {
                        string firstWord = foundMatchingVideo.Title.Substring(0,
                                                                              foundMatchingVideo.Title.IndexOf(" ", StringComparison.InvariantCulture));
                        if (title.Contains(firstWord))
                        {
                            return(foundMatchingVideo);
                        }
                    }

                    string foundTitle      = foundMatchingVideo.Title.Replace(" ", ".").ToLower();
                    string titleSimplified = title.ToLower().Replace(" ", ".");

                    if (titleSimplified.Contains(foundTitle))
                    {
                        return(foundMatchingVideo);
                    }
                }

                return(null);
            }

            if (!string.IsNullOrEmpty(episode))
            {
                var foundMatchingVideos = videos.Where(x => x.FullTitle.Contains("E" + episode)).ToList();
                logger.LogDebug("Found videos (by Episode): " + foundMatchingVideos.Count + " Episode: E" + episode);
                if (foundMatchingVideos.Any() && foundMatchingVideos.Count < 6)
                {
                    foreach (var foundMatchingVideo in foundMatchingVideos)
                    {
                        var firstwordLength = foundMatchingVideo.Title.IndexOf(" ", StringComparison.InvariantCulture);
                        if (firstwordLength > 0)
                        {
                            var firstWord = foundMatchingVideo.Title.Substring(0, firstwordLength);
                            if (title.Contains(firstWord))
                            {
                                return(foundMatchingVideo);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Example #12
0
        private List <UsenetRelease> GetReleasesToExcludeFromNzbget(List <UsenetRelease> releases)
        {
            // All releases found in queue must be excluded from releases list
            List <UsenetRelease> excludeReleases = new List <UsenetRelease>();

            var queueEntries = nzbgetService.GetQueue();

            if (queueEntries == null || queueEntries.Count == 0)
            {
                return(excludeReleases);
            }

            foreach (var queueEntry in queueEntries)
            {
                var videoFound = videoMatching.GetVideoByName(queueEntry.NzbName);
                if (videoFound != null)
                {
                    var quality = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(queueEntry.NzbName));
                    if (quality == null)
                    {
                        logger.LogError("Quality not found for queue entry {nzbname}.", queueEntry.NzbName);
                        continue;
                    }

                    var releasesFound = releases
                                        .Where(x => x.Video == videoFound.Id && x.VideoQuality.Id == quality.Id).ToList();
                    if (releasesFound != null && releasesFound.Any())
                    {
                        excludeReleases.AddRange(releasesFound);
                    }
                }
            }

            var historyEntries = nzbgetService.GetHistory();

            if (historyEntries == null || historyEntries.Count == 0)
            {
                return(excludeReleases);
            }

            foreach (var historyEntry in historyEntries)
            {
                var videoFound = videoMatching.GetVideoByName(historyEntry.NzbName);
                if (videoFound != null)
                {
                    var quality = videoQualityProdiver.GetByName(StringExtractor.ExtractQuality(historyEntry.NzbName));
                    if (quality == null)
                    {
                        logger.LogError("Quality not found for history entry {nzbname}.", historyEntry.NzbName);
                        continue;
                    }

                    var releasesFound = releases
                                        .Where(x => x.Video == videoFound.Id && x.VideoQuality.Id == quality.Id).ToList();
                    if (releasesFound != null && releasesFound.Any())
                    {
                        excludeReleases.AddRange(releasesFound);
                    }
                }
            }

            return(excludeReleases);
        }