1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-30 19:56:54 +00:00

Refactored quality parsing, should make things easier to work with

This commit is contained in:
Mark McDowall 2013-08-31 19:44:55 -07:00
parent f5da886db6
commit d1de6c1b2c
4 changed files with 412 additions and 231 deletions

View file

@ -12,75 +12,117 @@ namespace NzbDrone.Core.Test.ParserTests
public class QualityParserFixture : CoreTest
{
public static object[] QualityParserCases =
public static object[] SdtvCases =
{
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD", Quality.DVD, false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.X-viD.AC3.-HELLYWOOD", Quality.DVD, false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.AC3.-HELLYWOOD", Quality.DVD, false },
new object[] { "Two.and.a.Half.Men.S08E05.720p.HDTV.X264-DIMENSION", Quality.HDTV720p, false },
new object[] { "Chuck S11E03 has no periods or extension HDTV", Quality.SDTV, false },
new object[] { "Chuck.S04E05.HDTV.XviD-LOL", Quality.SDTV, false },
new object[] { "The.Girls.Next.Door.S03E06.DVDRip.XviD-WiDE", Quality.DVD, false },
new object[] { "The.Girls.Next.Door.S03E06.DVD.Rip.XviD-WiDE", Quality.DVD, false },
new object[] { "The.Girls.Next.Door.S03E06.HDTV-WiDE", Quality.SDTV, false },
new object[] { "Degrassi.S10E27.WS.DSR.XviD-2HD", Quality.SDTV, false },
new object[] { "Sonny.With.a.Chance.S02E15.720p.WEB-DL.DD5.1.H.264-SURFER", Quality.WEBDL720p, false },
new object[] { "Sonny.With.a.Chance.S02E15.720p", Quality.HDTV720p, false },
new object[] { "Sonny.With.a.Chance.S02E15.mkv", Quality.HDTV720p, false },
new object[] { "Sonny.With.a.Chance.S02E15.avi", Quality.SDTV, false },
new object[] { "Sonny.With.a.Chance.S02E15.xvid", Quality.SDTV, false },
new object[] { "Sonny.With.a.Chance.S02E15.divx", Quality.SDTV, false },
new object[] { "Sonny.With.a.Chance.S02E15", Quality.Unknown, false },
new object[] { "Chuck - S01E04 - So Old - Playdate - 720p TV.mkv", Quality.HDTV720p, false },
new object[] { "Chuck - S22E03 - MoneyBART - HD TV.mkv", Quality.HDTV720p, false },
new object[] { "Chuck - S01E03 - Come Fly With Me - 720p BluRay.mkv", Quality.Bluray720p, false },
new object[] { "Chuck - S01E03 - Come Fly With Me - 1080p BluRay.mkv", Quality.Bluray1080p, false },
new object[] { "Chuck - S11E06 - D-Yikes! - 720p WEB-DL.mkv", Quality.WEBDL720p, false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi", Quality.DVD, false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi", Quality.DVD, false },
new object[] { "Law & Order: Special Victims Unit - 11x11 - Quickie", Quality.Unknown, false },
new object[] { "S07E23 - [HDTV-720p].mkv ", Quality.HDTV720p, false },
new object[] { "S07E23 - [WEBDL].mkv ", Quality.WEBDL720p, false },
new object[] { "S07E23.mkv ", Quality.HDTV720p, false },
new object[] { "S07E23 .avi ", Quality.SDTV, false },
new object[] { "WEEDS.S03E01-06.DUAL.XviD.Bluray.AC3.-HELLYWOOD.avi", Quality.DVD, false },
new object[] { "WEEDS.S03E01-06.DUAL.Bluray.AC3.-HELLYWOOD.avi", Quality.Bluray720p, false },
new object[] { "The Voice S01E11 The Finals 1080i HDTV DD5.1 MPEG2-TrollHD", Quality.RAWHD, false },
new object[] { "Nikita S02E01 HDTV XviD 2HD", Quality.SDTV, false },
new object[] { "Gossip Girl S05E11 PROPER HDTV XviD 2HD", Quality.SDTV, true },
new object[] { "The Jonathan Ross Show S02E08 HDTV x264 FTP", Quality.SDTV, false },
new object[] { "White.Van.Man.2011.S02E01.WS.PDTV.x264-TLA", Quality.SDTV, false },
new object[] { "White.Van.Man.2011.S02E01.WS.PDTV.x264-REPACK-TLA", Quality.SDTV, true },
new object[] { "WEEDS.S03E01-06.DUAL.XviD.Bluray.AC3-REPACK.-HELLYWOOD.avi", Quality.DVD, true },
new object[] { "Pawn Stars S04E87 REPACK 720p HDTV x264 aAF", Quality.HDTV720p, true },
new object[] { "The Real Housewives of Vancouver S01E04 DSR x264 2HD", Quality.SDTV, false },
new object[] { "Vanguard S01E04 Mexicos Death Train DSR x264 MiNDTHEGAP", Quality.SDTV, false },
new object[] { "Vanguard S01E04 Mexicos Death Train 720p WEB DL", Quality.WEBDL720p, false },
new object[] { "Hawaii Five 0 S02E21 720p WEB DL DD5 1 H 264", Quality.WEBDL720p, false },
new object[] { "Castle S04E22 720p WEB DL DD5 1 H 264 NFHD", Quality.WEBDL720p, false },
new object[] { "Fringe S04E22 720p WEB-DL DD5.1 H264-EbP.mkv", Quality.WEBDL720p, false },
new object[] { "CSI NY S09E03 1080p WEB DL DD5 1 H264 NFHD", Quality.WEBDL1080p, false },
new object[] { "Two and a Half Men S10E03 1080p WEB DL DD5 1 H 264 NFHD", Quality.WEBDL1080p, false },
new object[] { "Criminal.Minds.S08E01.1080p.WEB-DL.DD5.1.H264-NFHD", Quality.WEBDL1080p, false },
new object[] { "Its.Always.Sunny.in.Philadelphia.S08E01.1080p.WEB-DL.proper.AAC2.0.H.264", Quality.WEBDL1080p, true },
new object[] { "Two and a Half Men S10E03 1080p WEB DL DD5 1 H 264 REPACK NFHD", Quality.WEBDL1080p, true },
new object[] { "Glee.S04E09.Swan.Song.1080p.WEB-DL.DD5.1.H.264-ECI", Quality.WEBDL1080p, false },
new object[] { "Elementary.S01E10.The.Leviathan.480p.WEB-DL.x264-mSD", Quality.WEBDL480p, false },
new object[] { "Glee.S04E10.Glee.Actually.480p.WEB-DL.x264-mSD", Quality.WEBDL480p, false },
new object[] { "The.Big.Bang.Theory.S06E11.The.Santa.Simulation.480p.WEB-DL.x264-mSD", Quality.WEBDL480p, false },
new object[] { "The.Big.Bang.Theory.S06E11.The.Santa.Simulation.1080p.WEB-DL.DD5.1.H.264", Quality.WEBDL1080p, false },
new object[] { "DEXTER.S07E01.ARE.YOU.1080P.HDTV.X264-QCF", Quality.HDTV1080p, false },
new object[] { "DEXTER.S07E01.ARE.YOU.1080P.HDTV.x264-QCF", Quality.HDTV1080p, false },
new object[] { "DEXTER.S07E01.ARE.YOU.1080P.HDTV.proper.X264-QCF", Quality.HDTV1080p, true },
new object[] { "Dexter - S01E01 - Title [HDTV]", Quality.HDTV720p, false },
new object[] { "Dexter - S01E01 - Title [HDTV-720p]", Quality.HDTV720p, false },
new object[] { "Dexter - S01E01 - Title [HDTV-1080p]", Quality.HDTV1080p, false },
new object[] { "POI S02E11 1080i HDTV DD5.1 MPEG2-TrollHD", Quality.RAWHD, false },
new object[] { "How I Met Your Mother S01E18 Nothing Good Happens After 2 A.M. 720p HDTV DD5.1 MPEG2-TrollHD", Quality.RAWHD, false },
new object[] { "Arrested.Development.S04E01.iNTERNAL.1080p.WEBRip.x264-QRUS", Quality.WEBDL1080p, false },
new object[] { "Arrested.Development.S04E01.720p.WEBRip.AAC2.0.x264-NFRiP", Quality.WEBDL720p, false },
new object[] { "Sons.Of.Anarchy.S02E13.1080p.BluRay.x264-AVCDVD", Quality.Bluray1080p, false },
new object[] { "Under the Dome S01E10 Let the Games Begin 1080p", Quality.HDTV1080p, false }
new object[] { "S07E23 .avi ", false },
new object[] {"The.Shield.S01E13.x264-CtrlSD", false},
new object[] { "Nikita S02E01 HDTV XviD 2HD", false },
new object[] { "Gossip Girl S05E11 PROPER HDTV XviD 2HD", true },
new object[] { "The Jonathan Ross Show S02E08 HDTV x264 FTP", false },
new object[] { "White.Van.Man.2011.S02E01.WS.PDTV.x264-TLA", false },
new object[] { "White.Van.Man.2011.S02E01.WS.PDTV.x264-REPACK-TLA", true },
new object[] { "The Real Housewives of Vancouver S01E04 DSR x264 2HD", false },
new object[] { "Vanguard S01E04 Mexicos Death Train DSR x264 MiNDTHEGAP", false },
new object[] { "Chuck S11E03 has no periods or extension HDTV", false },
new object[] { "Chuck.S04E05.HDTV.XviD-LOL", false },
new object[] { "Sonny.With.a.Chance.S02E15.avi", false },
new object[] { "Sonny.With.a.Chance.S02E15.xvid", false },
new object[] { "Sonny.With.a.Chance.S02E15.divx", false },
new object[] { "The.Girls.Next.Door.S03E06.HDTV-WiDE", false },
new object[] { "Degrassi.S10E27.WS.DSR.XviD-2HD", false },
};
public static object[] DvdCases =
{
new object[] { "WEEDS.S03E01-06.DUAL.XviD.Bluray.AC3-REPACK.-HELLYWOOD.avi", true },
new object[] { "The.Shield.S01E13.NTSC.x264-CtrlSD", false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD", false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.X-viD.AC3.-HELLYWOOD", false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.AC3.-HELLYWOOD", false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi", false },
new object[] { "WEEDS.S03E01-06.DUAL.BDRip.XviD.AC3.-HELLYWOOD.avi", false },
new object[] { "WEEDS.S03E01-06.DUAL.XviD.Bluray.AC3.-HELLYWOOD.avi", false },
new object[] { "The.Girls.Next.Door.S03E06.DVDRip.XviD-WiDE", false },
new object[] { "The.Girls.Next.Door.S03E06.DVD.Rip.XviD-WiDE", false },
};
public static object[] Webdl480pCases =
{
new object[] { "Elementary.S01E10.The.Leviathan.480p.WEB-DL.x264-mSD", false },
new object[] { "Glee.S04E10.Glee.Actually.480p.WEB-DL.x264-mSD", false },
new object[] { "The.Big.Bang.Theory.S06E11.The.Santa.Simulation.480p.WEB-DL.x264-mSD", false },
};
public static object[] Hdtv720pCases =
{
new object[] { "Dexter - S01E01 - Title [HDTV]", false },
new object[] { "Dexter - S01E01 - Title [HDTV-720p]", false },
new object[] { "Pawn Stars S04E87 REPACK 720p HDTV x264 aAF", true },
new object[] { "Sonny.With.a.Chance.S02E15.720p", false },
new object[] { "S07E23 - [HDTV-720p].mkv ", false },
new object[] { "Chuck - S22E03 - MoneyBART - HD TV.mkv", false },
new object[] { "S07E23.mkv ", false },
new object[] { "Two.and.a.Half.Men.S08E05.720p.HDTV.X264-DIMENSION", false },
new object[] { "Sonny.With.a.Chance.S02E15.mkv", false },
};
public static object[] Hdtv1080pCases =
{
new object[] { "Under the Dome S01E10 Let the Games Begin 1080p", false },
new object[] { "DEXTER.S07E01.ARE.YOU.1080P.HDTV.X264-QCF", false },
new object[] { "DEXTER.S07E01.ARE.YOU.1080P.HDTV.x264-QCF", false },
new object[] { "DEXTER.S07E01.ARE.YOU.1080P.HDTV.proper.X264-QCF", true },
new object[] { "Dexter - S01E01 - Title [HDTV-1080p]", false },
};
public static object[] Webdl720pCases =
{
new object[] { "Arrested.Development.S04E01.720p.WEBRip.AAC2.0.x264-NFRiP", false },
new object[] { "Vanguard S01E04 Mexicos Death Train 720p WEB DL", false },
new object[] { "Hawaii Five 0 S02E21 720p WEB DL DD5 1 H 264", false },
new object[] { "Castle S04E22 720p WEB DL DD5 1 H 264 NFHD", false },
new object[] { "Chuck - S11E06 - D-Yikes! - 720p WEB-DL.mkv", false },
new object[] { "Sonny.With.a.Chance.S02E15.720p.WEB-DL.DD5.1.H.264-SURFER", false },
new object[] { "S07E23 - [WEBDL].mkv ", false },
new object[] { "Fringe S04E22 720p WEB-DL DD5.1 H264-EbP.mkv", false },
};
public static object[] Webdl1080pCases =
{
new object[] { "Arrested.Development.S04E01.iNTERNAL.1080p.WEBRip.x264-QRUS", false },
new object[] { "CSI NY S09E03 1080p WEB DL DD5 1 H264 NFHD", false },
new object[] { "Two and a Half Men S10E03 1080p WEB DL DD5 1 H 264 NFHD", false },
new object[] { "Criminal.Minds.S08E01.1080p.WEB-DL.DD5.1.H264-NFHD", false },
new object[] { "Its.Always.Sunny.in.Philadelphia.S08E01.1080p.WEB-DL.proper.AAC2.0.H.264", true },
new object[] { "Two and a Half Men S10E03 1080p WEB DL DD5 1 H 264 REPACK NFHD", true },
new object[] { "Glee.S04E09.Swan.Song.1080p.WEB-DL.DD5.1.H.264-ECI", false },
new object[] { "The.Big.Bang.Theory.S06E11.The.Santa.Simulation.1080p.WEB-DL.DD5.1.H.264", false },
};
public static object[] Bluray720pCases =
{
new object[] { "WEEDS.S03E01-06.DUAL.Bluray.AC3.-HELLYWOOD.avi", false },
new object[] { "Chuck - S01E03 - Come Fly With Me - 720p BluRay.mkv", false },
};
public static object[] Bluray1080pCases =
{
new object[] { "Chuck - S01E03 - Come Fly With Me - 1080p BluRay.mkv", false },
new object[] { "Sons.Of.Anarchy.S02E13.1080p.BluRay.x264-AVCDVD", false },
};
public static object[] RawCases =
{
new object[] { "POI S02E11 1080i HDTV DD5.1 MPEG2-TrollHD", false },
new object[] { "How I Met Your Mother S01E18 Nothing Good Happens After 2 A.M. 720p HDTV DD5.1 MPEG2-TrollHD", false },
new object[] { "The Voice S01E11 The Finals 1080i HDTV DD5.1 MPEG2-TrollHD", false },
};
public static object[] UnknownCases =
{
new object[] { "Sonny.With.a.Chance.S02E15", false },
new object[] { "Law & Order: Special Victims Unit - 11x11 - Quickie", false },
};
public static object[] SelfQualityParserCases =
@ -95,20 +137,100 @@ namespace NzbDrone.Core.Test.ParserTests
new object[] { Quality.Bluray1080p }
};
[Test, TestCaseSource("QualityParserCases")]
public void quality_parse(string postTitle, Quality quality, bool proper)
[Test, TestCaseSource("SdtvCases")]
public void should_parse_sdtv_quality(string postTitle, bool proper)
{
var result = Parser.Parser.ParseTitle(postTitle);
result.Quality.Quality.Should().Be(quality);
result.Quality.Proper.Should().Be(proper);
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.SDTV);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("DvdCases")]
public void should_parse_dvd_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.DVD);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Webdl480pCases")]
public void should_parse_webdl480p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.WEBDL480p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Hdtv720pCases")]
public void should_parse_hdtv720p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.HDTV720p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Hdtv1080pCases")]
public void should_parse_hdtv1080p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.HDTV1080p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Webdl720pCases")]
public void should_parse_webdl720p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.WEBDL720p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Webdl1080pCases")]
public void should_parse_webdl1080p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.WEBDL1080p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Bluray720pCases")]
public void should_parse_bluray720p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.Bluray720p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("Bluray1080pCases")]
public void should_parse_bluray1080p_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.Bluray1080p);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("RawCases")]
public void should_parse_raw_quality(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.RAWHD);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("UnknownCases")]
public void quality_parse(string postTitle, bool proper)
{
var result = Parser.QualityParser.ParseQuality(postTitle);
result.Quality.Should().Be(Quality.Unknown);
result.Proper.Should().Be(proper);
}
[Test, TestCaseSource("SelfQualityParserCases")]
public void parsing_our_own_quality_enum(Quality quality)
{
var fileName = String.Format("My series S01E01 [{0}]", quality);
var result = Parser.Parser.ParseTitle(fileName);
result.Quality.Quality.Should().Be(quality);
var result = Parser.QualityParser.ParseQuality(fileName);
result.Quality.Should().Be(quality);
}
}
}

