2012-03-07 05:53:49 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using NLog;
|
2012-03-07 23:37:36 +03:00
|
|
|
using Ninject;
|
2012-03-07 05:53:49 +03:00
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class BannerProvider
|
|
|
|
{
|
|
|
|
private readonly HttpProvider _httpProvider;
|
2012-03-07 05:59:43 +03:00
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2012-03-07 05:53:49 +03:00
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
private const string BANNER_URL_PREFIX = "http://www.thetvdb.com/banners/";
|
|
|
|
|
2012-03-07 23:37:36 +03:00
|
|
|
[Inject]
|
2012-03-07 05:59:43 +03:00
|
|
|
public BannerProvider(HttpProvider httpProvider, EnvironmentProvider environmentProvider,
|
2012-03-07 05:53:49 +03:00
|
|
|
DiskProvider diskProvider)
|
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
2012-03-07 05:59:43 +03:00
|
|
|
_environmentProvider = environmentProvider;
|
2012-03-07 05:53:49 +03:00
|
|
|
_diskProvider = diskProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BannerProvider()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-07 23:37:36 +03:00
|
|
|
public virtual bool Download(Series series)
|
2012-03-07 05:53:49 +03:00
|
|
|
{
|
2012-03-07 23:37:36 +03:00
|
|
|
var bannerPath = _environmentProvider.GetBannerPath();
|
2012-03-07 10:34:26 +03:00
|
|
|
|
2012-03-07 23:37:36 +03:00
|
|
|
logger.Trace("Ensuring Banner Folder exists: ", bannerPath);
|
|
|
|
_diskProvider.CreateDirectory(bannerPath);
|
2012-03-07 05:53:49 +03:00
|
|
|
|
2012-03-07 23:37:36 +03:00
|
|
|
var bannerFilename = Path.Combine(bannerPath, series.SeriesId.ToString()) + ".jpg";
|
2012-03-07 05:53:49 +03:00
|
|
|
|
|
|
|
logger.Trace("Downloading banner for '{0}'", series.Title);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_httpProvider.DownloadFile(BANNER_URL_PREFIX + series.BannerUrl, bannerFilename);
|
|
|
|
logger.Trace("Successfully download banner for '{0}'", series.Title);
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
logger.Debug("Failed to download banner for '{0}'", series.Title);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool Delete(int seriesId)
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
var bannerPath = _environmentProvider.GetBannerPath();
|
2012-03-07 05:53:49 +03:00
|
|
|
var bannerFilename = Path.Combine(bannerPath, seriesId.ToString()) + ".jpg";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logger.Trace("Checking if banner exists: {0}", bannerFilename);
|
|
|
|
|
|
|
|
if (_diskProvider.FileExists(bannerFilename))
|
|
|
|
{
|
|
|
|
logger.Trace("Deleting existing banner: {0}", bannerFilename);
|
|
|
|
_diskProvider.DeleteFile(bannerFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception ex)
|
|
|
|
{
|
|
|
|
logger.WarnException("Failed to delete banner: " + bannerFilename, ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|