2013-09-01 02:44:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-09-12 06:33:28 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-09-01 02:44:55 +00:00
|
|
|
|
using NzbDrone.Core.Qualities;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Parser
|
|
|
|
|
{
|
|
|
|
|
public class QualityParser
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2014-05-04 11:26:02 +00:00
|
|
|
|
private static readonly Regex SourceRegex = new Regex(@"\b(?:
|
|
|
|
|
(?<bluray>BluRay)|
|
2013-09-01 02:44:55 +00:00
|
|
|
|
(?<webdl>WEB-DL|WEBDL|WEB\sDL|WEB\-DL|WebRip)|
|
|
|
|
|
(?<hdtv>HDTV)|
|
2014-05-04 11:26:02 +00:00
|
|
|
|
(?<bdrip>BDRiP)|
|
|
|
|
|
(?<brrip>BRRip)|
|
|
|
|
|
(?<dvd>DVD|DVDRip|NTSC|PAL|xvidvd)|
|
|
|
|
|
(?<dsr>WS\sDSR|WS_DSR|WS\.DSR|DSR)|
|
|
|
|
|
(?<pdtv>PDTV)|
|
|
|
|
|
(?<sdtv>SDTV)
|
|
|
|
|
)\b",
|
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
|
2013-09-01 02:44:55 +00:00
|
|
|
|
|
2014-05-04 11:26:02 +00:00
|
|
|
|
private static readonly Regex RawHDRegex = new Regex(@"\b(?<rawhd>TrollHD|RawHD)\b",
|
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
2013-09-01 02:44:55 +00:00
|
|
|
|
|
2014-05-04 11:26:02 +00:00
|
|
|
|
private static readonly Regex ProperRegex = new Regex(@"\b(?<proper>proper|repack)\b",
|
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
|
2014-05-25 06:21:53 +00:00
|
|
|
|
private static readonly Regex ResolutionRegex = new Regex(@"\b(?:(?<_480p>480p)|(?<_576p>576p)|(?<_720p>720p)|(?<_1080p>1080p))\b",
|
2014-05-04 11:26:02 +00:00
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
|
|
|
|
|
private static readonly Regex CodecRegex = new Regex(@"\b(?:(?<x264>x264)|(?<h264>h264)|(?<xvidhd>XvidHD)|(?<xvid>Xvid)|(?<divx>divx))\b",
|
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
2013-09-01 02:44:55 +00:00
|
|
|
|
|
|
|
|
|
public static QualityModel ParseQuality(string name)
|
|
|
|
|
{
|
2014-03-13 20:12:42 +00:00
|
|
|
|
Logger.Debug("Trying to parse quality for {0}", name);
|
2013-09-01 02:44:55 +00:00
|
|
|
|
|
|
|
|
|
name = name.Trim();
|
2014-05-04 11:26:02 +00:00
|
|
|
|
var normalizedName = name.Replace('_', ' ').Trim().ToLower();
|
2013-09-01 02:44:55 +00:00
|
|
|
|
var result = new QualityModel { Quality = Quality.Unknown };
|
|
|
|
|
|
2014-05-04 11:26:02 +00:00
|
|
|
|
result.Proper = ProperRegex.IsMatch(normalizedName);
|
|
|
|
|
|
|
|
|
|
if (RawHDRegex.IsMatch(normalizedName))
|
2013-09-01 02:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
result.Quality = Quality.RAWHD;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-05-04 11:26:02 +00:00
|
|
|
|
|
|
|
|
|
var sourceMatch = SourceRegex.Match(normalizedName);
|
|
|
|
|
var resolution = ParseResolution(normalizedName);
|
|
|
|
|
var codecRegex = CodecRegex.Match(normalizedName);
|
2013-09-01 02:44:55 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 20:28:43 +00:00
|
|
|
|
if (resolution == Resolution._480p || resolution == Resolution._576p)
|
2014-05-25 06:21:53 +00:00
|
|
|
|
{
|
|
|
|
|
result.Quality = Quality.DVD;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-01 02:44:55 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 17:13:54 +00:00
|
|
|
|
if (sourceMatch.Groups["bdrip"].Success ||
|
2013-09-01 02:44:55 +00:00
|
|
|
|
sourceMatch.Groups["brrip"].Success)
|
2014-05-02 17:13:54 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
2013-09-01 02:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 06:01:36 +00:00
|
|
|
|
if (normalizedName.Contains("bluray720p"))
|
|
|
|
|
{
|
|
|
|
|
result.Quality = Quality.Bluray720p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (normalizedName.Contains("bluray1080p"))
|
|
|
|
|
{
|
|
|
|
|
result.Quality = Quality.Bluray1080p;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-01 02:44:55 +00:00
|
|
|
|
//Based on extension
|
|
|
|
|
if (result.Quality == Quality.Unknown && !name.ContainsInvalidPathChars())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-12 15:13:20 +00:00
|
|
|
|
result.Quality = MediaFileExtensions.GetQualityForExtension(Path.GetExtension(name));
|
2013-09-01 02:44:55 +00:00
|
|
|
|
}
|
|
|
|
|
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;
|
2014-05-25 06:21:53 +00:00
|
|
|
|
if (match.Groups["_576p"].Success) return Resolution._576p;
|
2013-09-01 02:44:55 +00:00
|
|
|
|
if (match.Groups["_720p"].Success) return Resolution._720p;
|
|
|
|
|
if (match.Groups["_1080p"].Success) return Resolution._1080p;
|
|
|
|
|
|
|
|
|
|
return Resolution.Unknown;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum Resolution
|
|
|
|
|
{
|
|
|
|
|
_480p,
|
2014-05-25 06:21:53 +00:00
|
|
|
|
_576p,
|
2013-09-01 02:44:55 +00:00
|
|
|
|
_720p,
|
|
|
|
|
_1080p,
|
|
|
|
|
Unknown
|
|
|
|
|
}
|
|
|
|
|
}
|