View file

@ -344,6 +344,7 @@
<Compile Include="Parser\Model\ReportInfo.cs" />
<Compile Include="Parser\Parser.cs" />
<Compile Include="Parser\ParsingService.cs" />
<Compile Include="Parser\QualityParser.cs" />
<Compile Include="Providers\UpdateXemMappingsCommand.cs" />
<Compile Include="Qualities\QualityProfileInUseException.cs" />
<Compile Include="Qualities\QualitySizeRepository.cs" />

View file

@ -119,7 +119,7 @@ namespace NzbDrone.Core.Parser
break;
result.Language = ParseLanguage(title);
result.Quality = ParseQuality(title);
result.Quality = QualityParser.ParseQuality(title);
return result;
}
}
@ -240,161 +240,6 @@ namespace NzbDrone.Core.Parser
return parseResult.SeriesTitle;
}
private static QualityModel ParseQuality(string name)
{
Logger.Trace("Trying to parse quality for {0}", name);
name = name.Trim();
var normalizedName = CleanSeriesTitle(name);
var result = new QualityModel { Quality = Quality.Unknown };
result.Proper = (normalizedName.Contains("proper") || normalizedName.Contains("repack"));
if ((normalizedName.Contains("dvd") && !normalizedName.Contains("avcdvd")) || normalizedName.Contains("bdrip") || normalizedName.Contains("brrip"))
{
result.Quality = Quality.DVD;
return result;
}
if (normalizedName.Contains("xvid") || normalizedName.Contains("divx") || normalizedName.Contains("dsr"))
{
if (normalizedName.Contains("bluray"))
{
result.Quality = Quality.DVD;
return result;
}
result.Quality = Quality.SDTV;
return result;
}
if (normalizedName.Contains("bluray"))
{
if (normalizedName.Contains("720p"))
{
result.Quality = Quality.Bluray720p;
return result;
}
if (normalizedName.Contains("1080p"))
{
result.Quality = Quality.Bluray1080p;
return result;
}
result.Quality = Quality.Bluray720p;
return result;
}
if (normalizedName.Contains("webdl") || normalizedName.Contains("webrip"))
{
if (normalizedName.Contains("1080p"))
{
result.Quality = Quality.WEBDL1080p;
return result;
}
if (normalizedName.Contains("720p"))
{
result.Quality = Quality.WEBDL720p;
return result;
}
if (name.Contains("[WEBDL]"))
{
result.Quality = Quality.WEBDL720p;
return result;
}
result.Quality = Quality.WEBDL480p;
return result;
}
if (normalizedName.Contains("trollhd") || normalizedName.Contains("rawhd"))
{
result.Quality = Quality.RAWHD;
return result;
}
if (normalizedName.Contains("x264") || normalizedName.Contains("h264") || normalizedName.Contains("720p"))
{
if (normalizedName.Contains("1080p"))
{
result.Quality = Quality.HDTV1080p;
return result;
}
result.Quality = Quality.HDTV720p;
return result;
}
//Based on extension
if (result.Quality == Quality.Unknown && !name.ContainsInvalidPathChars())
{
try
{
switch (Path.GetExtension(name).ToLower())
{
case ".avi":
case ".xvid":
case ".divx":
case ".wmv":
case ".mp4":
case ".mpg":
case ".mpeg":
case ".mov":
case ".rm":
case ".rmvb":
case ".flv":
case ".dvr-ms":
case ".ogm":
case ".strm":
{
result.Quality = Quality.SDTV;
break;
}
case ".mkv":
case ".ts":
{
result.Quality = Quality.HDTV720p;
break;
}
}
}
catch (ArgumentException)
{
//Swallow exception for cases where string contains illegal
//path characters.
}
}
if (name.Contains("[HDTV]"))
{
result.Quality = Quality.HDTV720p;
return result;
}
if (normalizedName.Contains("hdtv") && normalizedName.Contains("1080p"))
{
result.Quality = Quality.HDTV1080p;
return result;
}
if ((normalizedName.Contains("sdtv") || normalizedName.Contains("pdtv") ||
(result.Quality == Quality.Unknown && normalizedName.Contains("hdtv"))) &&
!normalizedName.Contains("mpeg"))
{
result.Quality = Quality.SDTV;
return result;
}
if (normalizedName.Contains("1080p"))
{
result.Quality = Quality.HDTV1080p;
return result;
}
return result;
}
private static Language ParseLanguage(string title)
{
var lowerTitle = title.ToLower();

View file

@ -0,0 +1,213 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NLog;
using NzbDrone.Common;
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(@"(?<bluray>BluRay)|
(?<webdl>WEB-DL|WEBDL|WEB\sDL|WEB\-DL|WebRip)|
(?<hdtv>HDTV)|
(?<bdrip>BDRiP)|(?<brrip>BRRip)|(?<dvd>\bDVD\b|DVDRip|NTSC|PAL)|
(?<dsr>WS\sDSR|WS_DSR|WS\.DSR|DSR)|(?<pdtv>PDTV)|(?<sdtv>SDTV)",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
private static readonly Regex ResolutionRegex = new Regex(@"(?<_480p>480p)|(?<_720p>720p)|(?<_1080p>1080p)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex CodecRegex = new Regex(@"(?<x264>x264)|(?<h264>h264)|(?<xvidhd>XvidHD)|(?<xvid>Xvid)|(?<divx>divx)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static QualityModel ParseQuality(string name)
{
Logger.Trace("Trying to parse quality for {0}", name);
name = name.Trim();
var normalizedName = name.CleanSeriesTitle();
var result = new QualityModel { Quality = Quality.Unknown };
result.Proper = (normalizedName.Contains("proper") || normalizedName.Contains("repack"));
if (normalizedName.Contains("trollhd") || normalizedName.Contains("rawhd"))
{
result.Quality = Quality.RAWHD;
return result;
}
var sourceMatch = SourceRegex.Match(name);
var resolution = ParseResolution(name);
var codecRegex = CodecRegex.Match(name);
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["dvd"].Success ||
sourceMatch.Groups["bdrip"].Success ||
sourceMatch.Groups["brrip"].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
{
switch (Path.GetExtension(name).ToLower())
{
case ".avi":
case ".xvid":
case ".divx":
case ".wmv":
case ".mp4":
case ".mpg":
case ".mpeg":
case ".mov":
case ".rm":
case ".rmvb":
case ".flv":
case ".dvr-ms":
case ".ogm":
case ".strm":
{
result.Quality = Quality.SDTV;
break;
}
case ".mkv":
case ".ts":
{
result.Quality = Quality.HDTV720p;
break;
}
}
}
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
}
}