1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-30 15:32:31 +01:00

Better handling of xml errors on tvrage

This commit is contained in:
Mark McDowall 2012-12-22 17:18:05 -08:00
parent 580585dc10
commit 2bd866f590
7 changed files with 152 additions and 55 deletions

View File

@ -12,7 +12,7 @@ using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test
namespace NzbDrone.Core.Test.HelperTests
{
[TestFixture]
// ReSharper disable InconsistentNaming

View File

@ -8,13 +8,14 @@ using FluentAssertions;
using NUnit.Framework;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
using TvdbLib.Data;
using TvdbLib.Exceptions;
namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests
namespace NzbDrone.Core.Test.HelperTests.XElementHelperTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
@ -23,25 +24,26 @@ namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests
[Test]
public void should_return_null_if_xelement_is_null()
{
Mocker.Resolve<TvRageProvider>().ParseDayOfWeek(null).Should().Be(null);
XElement test = null;
test.ConvertToDayOfWeek().Should().Be(null);
}
[Test]
public void should_return_null_if_value_is_null()
{
Mocker.Resolve<TvRageProvider>().ParseDayOfWeek(new XElement("airday", null)).Should().Be(null);
new XElement("airday", null).ConvertToDayOfWeek().Should().Be(null);
}
[Test]
public void should_return_null_if_value_is_empty()
{
Mocker.Resolve<TvRageProvider>().ParseDayOfWeek(new XElement("airday", "")).Should().Be(null);
new XElement("airday", "").ConvertToDayOfWeek().Should().Be(null);
}
[Test]
public void should_return_null_if_value_is_daily()
{
Mocker.Resolve<TvRageProvider>().ParseDayOfWeek(new XElement("airday", "Daily")).Should().Be(null);
new XElement("airday", "Daily").ConvertToDayOfWeek().Should().Be(null);
}
[Test]
@ -59,7 +61,7 @@ namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests
[TestCase("Saturday", DayOfWeek.Saturday)]
public void should_return_dayOfWeek_when_it_is_valid(string value, DayOfWeek expected)
{
Mocker.Resolve<TvRageProvider>().ParseDayOfWeek(new XElement("airday", value)).Should().Be(expected);
new XElement("airday", value).ConvertToDayOfWeek().Should().Be(expected);
}
}
}

View File

@ -0,0 +1,70 @@
// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.HelperTests.XElementHelperTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class XElementHelperTest : CoreTest
{
[Test]
public void Int32_should_return_zero_when_xelement_is_null()
{
XElement test = null;
test.ConvertTo<Int32>().Should().Be(0);
}
[Test]
public void Int32_should_return_zero_when_value_is_null()
{
new XElement("test", null).ConvertTo<Int32>().Should().Be(0);
}
[Test]
public void Int32_should_return_value_when_value_is_an_int()
{
new XElement("test", 10).ConvertTo<Int32>().Should().Be(10);
}
[Test]
public void Nullable_Int32_should_return_null_when_xelement_is_null()
{
XElement test = null;
test.ConvertTo<Nullable<Int32>>().Should().Be(null);
}
[Test]
public void DateTime_should_return_zero_when_xelement_is_null()
{
XElement test = null;
test.ConvertTo<DateTime>().Should().Be(DateTime.MinValue);
}
[Test]
public void DateTime_should_return_zero_when_value_is_null()
{
new XElement("test", null).ConvertTo<DateTime>().Should().Be(DateTime.MinValue);
}
[Test]
public void DateTime_should_return_value_when_value_is_a_date()
{
var date = DateTime.Today;
new XElement("test", date.ToString()).ConvertTo<DateTime>().Should().Be(date);
}
}
}

View File

@ -139,6 +139,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="HelperTests\XElementHelperFixture.cs" />
<Compile Include="JobTests\RenameSeasonJobFixture.cs" />
<Compile Include="ProviderTests\SearchProviderTests\GetSeriesTitleFixture.cs" />
<Compile Include="ProviderTests\TvRageMappingProviderTests\FindMatchingTvRageSeriesFixture.cs" />
@ -207,7 +208,7 @@
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
<Compile Include="Services\ParseErrorServiceFixture.cs" />
<Compile Include="SortHelperTest.cs" />
<Compile Include="HelperTests\SortHelperFixture.cs" />
<Compile Include="ProviderTests\EpisodeProviderTests\EpisodeProviderTest_DeleteInvalidEpisodes.cs" />
<Compile Include="ProviderTests\DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="ProviderTests\QualityTypeProviderTest.cs" />

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace NzbDrone.Core.Helpers
{
public static class XElementHelper
{
public static T ConvertTo<T>(this XElement element)
{
if (element == null)
return default(T);
if (String.IsNullOrEmpty(element.Value))
return default(T);
var converter = TypeDescriptor.GetConverter(typeof(T));
try
{
return (T)converter.ConvertFromString(element.Value);
}
catch
{
return default(T);
}
}
public static DayOfWeek? ConvertToDayOfWeek(this XElement element)
{
if (element == null)
return null;
if (String.IsNullOrWhiteSpace(element.Value))
return null;
try
{
return (DayOfWeek)Enum.Parse(typeof(DayOfWeek), element.Value);
}
catch (Exception)
{
}
return null;
}
}
}

