From a49850cc891451a4c49b9c406ba6af96eddb6e56 Mon Sep 17 00:00:00 2001 From: Keivan Date: Mon, 27 Sep 2010 22:01:54 -0700 Subject: [PATCH] refactored Episode, Added Quality Enum --- NzbDrone.Core.Test/Files/Queue.xml | 211 +++++++++++---------- NzbDrone.Core.Test/SabControllerTest.cs | 19 +- NzbDrone.Core/Helpers/Disk.cs | 25 --- NzbDrone.Core/NzbDrone.Core.csproj | 3 +- NzbDrone.Core/Providers/DiskProvider.cs | 6 + NzbDrone.Core/Providers/EpisodeProvider.cs | 68 +++++++ NzbDrone.Core/Providers/HttpProvider.cs | 14 +- NzbDrone.Core/Providers/IHttpProvider.cs | 2 +- NzbDrone.Core/Providers/SabProvider.cs | 8 +- NzbDrone.Core/Providers/SeriesProvider.cs | 3 +- NzbDrone.Core/Repository/Episode.cs | 116 +---------- NzbDrone.Core/Repository/Quality.cs | 35 ++++ 12 files changed, 240 insertions(+), 270 deletions(-) delete mode 100644 NzbDrone.Core/Helpers/Disk.cs create mode 100644 NzbDrone.Core/Providers/EpisodeProvider.cs create mode 100644 NzbDrone.Core/Repository/Quality.cs diff --git a/NzbDrone.Core.Test/Files/Queue.xml b/NzbDrone.Core.Test/Files/Queue.xml index 82afab54f..33e7622ce 100644 --- a/NzbDrone.Core.Test/Files/Queue.xml +++ b/NzbDrone.Core.Test/Files/Queue.xml @@ -1,115 +1,120 @@ -us-en -5c770e3197e4fe763423ee7c392c25d1 -Queued -0 -unknown -0:00:00 -11h - + + us-en + 5c770e3197e4fe763423ee7c392c25d1 + + + Queued + 0 + unknown + 0:00:00 + 11h + - - -770.96 -770.96 MB -Ubuntu Test -Normal -None -770.96 -0 -SABnzbd_nzo_xyr5ak + + + 770.96 + 770.96 MB + Ubuntu Test + Normal + None + 770.96 + 0 + SABnzbd_nzo_xyr5ak -3 -770.96 MB - - -0 -770.96 MB -0 -0 -259.45 -False + 3 + 770.96 MB + + + 0 + 770.96 MB + 0 + 0 + 259.45 + False - -0 -1 -0 -None -anime -apps -books -consoles + + 0 + 1 + 0 + + None + anime + apps + books + consoles -ds-games -emulation -games -misc -movies -music -pda -resources -test + ds-games + emulation + games + misc + movies + music + pda + resources + test -tv -tv-dvd -unknown -wii-games -xbox-dlc -xbox-xbla - -259.45 -770.96 + tv + tv-dvd + unknown + wii-games + xbox-dlc + xbox-xbla + + 259.45 + 770.96 - -157286400 - -C:\Program Files\SABnzbd\interfaces\Plush\templates -True -False -False -True -http://wiki.sabnzbd.org/ -4d + + 157286400 + + C:\Program Files\SABnzbd\interfaces\Plush\templates + True + False + False + True + http://wiki.sabnzbd.org/ + 4d - -0.6.x - - -True -Paused -0 -0 -False - -770.96 MB + + 0.6.x + + + True + Paused + 0 + 0 + False + + 770.96 MB -0B -770.96 -1177.64 -1177.64 - - - - - + 0B + 770.96 + 1177.64 + 1177.64 + + + + + + - - - - - - - - - + + + + + + + + + - - - -0:00:00 - -unknown -0.00 - -0 + + + + 0:00:00 + + unknown + 0.00 + + 0 \ No newline at end of file diff --git a/NzbDrone.Core.Test/SabControllerTest.cs b/NzbDrone.Core.Test/SabControllerTest.cs index 6727e01ce..c86f59689 100644 --- a/NzbDrone.Core.Test/SabControllerTest.cs +++ b/NzbDrone.Core.Test/SabControllerTest.cs @@ -34,7 +34,7 @@ namespace NzbDrone.Core.Test config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority); var http = new Mock(); - http.Setup(s => s.GetRequest("http://192.168.5.55:2222/sabnzbd/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&cat=tv&nzbname=This+is+an+Nzb&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")).Returns("ok"); + http.Setup(s => s.DownloadString("http://192.168.5.55:2222/sabnzbd/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&cat=tv&nzbname=This+is+an+Nzb&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")).Returns("ok"); var target = new SabProvider(config.Object, new Mock().Object, http.Object); @@ -67,7 +67,7 @@ namespace NzbDrone.Core.Test config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority); var http = new Mock(); - http.Setup(s => s.GetRequest("http://192.168.5.55:2222/sabnzbd/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&cat=tv&nzbname=This+is+an+Nzb&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")).Returns("error"); + http.Setup(s => s.DownloadString("http://192.168.5.55:2222/sabnzbd/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&cat=tv&nzbname=This+is+an+Nzb&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")).Returns("error"); var target = new SabProvider(config.Object, new Mock().Object, http.Object); @@ -102,16 +102,15 @@ namespace NzbDrone.Core.Test var http = new Mock(); http.Setup( s => - s.GetRequest( + s.DownloadString( "http://192.168.5.55:2222/sabnzbd/api?mode=queue&output=xml&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) .Returns(new StreamReader(@".\Files\Queue.xml").ReadToEnd()); var target = new SabProvider(config.Object, new Mock().Object, http.Object); Episode episode = new Episode(); - FeedItem item = new FeedItem(); - item.TitleFix = "Ubuntu Test"; - episode.FeedItem = item; + + episode.FileName = "Ubuntu Test"; //Act bool result = target.IsInQueue(episode); @@ -140,16 +139,13 @@ namespace NzbDrone.Core.Test var http = new Mock(); http.Setup( s => - s.GetRequest( + s.DownloadString( "http://192.168.5.55:2222/sabnzbd/api?mode=queue&output=xml&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) .Returns(new StreamReader(@".\Files\QueueEmpty.xml").ReadToEnd()); var target = new SabProvider(config.Object, new Mock().Object, http.Object); Episode episode = new Episode(); - FeedItem item = new FeedItem(); - item.TitleFix = "Ubuntu Test"; - episode.FeedItem = item; //Act bool result = target.IsInQueue(episode); @@ -178,7 +174,7 @@ namespace NzbDrone.Core.Test var http = new Mock(); http.Setup( s => - s.GetRequest( + s.DownloadString( "http://192.168.5.55:2222/sabnzbd/api?mode=queue&output=xml&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")) .Returns(new StreamReader(@".\Files\QueueError.xml").ReadToEnd()); @@ -187,7 +183,6 @@ namespace NzbDrone.Core.Test Episode episode = new Episode(); FeedItem item = new FeedItem(); item.TitleFix = "Ubuntu Test"; - episode.FeedItem = item; //Act bool result = target.IsInQueue(episode); diff --git a/NzbDrone.Core/Helpers/Disk.cs b/NzbDrone.Core/Helpers/Disk.cs deleted file mode 100644 index 4c56b58a6..000000000 --- a/NzbDrone.Core/Helpers/Disk.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NzbDrone.Core.Helpers -{ - - static class Disk - { - - /// - /// Cleans the path. making it a uniform path. - /// this will normalize all different presentations of a single folder. - /// - /// The path. - /// Cleaned Path - public static string CleanPath(string path) - { - if (string.IsNullOrEmpty(path)) throw new ArgumentException("Path can not be null or empty"); - return path.ToLower().Trim('/', '\\', ' '); - } - - } -} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 4e0f4a0e4..34808b3c3 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -136,17 +136,18 @@ + - + diff --git a/NzbDrone.Core/Providers/DiskProvider.cs b/NzbDrone.Core/Providers/DiskProvider.cs index 3773585ab..785cb78ae 100644 --- a/NzbDrone.Core/Providers/DiskProvider.cs +++ b/NzbDrone.Core/Providers/DiskProvider.cs @@ -23,5 +23,11 @@ namespace NzbDrone.Core.Providers } #endregion + + public static string CleanPath(string path) + { + if (string.IsNullOrEmpty(path)) throw new ArgumentException("Path can not be null or empty"); + return path.ToLower().Trim('/', '\\', ' '); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs new file mode 100644 index 000000000..5f3a8bc66 --- /dev/null +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -0,0 +1,68 @@ +using System; +using System.Text.RegularExpressions; +using NzbDrone.Core.Repository; + +namespace NzbDrone.Core.Providers +{ + public class EpisodeProvider + { + private static Regex _parseRegex = + new Regex( + @"(?.*) +(?: + s(?\d+)e(?\d+)-?e(?\d+) +| s(?\d+)e(?\d+) +| (?\d+)x(?\d+) +| (?\d{4}.\d{2}.\d{2}) +) +(?: + (?.*?) + (? + (?:hdtv|pdtv|xvid|ws|720p|x264|bdrip|dvdrip|dsr|proper) + .*) +| (?.*) +)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace); + + + public static Episode Parse(string title) + { + Match match = _parseRegex.Match(title); + + if (!match.Success) + return null; + + 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") + }; + } + + private static string ReplaceSeparatorChars(string s) + { + if (s == null) return string.Empty; + return s.Replace('.', ' ').Replace('-', ' ').Replace('_', ' ').Trim(); + } + + private static int ParseInt(string s) + { + int i; + int.TryParse(s, out i); + return i; + } + + private static DateTime ParseAirDate(string s) + { + DateTime d; + if (DateTime.TryParse(ReplaceSeparatorChars(s).Replace(' ', '-'), out d)) + return d; + return DateTime.MinValue; + } + + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Providers/HttpProvider.cs b/NzbDrone.Core/Providers/HttpProvider.cs index a01156f21..acd2a2d57 100644 --- a/NzbDrone.Core/Providers/HttpProvider.cs +++ b/NzbDrone.Core/Providers/HttpProvider.cs @@ -5,19 +5,9 @@ namespace NzbDrone.Core.Providers { class HttpProvider : IHttpProvider { - public string GetRequest(string request) + public string DownloadString(string request) { - //Get the request and return as String Array - try - { - var webClient = new WebClient(); - return webClient.DownloadString(request); - } - catch (Exception ex) - { - Console.WriteLine(ex); - return String.Empty; - } + return new WebClient().DownloadString(request); } } } diff --git a/NzbDrone.Core/Providers/IHttpProvider.cs b/NzbDrone.Core/Providers/IHttpProvider.cs index 8ac6aa43a..00b808ffb 100644 --- a/NzbDrone.Core/Providers/IHttpProvider.cs +++ b/NzbDrone.Core/Providers/IHttpProvider.cs @@ -2,6 +2,6 @@ { public interface IHttpProvider { - string GetRequest(string request); + string DownloadString(string request); } } diff --git a/NzbDrone.Core/Providers/SabProvider.cs b/NzbDrone.Core/Providers/SabProvider.cs index 217991172..04d5a9900 100644 --- a/NzbDrone.Core/Providers/SabProvider.cs +++ b/NzbDrone.Core/Providers/SabProvider.cs @@ -35,7 +35,7 @@ namespace NzbDrone.Core.Providers _logger.DebugFormat("Adding report [{0}] to the queue.", nzbName); - string response = _http.GetRequest(request).Replace("\n", String.Empty); + string response = _http.DownloadString(request).Replace("\n", String.Empty); _logger.DebugFormat("Queue Repsonse: [{0}]", response); if (response == "ok") @@ -48,7 +48,7 @@ namespace NzbDrone.Core.Providers { string action = "mode=queue&output=xml"; string request = GetSabRequest(action); - string response = _http.GetRequest(request); + string response = _http.DownloadString(request); XDocument xDoc = XDocument.Parse(response); @@ -60,9 +60,9 @@ namespace NzbDrone.Core.Providers return false; //Get the Count of Items in Queue where 'filename' is Equal to goodName, if not zero, return true (isInQueue))) - if ((from s in xDoc.Descendants("slot") where s.Element("filename").Value.Equals(epsiode.FeedItem.TitleFix, StringComparison.InvariantCultureIgnoreCase) select s).Count() != 0) + if ((from s in xDoc.Descendants("slot") where s.Element("filename").Value.Equals(epsiode.FileName, StringComparison.InvariantCultureIgnoreCase) select s).Count() != 0) { - _logger.DebugFormat("Episode in queue - '{0}'", epsiode.FeedItem.TitleFix); + _logger.DebugFormat("Episode in queue - '{0}'", epsiode.FileName); return true; } diff --git a/NzbDrone.Core/Providers/SeriesProvider.cs b/NzbDrone.Core/Providers/SeriesProvider.cs index a4cc149d6..791aa8e55 100644 --- a/NzbDrone.Core/Providers/SeriesProvider.cs +++ b/NzbDrone.Core/Providers/SeriesProvider.cs @@ -1,7 +1,6 @@ using System.IO; using System.Linq; using log4net; -using NzbDrone.Core.Helpers; using NzbDrone.Core.Repository; using SubSonic.Repository; using TvdbLib.Data; @@ -44,7 +43,7 @@ namespace NzbDrone.Core.Providers foreach (string seriesFolder in _diskProvider.GetDirectories(_config.SeriesRoot)) { - var cleanPath = Disk.CleanPath(new DirectoryInfo(seriesFolder).FullName); + var cleanPath = DiskProvider.CleanPath(new DirectoryInfo(seriesFolder).FullName); if (!_sonioRepo.Exists(s => s.Path == cleanPath)) { _logger.InfoFormat("Folder '{0} isn't mapped to a series in the database. Trying to map it.'", cleanPath); diff --git a/NzbDrone.Core/Repository/Episode.cs b/NzbDrone.Core/Repository/Episode.cs index 2de356d68..a76efdae3 100644 --- a/NzbDrone.Core/Repository/Episode.cs +++ b/NzbDrone.Core/Repository/Episode.cs @@ -8,120 +8,16 @@ namespace NzbDrone.Core.Repository { public class Episode { - //Information about the current episode being processed - - private const string EpisodePattern = @" -(?.*) -(?: - s(?\d+)e(?\d+)-?e(?\d+) -| s(?\d+)e(?\d+) -| (?\d+)x(?\d+) -| (?\d{4}.\d{2}.\d{2}) -) -(?: - (?.*?) - (? - (?:hdtv|pdtv|xvid|ws|720p|x264|bdrip|dvdrip|dsr|proper) - .*) -| (?.*) -) -"; - public FeedItem FeedItem { get; set; } - public string ShowName { get; set; } - public string EpisodeName { get; set; } - public string EpisodeName2 { get; set; } - public int SeasonNumber { get; set; } + public string SeriesId { get; set; } + public string Title { get; set; } + public string Title2 { get; set; } + public int Season { get; set; } public int EpisodeNumber { get; set; } public int EpisodeNumber2 { get; set; } public DateTime AirDate { get; set; } public string Release { get; set; } public int Quality { get; set; } - - public bool IsProper - { - get { return FeedItem.Title.Contains("PROPER"); } - } - - public bool IsDaily - { - get { return AirDate != DateTime.MinValue; } - } - - public bool IsMulti - { - get { return SeasonNumber != 0 && EpisodeNumber != 0 && EpisodeNumber2 != 0; } - } - - public string EpisodeTitle - { - get { return IsDaily ? GetDailyEpisode() : IsMulti ? GetMultiEpisode() : GetSeasonEpisode(); } - } - - public string Title - { - get { return string.Format("{0} - {1}", ShowName, EpisodeTitle); } - } - - public override string ToString() - { - return string.Format("{0} - {1}", ShowName, EpisodeTitle); - } - - private string GetDailyEpisode() - { - return AirDate.ToString("yyyy-MM-dd"); - } - - private string GetMultiEpisode() - { - return string.Format("{0}x{1:D2}-{0}x{2:D2}", SeasonNumber, EpisodeNumber, EpisodeNumber2); - } - - private string GetSeasonEpisode() - { - return string.Format("{0}x{1:D2}", SeasonNumber, EpisodeNumber); - } - - public static Episode Parse(FeedItem feedItem) - { - Match match = Regex.Match(feedItem.Title, EpisodePattern, - RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase); - - if (!match.Success) - return null; - - return new Episode - { - ShowName = ReplaceSeparatorChars(match.Groups["showName"].Value), - SeasonNumber = ParseInt(match.Groups["seasonNumber"].Value), - EpisodeNumber = ParseInt(match.Groups["episodeNumber"].Value), - EpisodeNumber2 = ParseInt(match.Groups["episodeNumber2"].Value), - EpisodeName = ReplaceSeparatorChars(match.Groups["episodeName"].Value), - Release = ReplaceSeparatorChars(match.Groups["release"].Value) - }; - } - - private static string ReplaceSeparatorChars(string s) - { - if (s == null) return string.Empty; - return s.Replace('.', ' ').Replace('-', ' ').Replace('_', ' ').Trim(); - } - - private static int ParseInt(string s) - { - int i; - int.TryParse(s, out i); - return i; - } - - private static DateTime ParseAirDate(string s) - { - DateTime d; - if (DateTime.TryParse(ReplaceSeparatorChars(s).Replace(' ', '-'), out d)) - return d; - return DateTime.MinValue; - } - - + public bool Proper { get; set; } + public String FileName { get; set; } } } diff --git a/NzbDrone.Core/Repository/Quality.cs b/NzbDrone.Core/Repository/Quality.cs new file mode 100644 index 000000000..b1516f103 --- /dev/null +++ b/NzbDrone.Core/Repository/Quality.cs @@ -0,0 +1,35 @@ +namespace NzbDrone.Core.Repository +{ + // ReSharper disable InconsistentNaming + /// + /// Represents Video Quality + /// + public enum Quality + { + /// + /// Quality is unknown + /// + Unknown = 0, + /// + /// SD File (Source could be HD) + /// + SDTV = 1, + /// + /// SD File (DVD Source) + /// + DVD = 2, + /// + /// HD File (HDTV Source) + /// + HDTV = 3, + /// + /// HD File (Online Source) + /// + WEBDL = 4, + /// + /// HD File (Blu-ray Source) + /// + Bluray = 5 + + } +} \ No newline at end of file