1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 01:57:21 +00: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.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test namespace NzbDrone.Core.Test.HelperTests
{ {
[TestFixture] [TestFixture]
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming

View file

@ -8,13 +8,14 @@
using NUnit.Framework; using NUnit.Framework;
using Ninject; using Ninject;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using TvdbLib.Data; using TvdbLib.Data;
using TvdbLib.Exceptions; using TvdbLib.Exceptions;
namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests namespace NzbDrone.Core.Test.HelperTests.XElementHelperTests
{ {
[TestFixture] [TestFixture]
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
@ -23,25 +24,26 @@ public class ParseDayOfWeekFixture : CoreTest
[Test] [Test]
public void should_return_null_if_xelement_is_null() 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] [Test]
public void should_return_null_if_value_is_null() 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] [Test]
public void should_return_null_if_value_is_empty() 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] [Test]
public void should_return_null_if_value_is_daily() 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] [Test]
@ -59,7 +61,7 @@ public void should_return_null_if_value_is_weekdays()
[TestCase("Saturday", DayOfWeek.Saturday)] [TestCase("Saturday", DayOfWeek.Saturday)]
public void should_return_dayOfWeek_when_it_is_valid(string value, DayOfWeek expected) 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> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="HelperTests\XElementHelperFixture.cs" />
<Compile Include="JobTests\RenameSeasonJobFixture.cs" /> <Compile Include="JobTests\RenameSeasonJobFixture.cs" />
<Compile Include="ProviderTests\SearchProviderTests\GetSeriesTitleFixture.cs" /> <Compile Include="ProviderTests\SearchProviderTests\GetSeriesTitleFixture.cs" />
<Compile Include="ProviderTests\TvRageMappingProviderTests\FindMatchingTvRageSeriesFixture.cs" /> <Compile Include="ProviderTests\TvRageMappingProviderTests\FindMatchingTvRageSeriesFixture.cs" />
@ -207,7 +208,7 @@
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" /> <Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" /> <Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
<Compile Include="Services\ParseErrorServiceFixture.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\EpisodeProviderTests\EpisodeProviderTest_DeleteInvalidEpisodes.cs" />
<Compile Include="ProviderTests\DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" /> <Compile Include="ProviderTests\DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="ProviderTests\QualityTypeProviderTest.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\EpisodeSortingHelper.cs" />
<Compile Include="Helpers\SortHelper.cs" /> <Compile Include="Helpers\SortHelper.cs" />
<Compile Include="Helpers\SabnzbdPriorityTypeConverter.cs" /> <Compile Include="Helpers\SabnzbdPriorityTypeConverter.cs" />
<Compile Include="Helpers\XElementHelper.cs" />
<Compile Include="Jobs\CleanupRecycleBinJob.cs" /> <Compile Include="Jobs\CleanupRecycleBinJob.cs" />
<Compile Include="Jobs\XemUpdateJob.cs" /> <Compile Include="Jobs\XemUpdateJob.cs" />
<Compile Include="Jobs\EmptyRecycleBinJob.cs" /> <Compile Include="Jobs\EmptyRecycleBinJob.cs" />

View file

@ -6,6 +6,7 @@
using NLog; using NLog;
using Ninject; using Ninject;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Model.TvRage; using NzbDrone.Core.Model.TvRage;
namespace NzbDrone.Core.Providers namespace NzbDrone.Core.Providers
@ -40,27 +41,22 @@ public virtual IList<TvRageSearchResult> SearchSeries(string title)
try try
{ {
var show = new TvRageSearchResult(); 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.Name = s.Element("name").Value;
show.Link = s.Element("link").Value; show.Link = s.Element("link").Value;
show.Country = s.Element("country").Value; show.Country = s.Element("country").Value;
DateTime started; show.Started = s.Element("started").ConvertTo<DateTime>();
if (DateTime.TryParse(s.Element("started").Value, out started)) ; show.Ended = s.Element("ended").ConvertTo<DateTime>();
show.Started = started;
DateTime ended;
if (DateTime.TryParse(s.Element("ended").Value, out ended)) ;
show.Ended = ended;
if (show.Ended < new DateTime(1900, 1, 1)) if (show.Ended < new DateTime(1900, 1, 1))
show.Ended = null; 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.Status = s.Element("status").Value;
show.RunTime = Int32.Parse(s.Element("runtime").Value); show.RunTime = s.Element("seasons").ConvertTo<Int32>();
show.AirTime = DateTime.Parse(s.Element("airtime").Value); show.AirTime = s.Element("seasons").ConvertTo<DateTime>();
show.AirDay = ParseDayOfWeek(s.Element("airday")); show.AirDay = s.Element("airday").ConvertToDayOfWeek();
searchResults.Add(show); searchResults.Add(show);
} }
@ -89,26 +85,21 @@ public virtual TvRageSeries GetSeries(int id)
} }
var show = new TvRageSeries(); 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.Name = s.Element("showname").Value;
show.Link = s.Element("showlink").Value; show.Link = s.Element("showlink").Value;
show.Seasons = Int32.Parse(s.Element("seasons").Value); show.Seasons = s.Element("seasons").ConvertTo<Int32>();
show.Started = Int32.Parse(s.Element("started").Value); show.Started = s.Element("started").ConvertTo<Int32>();
DateTime startDate; show.StartDate = s.Element("startdate").ConvertTo<DateTime>();
if (DateTime.TryParse(s.Element("startdate").Value, out startDate)) ; show.Ended = s.Element("ended").ConvertTo<DateTime>();
show.StartDate = startDate;
DateTime ended;
if (DateTime.TryParse(s.Element("ended").Value, out ended)) ;
show.Ended = ended;
show.OriginCountry = s.Element("origin_country").Value; show.OriginCountry = s.Element("origin_country").Value;
show.Status = s.Element("status").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.Network = s.Element("network").Value;
show.AirTime = DateTime.Parse(s.Element("airtime").Value); show.AirTime = s.Element("airtime").ConvertTo<DateTime>();
show.AirDay = ParseDayOfWeek(s.Element("airday")); show.AirDay = s.Element("airday").ConvertToDayOfWeek();
show.UtcOffset = GetUtcOffset(s.Element("timezone").Value); show.UtcOffset = GetUtcOffset(s.Element("timezone").Value);
return show; return show;
} }
@ -139,10 +130,10 @@ public virtual List<TvRageEpisode> GetEpisodes(int id)
try try
{ {
var episode = new TvRageEpisode(); var episode = new TvRageEpisode();
episode.EpisodeNumber = Int32.Parse(e.Element("epnum").Value); episode.EpisodeNumber = e.Element("epnum").ConvertTo<Int32>();
episode.SeasonNumber = Int32.Parse(e.Element("seasonnum").Value); episode.SeasonNumber = e.Element("seasonnum").ConvertTo<Int32>();
episode.ProductionCode = e.Element("prodnum").Value; 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.Link = e.Element("link").Value;
episode.Title = e.Element("title").Value; episode.Title = e.Element("title").Value;
episodes.Add(episode); episodes.Add(episode);
@ -174,24 +165,5 @@ internal int GetUtcOffset(string timeZone)
return offset; 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;
}
} }
} }