Properly using Xem now

pull/19/head
Mark McDowall 12 years ago
parent 4b5d20cefe
commit c9c967fa1d

@ -0,0 +1,7 @@
{
"result": "failure",
"data": [ ],
"message": "no show with the tvdb_id 79488 found"
}

@ -0,0 +1,24 @@
{
"result": "success",
"data": {
"220571": [
"Is This a Zombie? Of the Dead",
"Kore wa Zombie Desuka?",
"Kore wa Zombie Desuka? Of the Dead",
"Kore wa Zombie Desuka Of the Dead",
"Kore wa Zombie Desu ka - Of the Dead",
"Kore wa Zombie Desu ka of the Dead"
],
"79151": [
"Fate Stay Night",
"Fate/Zero",
"Fate Zero",
"Fate/Zero (2012)",
"Fate Zero S2",
"Fate Zero"
]
},
"message": ""
}

@ -0,0 +1,32 @@
{
"result": "success",
"data": [
{
"scene": {
"season": 1,
"episode": 1,
"absolute": 1
},
"tvdb": {
"season": 1,
"episode": 1,
"absolute": 1
}
},
{
"scene": {
"season": 1,
"episode": 2,
"absolute": 2
},
"tvdb": {
"season": 1,
"episode": 2,
"absolute": 2
}
}
],
"message": "full mapping for 73388 on tvdb. this was a cached version"
}

@ -191,6 +191,8 @@
<Compile Include="JobTests\AppUpdateJobFixture.cs" />
<Compile Include="ProviderTests\UpdateProviderTests\GetUpdateLogFixture.cs" />
<Compile Include="ProviderTests\UpdateProviderTests\GetAvilableUpdateFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
<Compile Include="Services\ParseErrorServiceFixture.cs" />
<Compile Include="SortHelperTest.cs" />
<Compile Include="ProviderTests\EpisodeProviderTest_DeleteInvalidEpisodes.cs" />
@ -332,6 +334,15 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Files\Xem\Failure.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\Xem\Ids.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\Xem\Mappings.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="log.config">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common.AutoMoq;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.XemCommunicationProviderTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class GetSceneTvdbMappingsFixture : CoreTest
{
private void WithFailureJson()
{
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Xem\Failure.txt"));
}
private void WithIdsJson()
{
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Xem\Ids.txt"));
}
private void WithMappingsJson()
{
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Xem\Mappings.txt"));
}
[Test]
public void should_throw_when_failure_is_found()
{
WithFailureJson();
Assert.Throws<Exception>(() => Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345));
}
[Test]
public void should_get_list_of_mappings()
{
WithMappingsJson();
Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345).Should().NotBeEmpty();
}
[Test]
public void should_have_two_mappings()
{
WithMappingsJson();
Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345).Should().HaveCount(2);
}
[Test]
public void should_have_expected_results()
{
WithMappingsJson();
var results = Mocker.Resolve<XemCommunicationProvider>().GetSceneTvdbMappings(12345);
var first = results.First();
first.Scene.Absolute.Should().Be(1);
first.Scene.Season.Should().Be(1);
first.Scene.Episode.Should().Be(1);
first.Tvdb.Absolute.Should().Be(1);
first.Tvdb.Season.Should().Be(1);
first.Tvdb.Episode.Should().Be(1);
}
}
}

@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common.AutoMoq;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.XemCommunicationProviderTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class GetXemSeriesIdsFixture : CoreTest
{
private void WithFailureJson()
{
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Xem\Failure.txt"));
}
private void WithIdsJson()
{
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Xem\Ids.txt"));
}
private void WithMappingsJson()
{
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString(It.IsAny<String>()))
.Returns(File.ReadAllText(@".\Files\Xem\Mappings.txt"));
}
[Test]
public void should_throw_when_failure_is_found()
{
WithFailureJson();
Assert.Throws<Exception>(() => Mocker.Resolve<XemCommunicationProvider>().GetXemSeriesIds());
}
[Test]
public void should_get_list_of_int()
{
WithIdsJson();
Mocker.Resolve<XemCommunicationProvider>().GetXemSeriesIds().Should().NotBeEmpty();
}
[Test]
public void should_have_two_ids()
{
WithIdsJson();
Mocker.Resolve<XemCommunicationProvider>().GetXemSeriesIds().Should().HaveCount(2);
}
}
}

