sonarr-repo-only/NzbDrone.Core/Providers/EpisodeProvider.cs

55 lines
1.8 KiB
C#
Raw Normal View History

2010-09-28 05:01:54 +00:00
using System.Text.RegularExpressions;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class EpisodeProvider
{
2010-09-28 06:09:24 +00:00
private static readonly Regex ParseRegex = new Regex(@"(?<showName>.*)
2010-09-28 05:01:54 +00:00
(?:
s(?<seasonNumber>\d+)e(?<episodeNumber>\d+)-?e(?<episodeNumber2>\d+)
| s(?<seasonNumber>\d+)e(?<episodeNumber>\d+)
| (?<seasonNumber>\d+)x(?<episodeNumber>\d+)
| (?<airDate>\d{4}.\d{2}.\d{2})
)
(?:
(?<episodeName>.*?)
(?<release>
(?:hdtv|pdtv|xvid|ws|720p|x264|bdrip|dvdrip|dsr|proper)
.*)
| (?<episodeName>.*)
)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
public static Episode Parse(string title)
{
2010-09-28 06:09:24 +00:00
Match match = ParseRegex.Match(title);
2010-09-28 05:01:54 +00:00
2010-09-28 06:09:24 +00:00
if (!match.Success)
return null;
2010-09-28 05:01:54 +00:00
2010-09-28 06:09:24 +00:00
return new Episode {
Season = ParseInt(match.Groups["seasonNumber"].Value),
EpisodeNumber = ParseInt(match.Groups["episodeNumber"].Value),
EpisodeNumber2 = ParseInt(match.Groups["episodeNumber2"].Value),
Title = ReplaceSeparatorChars(match.Groups["episodeName"].Value),
Release = ReplaceSeparatorChars(match.Groups["release"].Value),
Proper = title.Contains("PROPER")
};
2010-09-28 05:01:54 +00:00
}
private static string ReplaceSeparatorChars(string s)
{
2010-09-28 06:09:24 +00:00
if (s == null)
return string.Empty;
2010-09-28 05:01:54 +00:00
return s.Replace('.', ' ').Replace('-', ' ').Replace('_', ' ').Trim();
}
private static int ParseInt(string s)
{
int i;
int.TryParse(s, out i);
return i;
}
}
}