using System; using System.IO; using System.Text.RegularExpressions; using NLog; using NzbDrone.Common; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Qualities; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Parser { public class QualityParser { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Regex SourceRegex = new Regex(@"\b(?: (?BluRay)| (?WEB-DL|WEBDL|WEB\sDL|WEB\-DL|WebRip)| (?HDTV)| (?BDRiP)| (?BRRip)| (?DVD|DVDRip|NTSC|PAL|xvidvd)| (?WS\sDSR|WS_DSR|WS\.DSR|DSR)| (?PDTV)| (?SDTV) )\b", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); private static readonly Regex RawHDRegex = new Regex(@"\b(?TrollHD|RawHD)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex ProperRegex = new Regex(@"\b(?proper|repack)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex ResolutionRegex = new Regex(@"\b(?:(?<_480p>480p)|(?<_720p>720p)|(?<_1080p>1080p))\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex CodecRegex = new Regex(@"\b(?:(?x264)|(?h264)|(?XvidHD)|(?Xvid)|(?divx))\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static QualityModel ParseQuality(string name) { Logger.Debug("Trying to parse quality for {0}", name); name = name.Trim(); var normalizedName = name.Replace('_', ' ').Trim().ToLower(); var result = new QualityModel { Quality = Quality.Unknown }; result.Proper = ProperRegex.IsMatch(normalizedName); if (RawHDRegex.IsMatch(normalizedName)) { result.Quality = Quality.RAWHD; return result; } var sourceMatch = SourceRegex.Match(normalizedName); var resolution = ParseResolution(normalizedName); var codecRegex = CodecRegex.Match(normalizedName); if (sourceMatch.Groups["bluray"].Success) { if (codecRegex.Groups["xvid"].Success || codecRegex.Groups["divx"].Success) { result.Quality = Quality.DVD; return result; } if (resolution == Resolution._1080p) { result.Quality = Quality.Bluray1080p; return result; } result.Quality = Quality.Bluray720p; return result; } if (sourceMatch.Groups["webdl"].Success) { if (resolution == Resolution._1080p) { result.Quality = Quality.WEBDL1080p; return result; } if (resolution == Resolution._720p) { result.Quality = Quality.WEBDL720p; return result; } if (name.Contains("[WEBDL]")) { result.Quality = Quality.WEBDL720p; return result; } result.Quality = Quality.WEBDL480p; return result; } if (sourceMatch.Groups["hdtv"].Success) { if (resolution == Resolution._1080p) { result.Quality = Quality.HDTV1080p; return result; } if (resolution == Resolution._720p) { result.Quality = Quality.HDTV720p; return result; } if (name.Contains("[HDTV]")) { result.Quality = Quality.HDTV720p; return result; } result.Quality = Quality.SDTV; return result; } if (sourceMatch.Groups["bdrip"].Success || sourceMatch.Groups["brrip"].Success) { if (resolution == Resolution._720p) { result.Quality = Quality.Bluray720p; return result; } else if (resolution == Resolution._1080p) { result.Quality = Quality.Bluray1080p; return result; } else { result.Quality = Quality.DVD; return result; } } if (sourceMatch.Groups["dvd"].Success) { result.Quality = Quality.DVD; return result; } if (sourceMatch.Groups["pdtv"].Success || sourceMatch.Groups["sdtv"].Success || sourceMatch.Groups["dsr"].Success) { result.Quality = Quality.SDTV; return result; } if (resolution == Resolution._1080p) { result.Quality = Quality.HDTV1080p; return result; } if (resolution == Resolution._720p) { result.Quality = Quality.HDTV720p; return result; } if (codecRegex.Groups["x264"].Success) { result.Quality = Quality.SDTV; return result; } //Based on extension if (result.Quality == Quality.Unknown && !name.ContainsInvalidPathChars()) { try { result.Quality = MediaFileExtensions.GetQualityForExtension(Path.GetExtension(name)); } catch (ArgumentException) { //Swallow exception for cases where string contains illegal //path characters. } } return result; } private static Resolution ParseResolution(string name) { var match = ResolutionRegex.Match(name); if (!match.Success) return Resolution.Unknown; if (match.Groups["_480p"].Success) return Resolution._480p; if (match.Groups["_720p"].Success) return Resolution._720p; if (match.Groups["_1080p"].Success) return Resolution._1080p; return Resolution.Unknown; } } public enum Resolution { _480p, _720p, _1080p, Unknown } }