@ -0,0 +1,18 @@
using System;
using System.Data;
using Migrator.Framework;
using NzbDrone.Common;
namespace NzbDrone.Core.Datastore.Migrations
{
[Migration(20121016)]
public class Migration20121016 : NzbDroneMigration
{
protected override void MainDbUpgrade()
{
Database.AddColumn("Episodes", new Column("SceneAbsoluteEpisodeNumber", DbType.Int32, ColumnProperty.Null));
Database.AddColumn("Episodes", new Column("SceneSeasonNumber", DbType.Int32, ColumnProperty.Null));
Database.AddColumn("Episodes", new Column("SceneEpisodeNumber", DbType.Int32, ColumnProperty.Null));
}
}
}

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Xem
{
public class XemResult<T>
{
public string Result { get; set; }
public T Data { get; set; }
public string Message { get; set; }
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Xem
{
public class XemSceneTvdbMapping
{
public XemValues Scene { get; set; }
public XemValues Tvdb { get; set; }
}
}

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Xem
{
public class XemValues
{
public int Season { get; set; }
public int Episode { get; set; }
public int Absolute { get; set; }
}
}

@ -292,6 +292,9 @@
<Compile Include="Model\Xbmc\TvShowResult.cs" />
<Compile Include="Model\Xbmc\ErrorResult.cs" />
<Compile Include="Model\Xbmc\IconType.cs" />
<Compile Include="Model\Xem\XemResult.cs" />
<Compile Include="Model\Xem\XemSceneTvdbMapping.cs" />
<Compile Include="Model\Xem\XemValues.cs" />
<Compile Include="Providers\BannerProvider.cs" />
<Compile Include="Providers\DecisionEngine\AllowedReleaseGroupSpecification.cs" />
<Compile Include="Providers\DecisionEngine\CustomStartDateSpecification.cs" />
@ -330,6 +333,7 @@
<Compile Include="Jobs\RssSyncJob.cs" />
<Compile Include="Jobs\UpdateInfoJob.cs" />
<Compile Include="Providers\StatsProvider.cs" />
<Compile Include="Providers\XemCommunicationProvider.cs" />
<Compile Include="Repository\MetadataDefinition.cs" />
<Compile Include="Repository\Search\SearchHistoryItem.cs" />
<Compile Include="Repository\Search\SearchHistory.cs" />

@ -331,7 +331,7 @@ namespace NzbDrone.Core.Providers
episodeToUpdate.TvDbEpisodeId = episode.Id;
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteNumber;
episodeToUpdate.Title = episode.EpisodeName;
episodeToUpdate.Overview = episode.Overview.Truncate(3500);
@ -435,5 +435,10 @@ namespace NzbDrone.Core.Providers
logger.Trace("Updating PostDownloadStatus for all episodeIds in {0}", episodeIdString);
_database.Execute(episodeIdQuery);
}
public virtual void UpdateEpisodes(List<Episode> episodes)
{
_database.UpdateMany(episodes);
}
}
}

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Core.Model.Xem;
namespace NzbDrone.Core.Providers
{
public class XemCommunicationProvider
{
private readonly HttpProvider _httpProvider;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private const string XEM_BASE_URL = "http://thexem.de/map/";
[Inject]
public XemCommunicationProvider(HttpProvider httpProvider)
{
_httpProvider = httpProvider;
}
public XemCommunicationProvider()
{
}
public virtual List<Int32> GetXemSeriesIds(string origin = "tvdb")
{
_logger.Trace("Fetching Series IDs from: {0}", origin);
var url = String.Format("{0}allNames?origin={1}", XEM_BASE_URL, origin);
var response =_httpProvider.DownloadString(url);
CheckForFailureResult(response);
var result = JsonConvert.DeserializeObject<Dictionary<int, List<String>>>(JObject.Parse(response).SelectToken("data").ToString());
return result.Keys.ToList();
}
public virtual List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id)
{
_logger.Trace("Fetching Mappings for: {0}", id);
var url = String.Format("{0}all?id={1}&origin=tvdb", XEM_BASE_URL, id);
var response = _httpProvider.DownloadString(url);
CheckForFailureResult(response);
var result = JsonConvert.DeserializeObject<List<XemSceneTvdbMapping>>(JObject.Parse(response).SelectToken("data").ToString());
return result;
}
public virtual void CheckForFailureResult(string response)
{
var result = JsonConvert.DeserializeObject<XemResult<dynamic>>(response);
if (result != null && result.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Error response received from Xem: " + result.Message);
}
}
}

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using Ninject;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class XemProvider
{
private readonly SeriesProvider _seriesProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly XemCommunicationProvider _xemCommunicationProvider;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
[Inject]
public XemProvider(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
XemCommunicationProvider xemCommunicationProvider)
{
_seriesProvider = seriesProvider;
_episodeProvider = episodeProvider;
_xemCommunicationProvider = xemCommunicationProvider;
}
public XemProvider()
{
}
public virtual void UpdateMappings()
{
_logger.Trace("Starting scene numbering update");
try
{
var ids = _xemCommunicationProvider.GetXemSeriesIds();
var series = _seriesProvider.GetAllSeries();
var wantedSeries = series.Where(s => ids.Contains(s.SeriesId));
foreach(var ser in wantedSeries)
{
_logger.Trace("Updating scene numbering mapping for: {0}", ser.Title);
try
{
var episodesToUpdate = new List<Episode>();
var mappings = _xemCommunicationProvider.GetSceneTvdbMappings(ser.SeriesId);
if (mappings == null)
{
_logger.Trace("Mappings for: {0} are null, skipping", ser.Title);
continue;
}
foreach(var mapping in mappings)
{
_logger.Trace("Setting scene numbering mappings for {0} S{1:00}E{2:00}", ser.Title, mapping.Tvdb.Season, mapping.Tvdb.Episode);
var episode = _episodeProvider.GetEpisode(ser.SeriesId, mapping.Tvdb.Season, mapping.Tvdb.Episode);
episode.AbsoluteEpisodeNumber = mapping.Scene.Absolute;
episode.SceneSeasonNumber = mapping.Scene.Season;
episode.SceneEpisodeNumber = mapping.Scene.Episode;
episodesToUpdate.Add(episode);
}
_logger.Trace("Committing scene numbering mappings to database for: {0}", ser.Title);
_episodeProvider.UpdateEpisodes(episodesToUpdate);
}
catch(Exception ex)
{
_logger.WarnException("Error updating scene numbering mappings for: " + ser, ex);
}
}
_logger.Trace("Completed scene numbering update");
}
catch(Exception ex)
{
_logger.WarnException("Error updating Scene Mappings", ex);
throw;
}
}
}
}

@ -22,6 +22,9 @@ namespace NzbDrone.Core.Repository
public Boolean Ignored { get; set; }
public PostDownloadStatusType PostDownloadStatus { get; set; }
public int AbsoluteEpisodeNumber { get; set; }
public int SceneAbsoluteEpisodeNumber { get; set; }
public int SceneSeasonNumber { get; set; }
public int SceneEpisodeNumber { get; set; }
/// <summary>
/// Gets or sets the grab date.

Loading…
Cancel
Save