Added BannerDownloadJob, it will run every 30 days.

New series will have their banner downloaded on import.
pull/4/head
Mark McDowall 13 years ago
parent be92686e0e
commit dfd0720872

@ -0,0 +1,250 @@
using System;
using System.Collections.Generic;
using AutoMoq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class BannerDownloadJobTest : TestBase
{
[Test]
public void BannerDownload_all()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
var notification = new ProgressNotification("Banner Download");
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
mocker.VerifyAllMocks();
mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(fakeSeries.Count));
}
[Test]
public void BannerDownload_some_null_BannerUrl()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.WhereRandom(2)
.Have(s => s.BannerUrl = null)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
var notification = new ProgressNotification("Banner Download");
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
mocker.VerifyAllMocks();
mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(8));
}
[Test]
public void BannerDownload_some_failed_download()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
const string path = @"C:\Users\mark.mcdowall\Dropbox\Visual Studio 2010\NzbDrone\NzbDrone.Core.Test\bin\Debug\Content\Images\Banners\";
var mocker = new AutoMoqer(MockBehavior.Strict);
var notification = new ProgressNotification("Banner Download");
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "1.jpg"))
.Returns(false);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "2.jpg"))
.Returns(true);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "3.jpg"))
.Returns(false);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "4.jpg"))
.Returns(true);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "5.jpg"))
.Returns(false);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "6.jpg"))
.Returns(true);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "7.jpg"))
.Returns(false);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "8.jpg"))
.Returns(true);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "9.jpg"))
.Returns(false);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), path + "10.jpg"))
.Returns(true);
mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
mocker.VerifyAllMocks();
mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(fakeSeries.Count));
}
[Test]
public void BannerDownload_all_failed_download()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
var notification = new ProgressNotification("Banner Download");
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Returns(false);
mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
mocker.VerifyAllMocks();
mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(fakeSeries.Count));
}
[Test]
public void BannerDownload_single_banner()
{
//Setup
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 1)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
var notification = new ProgressNotification("Banner Download");
mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetSeries(1))
.Returns(fakeSeries);
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
mocker.Resolve<BannerDownloadJob>().Start(notification, 1, 0);
//Assert
mocker.VerifyAllMocks();
mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Once());
}
[Test]
public void Download_Banner()
{
//Setup
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 1)
.Build();
var mocker = new AutoMoqer(MockBehavior.Strict);
var notification = new ProgressNotification("Banner Download");
mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
//Act
mocker.Resolve<BannerDownloadJob>().DownloadBanner(notification, fakeSeries);
//Assert
mocker.VerifyAllMocks();
mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Once());
}
}
}

@ -44,6 +44,8 @@ namespace NzbDrone.Core.Test
.Setup(j => j.Start(notification, series[1].SeriesId, 0))
.Callback(() => series[1].LastDiskSync = DateTime.Now);
mocker.GetMock<BannerDownloadJob>()
.Setup(j => j.Start(notification, It.IsAny<int>(), 0));
mocker.GetMock<UpdateInfoJob>()
.Setup(j => j.Start(notification, series[0].SeriesId, 0))
@ -109,6 +111,8 @@ namespace NzbDrone.Core.Test
.Setup(j => j.Start(notification, series[0].SeriesId, 0))
.Callback(() => series[0].LastDiskSync = DateTime.Now);
mocker.GetMock<BannerDownloadJob>()
.Setup(j => j.Start(notification, series[0].SeriesId, 0));
mocker.GetMock<SeriesProvider>()
.Setup(s => s.GetSeries(series[0].SeriesId)).Returns(series[0]);

@ -89,6 +89,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BacklogSearchJobTest.cs" />
<Compile Include="BannerDownloadJobTest.cs" />
<Compile Include="MisnamedProviderTest.cs" />
<Compile Include="SeasonSearchJobTest.cs" />
<Compile Include="SearchProviderTest_PartialSeason.cs" />

@ -109,6 +109,7 @@ namespace NzbDrone.Core
_kernel.Bind<IJob>().To<SeriesSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<BacklogSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<BannerDownloadJob>().InSingletonScope();
_kernel.Get<JobProvider>().Initialize();
_kernel.Get<WebTimer>().StartTimer(30);

@ -0,0 +1,22 @@
using System;
using System.Data;
using Migrator.Framework;
namespace NzbDrone.Core.Datastore.Migrations
{
[Migration(20110909)]
public class Migration20110909 : Migration
{
public override void Up()
{
Database.AddColumn("Series", "Runtime", DbType.Int32, ColumnProperty.Null);
Database.AddColumn("Series", "BannerUrl", DbType.String, ColumnProperty.Null);
}
public override void Down()
{
throw new NotImplementedException();
}
}
}

