2010-10-21 03:49:23 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
|
{
|
2011-04-10 10:21:00 +02:00
|
|
|
|
public static class Parser
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
private static readonly Regex[] ReportTitleRegex = new[]
|
2011-04-23 10:04:30 +02:00
|
|
|
|
{
|
|
|
|
|
new Regex(@"^(?<title>.+?)?\W?(?<year>\d{4}?)?\W+(?<airyear>\d{4})\W+(?<airmonth>\d{2})\W+(?<airday>\d{2})\W?(?!\\)",
|
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
2011-04-25 17:21:12 +02:00
|
|
|
|
new Regex(@"^(?<title>.*?)?(?:\W?S?(?<season>\d{1,2}(?!\d+))(?:(?:\-|\.|[ex]|\s|\sto\s){1,2}(?<episode>\d{1,2}(?!\d+)))+)+\W?(?!\\)",
|
2011-04-23 10:04:30 +02:00
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
|
|
|
|
new Regex(@"^(?<title>.+?)?\W?(?<year>\d{4}?)?(?:\W(?<season>\d+)(?<episode>\d{2}))+\W?(?!\\)",
|
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
|
|
|
|
//Supports 103/113 naming
|
|
|
|
|
new Regex(@"^(?<title>.*?)?(?:\W?S?(?<season>\d{1,2}(?!\d+))(?:(?:\-|\.|[ex]|\s|to)+(?<episode>\d+))+)+\W?(?!\\)",
|
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled)
|
|
|
|
|
};
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
2011-03-25 05:15:02 +01:00
|
|
|
|
private static readonly Regex[] SeasonReportTitleRegex = new[]
|
2011-04-10 04:44:01 +02:00
|
|
|
|
{
|
|
|
|
|
new Regex(
|
|
|
|
|
@"(?<title>.+?)?\W?(?<year>\d{4}?)?\W(?:S|Season)?\W?(?<season>\d+)(?!\\)",
|
|
|
|
|
RegexOptions.IgnoreCase |
|
|
|
|
|
RegexOptions.Compiled),
|
|
|
|
|
};
|
2011-03-25 05:15:02 +01:00
|
|
|
|
|
2011-04-23 10:04:30 +02:00
|
|
|
|
private static readonly Regex NormalizeRegex = new Regex(@"((^|\W)(a|an|the|and|or|of)($|\W))|\W|\b(?!(?:19\d{2}|20\d{2}))\d+\b",
|
2011-04-10 04:44:01 +02:00
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// Parses a post title into list of episodes it contains
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// </summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// <param name = "title">Title of the report</param>
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// <returns>List of episodes contained to the post</returns>
|
2011-04-04 05:50:12 +02:00
|
|
|
|
internal static EpisodeParseResult ParseEpisodeInfo(string title)
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Parsing string '{0}'", title);
|
|
|
|
|
|
2011-04-04 07:24:19 +02:00
|
|
|
|
foreach (var regex in ReportTitleRegex)
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
2011-04-19 08:33:09 +02:00
|
|
|
|
var simpleTitle = Regex.Replace(title, @"480[i|p]|720[i|p]|1080[i|p]|[x|h]264", String.Empty, RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
|
|
|
|
|
|
var match = regex.Matches(simpleTitle);
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
|
|
|
|
if (match.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
var seriesName = NormalizeTitle(match[0].Groups["title"].Value);
|
2011-04-25 19:48:16 +02:00
|
|
|
|
|
|
|
|
|
var airyear = 0;
|
|
|
|
|
Int32.TryParse(match[0].Groups["airyear"].Value, out airyear);
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
2011-04-20 04:59:28 +02:00
|
|
|
|
EpisodeParseResult parsedEpisode;
|
2011-02-03 21:09:19 +01:00
|
|
|
|
|
2011-04-25 19:48:16 +02:00
|
|
|
|
if (airyear < 1)
|
2011-04-04 05:50:12 +02:00
|
|
|
|
{
|
2011-04-20 04:59:28 +02:00
|
|
|
|
var season = 0;
|
|
|
|
|
Int32.TryParse(match[0].Groups["season"].Value, out season);
|
|
|
|
|
|
|
|
|
|
parsedEpisode = new EpisodeParseResult
|
|
|
|
|
{
|
|
|
|
|
Proper = title.ToLower().Contains("proper"),
|
2011-04-23 00:23:29 +02:00
|
|
|
|
CleanTitle = seriesName,
|
2011-04-20 04:59:28 +02:00
|
|
|
|
SeasonNumber = season,
|
|
|
|
|
Episodes = new List<int>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (Match matchGroup in match)
|
|
|
|
|
{
|
|
|
|
|
var count = matchGroup.Groups["episode"].Captures.Count;
|
|
|
|
|
var first = Convert.ToInt32(matchGroup.Groups["episode"].Captures[0].Value);
|
|
|
|
|
var last = Convert.ToInt32(matchGroup.Groups["episode"].Captures[count - 1].Value);
|
2011-04-19 08:33:09 +02:00
|
|
|
|
|
2011-04-20 04:59:28 +02:00
|
|
|
|
for (int i = first; i <= last; i++)
|
|
|
|
|
{
|
|
|
|
|
parsedEpisode.Episodes.Add(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Try to Parse as a daily show
|
|
|
|
|
if (airyear > 0)
|
2011-04-19 08:33:09 +02:00
|
|
|
|
{
|
2011-04-20 04:59:28 +02:00
|
|
|
|
var airmonth = Convert.ToInt32(match[0].Groups["airmonth"].Value);
|
|
|
|
|
var airday = Convert.ToInt32(match[0].Groups["airday"].Value);
|
|
|
|
|
|
|
|
|
|
parsedEpisode = new EpisodeParseResult
|
|
|
|
|
{
|
|
|
|
|
Proper = title.ToLower().Contains("proper"),
|
2011-04-23 00:23:29 +02:00
|
|
|
|
CleanTitle = seriesName,
|
2011-04-20 04:59:28 +02:00
|
|
|
|
AirDate = new DateTime(airyear, airmonth, airday)
|
|
|
|
|
};
|
2011-04-19 08:33:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 04:59:28 +02:00
|
|
|
|
//Something went wrong with this one... return null
|
|
|
|
|
else
|
|
|
|
|
return null;
|
2011-04-04 05:50:12 +02:00
|
|
|
|
}
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
2011-04-04 06:20:01 +02:00
|
|
|
|
parsedEpisode.Quality = ParseQuality(title);
|
|
|
|
|
|
2011-04-04 05:50:12 +02:00
|
|
|
|
Logger.Trace("Episode Parsed. {0}", parsedEpisode);
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
2011-04-04 06:20:01 +02:00
|
|
|
|
return parsedEpisode;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-25 06:15:23 +02:00
|
|
|
|
Logger.Debug("Unable to parse text into episode info. {0}", title);
|
2011-04-04 06:20:01 +02:00
|
|
|
|
return null;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-25 05:15:02 +01:00
|
|
|
|
/// <summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// Parses a post title into season it contains
|
2011-03-25 05:15:02 +01:00
|
|
|
|
/// </summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// <param name = "title">Title of the report</param>
|
2011-03-25 05:15:02 +01:00
|
|
|
|
/// <returns>Season information contained in the post</returns>
|
|
|
|
|
internal static SeasonParseResult ParseSeasonInfo(string title)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Parsing string '{0}'", title);
|
|
|
|
|
|
|
|
|
|
foreach (var regex in ReportTitleRegex)
|
|
|
|
|
{
|
|
|
|
|
var match = regex.Matches(title);
|
|
|
|
|
|
|
|
|
|
if (match.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
var seriesName = NormalizeTitle(match[0].Groups["title"].Value);
|
2011-04-20 03:20:20 +02:00
|
|
|
|
int year;
|
2011-03-25 05:15:02 +01:00
|
|
|
|
Int32.TryParse(match[0].Groups["year"].Value, out year);
|
|
|
|
|
|
|
|
|
|
if (year < 1900 || year > DateTime.Now.Year + 1)
|
|
|
|
|
{
|
|
|
|
|
year = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var seasonNumber = Convert.ToInt32(match[0].Groups["season"].Value);
|
|
|
|
|
|
|
|
|
|
var result = new SeasonParseResult
|
2011-04-10 04:44:01 +02:00
|
|
|
|
{
|
|
|
|
|
SeriesTitle = seriesName,
|
|
|
|
|
SeasonNumber = seasonNumber,
|
2011-04-20 03:20:20 +02:00
|
|
|
|
Year = year,
|
|
|
|
|
Quality = ParseQuality(title)
|
2011-04-10 04:44:01 +02:00
|
|
|
|
};
|
2011-03-25 05:15:02 +01:00
|
|
|
|
|
2011-04-05 00:46:07 +02:00
|
|
|
|
|
2011-03-25 05:15:02 +01:00
|
|
|
|
Logger.Trace("Season Parsed. {0}", result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null; //Return null
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 09:50:33 +01:00
|
|
|
|
/// <summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// Parses a post title to find the series that relates to it
|
2011-03-03 09:50:33 +01:00
|
|
|
|
/// </summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// <param name = "title">Title of the report</param>
|
2011-03-03 09:50:33 +01:00
|
|
|
|
/// <returns>Normalized Series Name</returns>
|
|
|
|
|
internal static string ParseSeriesName(string title)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Parsing string '{0}'", title);
|
|
|
|
|
|
|
|
|
|
foreach (var regex in ReportTitleRegex)
|
|
|
|
|
{
|
|
|
|
|
var match = regex.Matches(title);
|
|
|
|
|
|
|
|
|
|
if (match.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
var seriesName = NormalizeTitle(match[0].Groups["title"].Value);
|
2011-04-25 19:48:16 +02:00
|
|
|
|
|
2011-03-03 09:50:33 +01:00
|
|
|
|
Logger.Trace("Series Parsed. {0}", seriesName);
|
|
|
|
|
return seriesName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// <summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// Parses proper status out of a report title
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// </summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// <param name = "title">Title of the report</param>
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal static bool ParseProper(string title)
|
|
|
|
|
{
|
|
|
|
|
return title.ToLower().Contains("proper");
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-21 01:29:12 +02:00
|
|
|
|
internal static QualityTypes ParseQuality(string name)
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Trying to parse quality for {0}", name);
|
|
|
|
|
|
|
|
|
|
var result = QualityTypes.Unknown;
|
|
|
|
|
name = name.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
if (name.Contains("dvd"))
|
|
|
|
|
return QualityTypes.DVD;
|
2011-02-03 20:47:51 +01:00
|
|
|
|
|
|
|
|
|
if (name.Contains("bdrip") || name.Contains("brrip"))
|
|
|
|
|
{
|
|
|
|
|
return QualityTypes.BDRip;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
|
if (name.Contains("xvid") || name.Contains("divx"))
|
|
|
|
|
{
|
2011-02-03 20:47:51 +01:00
|
|
|
|
if (name.Contains("bluray"))
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
2011-02-03 20:47:51 +01:00
|
|
|
|
return QualityTypes.BDRip;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
2011-02-03 20:47:51 +01:00
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
|
return QualityTypes.TV;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-03 20:47:51 +01:00
|
|
|
|
if (name.Contains("bluray"))
|
2011-02-01 02:01:49 +01:00
|
|
|
|
{
|
2011-03-27 01:16:50 +01:00
|
|
|
|
if (name.Contains("720p"))
|
|
|
|
|
return QualityTypes.Bluray720;
|
|
|
|
|
|
|
|
|
|
if (name.Contains("1080p"))
|
|
|
|
|
return QualityTypes.Bluray1080;
|
|
|
|
|
|
|
|
|
|
return QualityTypes.Bluray720;
|
2011-02-01 02:01:49 +01:00
|
|
|
|
}
|
2010-10-21 03:49:23 +02:00
|
|
|
|
if (name.Contains("web-dl"))
|
|
|
|
|
return QualityTypes.WEBDL;
|
|
|
|
|
if (name.Contains("x264") || name.Contains("h264") || name.Contains("720p"))
|
|
|
|
|
return QualityTypes.HDTV;
|
|
|
|
|
|
|
|
|
|
//Based on extension
|
2011-04-25 19:48:16 +02:00
|
|
|
|
if (result == QualityTypes.Unknown && Path.HasExtension(name))
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
2011-04-25 19:48:16 +02:00
|
|
|
|
switch (Path.GetExtension(name).ToLower())
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
|
|
|
|
case ".avi":
|
|
|
|
|
case ".xvid":
|
|
|
|
|
case ".wmv":
|
|
|
|
|
{
|
|
|
|
|
result = QualityTypes.TV;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ".mkv":
|
|
|
|
|
{
|
|
|
|
|
result = QualityTypes.HDTV;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Quality Parsed:{0} Title:", result, name);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// Normalizes the title. removing all non-word characters as well as common tokens
|
|
|
|
|
/// such as 'the' and 'and'
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// </summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
|
/// <param name = "title">title</param>
|
2010-10-21 03:49:23 +02:00
|
|
|
|
/// <returns></returns>
|
2011-04-10 10:21:00 +02:00
|
|
|
|
public static string NormalizeTitle(string title)
|
2010-10-21 03:49:23 +02:00
|
|
|
|
{
|
|
|
|
|
return NormalizeRegex.Replace(title, String.Empty).ToLower();
|
|
|
|
|
}
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
2011-04-19 08:37:34 +02:00
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
public static string NormalizePath(string path)
|
|
|
|
|
{
|
2011-04-19 08:37:34 +02:00
|
|
|
|
if (String.IsNullOrWhiteSpace(path))
|
2010-10-24 09:46:58 +02:00
|
|
|
|
throw new ArgumentException("Path can not be null or empty");
|
2010-11-06 17:01:46 +01:00
|
|
|
|
|
2011-02-03 20:47:51 +01:00
|
|
|
|
var info = new FileInfo(path);
|
|
|
|
|
|
|
|
|
|
if (info.FullName.StartsWith(@"\\")) //UNC
|
|
|
|
|
{
|
2011-03-10 01:44:21 +01:00
|
|
|
|
return info.FullName.TrimEnd('/', '\\', ' ');
|
2011-02-03 20:47:51 +01:00
|
|
|
|
}
|
2010-11-06 17:01:46 +01:00
|
|
|
|
|
2011-03-10 01:44:21 +01:00
|
|
|
|
return info.FullName.Trim('/', '\\', ' ');
|
2010-10-24 09:46:58 +02:00
|
|
|
|
}
|
2010-10-21 03:49:23 +02:00
|
|
|
|
}
|
2011-04-25 20:24:11 +02:00
|
|
|
|
} |