2011-09-10 10:42:05 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Ninject;
|
|
|
|
|
using NLog;
|
2011-10-29 06:54:33 +02:00
|
|
|
|
using NzbDrone.Common;
|
2011-09-10 10:42:05 +02:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 02:33:17 +01:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2011-09-10 10:42:05 +02:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
2011-12-02 02:33:17 +01:00
|
|
|
|
namespace NzbDrone.Core.Jobs
|
2011-09-10 10:42:05 +02:00
|
|
|
|
{
|
|
|
|
|
public class BannerDownloadJob : IJob
|
|
|
|
|
{
|
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2011-10-21 07:04:26 +02:00
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-09-10 10:42:05 +02:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
private const string _bannerUrlPrefix = "http://www.thetvdb.com/banners/";
|
|
|
|
|
|
|
|
|
|
[Inject]
|
2011-11-03 06:04:14 +01:00
|
|
|
|
public BannerDownloadJob(SeriesProvider seriesProvider, HttpProvider httpProvider, DiskProvider diskProvider,
|
2011-11-13 08:27:16 +01:00
|
|
|
|
EnviromentProvider enviromentProvider)
|
2011-09-10 10:42:05 +02:00
|
|
|
|
{
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
_diskProvider = diskProvider;
|
2011-10-21 07:04:26 +02:00
|
|
|
|
_enviromentProvider = enviromentProvider;
|
2011-09-10 10:42:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BannerDownloadJob()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Banner Download"; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-15 03:47:23 +01:00
|
|
|
|
public TimeSpan DefaultInterval
|
2011-09-10 10:42:05 +02:00
|
|
|
|
{
|
2012-01-15 03:47:23 +01:00
|
|
|
|
get { return TimeSpan.FromDays(30); }
|
2011-09-10 10:42:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Starting banner download job");
|
|
|
|
|
|
2011-11-03 06:04:14 +01:00
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
_diskProvider.CreateDirectory(_enviromentProvider.GetBannerPath());
|
2011-09-10 10:42:05 +02:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2011-11-13 08:27:16 +01:00
|
|
|
|
var bannerFilename = Path.Combine(_enviromentProvider.GetBannerPath(), series.SeriesId.ToString()) + ".jpg";
|
2011-09-10 10:42:05 +02:00
|
|
|
|
|
|
|
|
|
notification.CurrentMessage = string.Format("Downloading banner for '{0}'", series.Title);
|
|
|
|
|
|
2011-10-21 07:04:26 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_httpProvider.DownloadFile(_bannerUrlPrefix + series.BannerUrl, bannerFilename);
|
2011-09-10 10:42:05 +02:00
|
|
|
|
notification.CurrentMessage = string.Format("Successfully download banner for '{0}'", series.Title);
|
2011-10-21 07:04:26 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
2011-09-10 10:42:05 +02:00
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Failed to download banner for '{0}'", series.Title);
|
|
|
|
|
notification.CurrentMessage = string.Format("Failed to download banner for '{0}'", series.Title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|