View File

@ -262,6 +262,7 @@
<Compile Include="Helpers\EpisodeSortingHelper.cs" />
<Compile Include="Helpers\SortHelper.cs" />
<Compile Include="Helpers\SabnzbdPriorityTypeConverter.cs" />
<Compile Include="Helpers\XElementHelper.cs" />
<Compile Include="Jobs\CleanupRecycleBinJob.cs" />
<Compile Include="Jobs\XemUpdateJob.cs" />
<Compile Include="Jobs\EmptyRecycleBinJob.cs" />

View File

@ -6,6 +6,7 @@ using System.Xml.Linq;
using NLog;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Model.TvRage;
namespace NzbDrone.Core.Providers
@ -40,27 +41,22 @@ namespace NzbDrone.Core.Providers
try
{
var show = new TvRageSearchResult();
show.ShowId = Int32.Parse(s.Element("showid").Value);
show.ShowId = s.Element("showid").ConvertTo<Int32>();
show.Name = s.Element("name").Value;
show.Link = s.Element("link").Value;
show.Country = s.Element("country").Value;
DateTime started;
if (DateTime.TryParse(s.Element("started").Value, out started)) ;
show.Started = started;
DateTime ended;
if (DateTime.TryParse(s.Element("ended").Value, out ended)) ;
show.Ended = ended;
show.Started = s.Element("started").ConvertTo<DateTime>();
show.Ended = s.Element("ended").ConvertTo<DateTime>();
if (show.Ended < new DateTime(1900, 1, 1))
show.Ended = null;
show.Seasons = Int32.Parse(s.Element("seasons").Value);
show.Seasons = s.Element("seasons").ConvertTo<Int32>();
show.Status = s.Element("status").Value;
show.RunTime = Int32.Parse(s.Element("runtime").Value);
show.AirTime = DateTime.Parse(s.Element("airtime").Value);
show.AirDay = ParseDayOfWeek(s.Element("airday"));
show.RunTime = s.Element("seasons").ConvertTo<Int32>();
show.AirTime = s.Element("seasons").ConvertTo<DateTime>();
show.AirDay = s.Element("airday").ConvertToDayOfWeek();
searchResults.Add(show);
}
@ -89,26 +85,21 @@ namespace NzbDrone.Core.Providers
}
var show = new TvRageSeries();
show.ShowId = Int32.Parse(s.Element("showid").Value);
show.ShowId = s.Element("showid").ConvertTo<Int32>();
show.Name = s.Element("showname").Value;
show.Link = s.Element("showlink").Value;
show.Seasons = Int32.Parse(s.Element("seasons").Value);
show.Started = Int32.Parse(s.Element("started").Value);
show.Seasons = s.Element("seasons").ConvertTo<Int32>();
show.Started = s.Element("started").ConvertTo<Int32>();
DateTime startDate;
if (DateTime.TryParse(s.Element("startdate").Value, out startDate)) ;
show.StartDate = startDate;
DateTime ended;
if (DateTime.TryParse(s.Element("ended").Value, out ended)) ;
show.Ended = ended;
show.StartDate = s.Element("startdate").ConvertTo<DateTime>();
show.Ended = s.Element("ended").ConvertTo<DateTime>();
show.OriginCountry = s.Element("origin_country").Value;
show.Status = s.Element("status").Value;
show.RunTime = Int32.Parse(s.Element("runtime").Value);
show.RunTime = s.Element("runtime").ConvertTo<Int32>();
show.Network = s.Element("network").Value;
show.AirTime = DateTime.Parse(s.Element("airtime").Value);
show.AirDay = ParseDayOfWeek(s.Element("airday"));
show.AirTime = s.Element("airtime").ConvertTo<DateTime>();
show.AirDay = s.Element("airday").ConvertToDayOfWeek();
show.UtcOffset = GetUtcOffset(s.Element("timezone").Value);
return show;
}
@ -139,10 +130,10 @@ namespace NzbDrone.Core.Providers
try
{
var episode = new TvRageEpisode();
episode.EpisodeNumber = Int32.Parse(e.Element("epnum").Value);
episode.SeasonNumber = Int32.Parse(e.Element("seasonnum").Value);
episode.EpisodeNumber = e.Element("epnum").ConvertTo<Int32>();
episode.SeasonNumber = e.Element("seasonnum").ConvertTo<Int32>();
episode.ProductionCode = e.Element("prodnum").Value;
episode.AirDate = DateTime.Parse(e.Element("airdate").Value);
episode.AirDate = e.Element("airdate").ConvertTo<DateTime>();
episode.Link = e.Element("link").Value;
episode.Title = e.Element("title").Value;
episodes.Add(episode);
@ -174,24 +165,5 @@ namespace NzbDrone.Core.Providers
return offset;
}
internal DayOfWeek? ParseDayOfWeek(XElement element)
{
if(element == null)
return null;
if(String.IsNullOrWhiteSpace(element.Value))
return null;
try
{
return (DayOfWeek)Enum.Parse(typeof(DayOfWeek), element.Value);
}
catch(Exception)
{
}
return null;
}
}
}