@ -176,6 +176,7 @@
<Compile Include="Datastore\MigrationLogger.cs" />
<Compile Include="Datastore\MigrationsHelper.cs" />
<Compile Include="Datastore\CustomeMapper.cs" />
<Compile Include="Datastore\Migrations\Migration20110909.cs" />
<Compile Include="Datastore\Migrations\Migration20110726.cs" />
<Compile Include="Datastore\Migrations\Migration20110707.cs" />
<Compile Include="Datastore\PetaDbProviderFactory.cs" />
@ -204,6 +205,7 @@
<Compile Include="Model\Xbmc\IconType.cs" />
<Compile Include="Providers\Core\UdpProvider.cs" />
<Compile Include="Providers\Jobs\BacklogSearchJob.cs" />
<Compile Include="Providers\Jobs\BannerDownloadJob.cs" />
<Compile Include="Providers\Jobs\RenameSeriesJob.cs" />
<Compile Include="Providers\Jobs\SeriesSearchJob.cs" />
<Compile Include="Providers\Jobs\RenameSeasonJob.cs" />

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers.Jobs
{
public class BannerDownloadJob : IJob
{
private readonly SeriesProvider _seriesProvider;
private readonly HttpProvider _httpProvider;
private readonly DiskProvider _diskProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private string _bannerPath = "";
private const string _bannerUrlPrefix = "http://www.thetvdb.com/banners/";
[Inject]
public BannerDownloadJob(SeriesProvider seriesProvider, HttpProvider httpProvider, DiskProvider diskProvider)
{
_seriesProvider = seriesProvider;
_httpProvider = httpProvider;
_diskProvider = diskProvider;
}
public BannerDownloadJob()
{
}
public string Name
{
get { return "Banner Download"; }
}
public int DefaultInterval
{
//30 days
get { return 43200; }
}
public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
{
Logger.Debug("Starting banner download job");
_bannerPath = Path.Combine(CentralDispatch.AppPath, "Content", "Images", "Banners");
_diskProvider.CreateDirectory(_bannerPath);
if (targetId > 0)
{
var series = _seriesProvider.GetSeries(targetId);
if (series != null && !String.IsNullOrEmpty(series.BannerUrl))
DownloadBanner(notification, series);
return;
}
var seriesInDb = _seriesProvider.GetAllSeries();
foreach (var series in seriesInDb.Where(s => !String.IsNullOrEmpty(s.BannerUrl)))
{
DownloadBanner(notification, series);
}
Logger.Debug("Finished banner download job");
}
public virtual void DownloadBanner(ProgressNotification notification, Series series)
{
var bannerFilename = String.Format("{0}{1}{2}.jpg", _bannerPath, Path.DirectorySeparatorChar, series.SeriesId);
notification.CurrentMessage = string.Format("Downloading banner for '{0}'", series.Title);
if (_httpProvider.DownloadFile(_bannerUrlPrefix + series.BannerUrl, bannerFilename))
notification.CurrentMessage = string.Format("Successfully download banner for '{0}'", series.Title);
else
{
Logger.Debug("Failed to download banner for '{0}'", series.Title);
notification.CurrentMessage = string.Format("Failed to download banner for '{0}'", series.Title);
}
}
}
}

@ -20,6 +20,7 @@ namespace NzbDrone.Core.Providers.Jobs
private readonly MediaFileProvider _mediaFileProvider;
private readonly UpdateInfoJob _updateInfoJob;
private readonly DiskScanJob _diskScanJob;
private readonly BannerDownloadJob _bannerDownloadJob;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
@ -27,13 +28,15 @@ namespace NzbDrone.Core.Providers.Jobs
[Inject]
public ImportNewSeriesJob(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
MediaFileProvider mediaFileProvider, UpdateInfoJob updateInfoJob, DiskScanJob diskScanJob)
MediaFileProvider mediaFileProvider, UpdateInfoJob updateInfoJob,
DiskScanJob diskScanJob, BannerDownloadJob bannerDownloadJob)
{
_seriesProvider = seriesProvider;
_episodeProvider = episodeProvider;
_mediaFileProvider = mediaFileProvider;
_updateInfoJob = updateInfoJob;
_diskScanJob = diskScanJob;
_bannerDownloadJob = bannerDownloadJob;
}
public string Name
@ -73,9 +76,12 @@ namespace NzbDrone.Core.Providers.Jobs
var updatedSeries = _seriesProvider.GetSeries(currentSeries.SeriesId);
AutoIgnoreSeasons(updatedSeries.SeriesId);
notification.CurrentMessage = String.Format("{0} was successfully imported", updatedSeries.Title);
//Download the banner for the new series
_bannerDownloadJob.Start(notification, updatedSeries.SeriesId, 0);
notification.CurrentMessage = String.Format("{0} was successfully imported", updatedSeries.Title);
}
catch (Exception e)
{
Logger.ErrorException(e.Message, e);

@ -94,6 +94,8 @@ namespace NzbDrone.Core.Providers
series.Language = tvDbSeries.Language != null ? tvDbSeries.Language.Abbriviation : string.Empty;
series.CleanTitle = Parser.NormalizeTitle(tvDbSeries.SeriesName);
series.LastInfoSync = DateTime.Now;
series.Runtime = (int)tvDbSeries.Runtime;
series.BannerUrl = tvDbSeries.BannerPath;
UpdateSeries(series);
return series;

@ -37,6 +37,10 @@ namespace NzbDrone.Core.Repository
public DateTime? LastDiskSync { get; set; }
public int Runtime { get; set; }
public string BannerUrl { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Series"/> is hidden.
/// </summary>

Loading…
Cancel
Save