2013-03-04 06:53:02 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-03-05 07:25:05 +01:00
|
|
|
|
using System.Threading;
|
2013-03-04 06:53:02 +01:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Common.Eventing;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaCover
|
|
|
|
|
{
|
2013-03-05 07:25:05 +01:00
|
|
|
|
public class MediaCoverService : IHandleAsync<SeriesUpdatedEvent>
|
2013-03-04 06:53:02 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly string _coverRootFolder;
|
|
|
|
|
|
|
|
|
|
private const string COVER_URL_PREFIX = "http://www.thetvdb.com/banners/";
|
|
|
|
|
|
|
|
|
|
public MediaCoverService(HttpProvider httpProvider, DiskProvider diskProvider, EnvironmentProvider environmentProvider, Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
|
|
_coverRootFolder = environmentProvider.GetMediaCoverPath();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-05 07:25:05 +01:00
|
|
|
|
public void HandleAsync(SeriesUpdatedEvent message)
|
2013-03-04 06:53:02 +01:00
|
|
|
|
{
|
|
|
|
|
EnsureCovers(message.Series);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureCovers(Series series)
|
|
|
|
|
{
|
|
|
|
|
foreach (var cover in series.Covers)
|
|
|
|
|
{
|
|
|
|
|
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
|
|
|
|
if (!_diskProvider.FileExists(fileName))
|
|
|
|
|
{
|
|
|
|
|
DownloadCover(series, cover);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DownloadCover(Series series, MediaCover cover)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
|
|
|
|
|
|
|
|
|
_logger.Info("Downloading {0} for {1}", cover.CoverType, series.Title);
|
|
|
|
|
_httpProvider.DownloadFile(COVER_URL_PREFIX + cover.Url, fileName);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Couldn't download media cover for " + series.TvDbId, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetCoverPath(int seriesId, MediaCoverTypes coverTypes)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(_coverRootFolder, seriesId.ToString("0000"), coverTypes.ToString().ToLower() + ".jpg");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|