2013-10-23 09:36:31 +03:00
|
|
|
using System;
|
2014-04-01 23:07:41 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.Configuration;
|
2014-02-02 01:07:30 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-10-23 09:36:31 +03:00
|
|
|
using NzbDrone.Core.Download;
|
2014-02-02 01:07:30 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-10-23 09:36:31 +03:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2014-02-23 03:17:19 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-10-23 09:36:31 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Blacklisting
|
|
|
|
{
|
|
|
|
public interface IBlacklistService
|
|
|
|
{
|
2014-04-01 23:07:41 +03:00
|
|
|
bool Blacklisted(int seriesId,string sourceTitle, DateTime publishedDate);
|
2014-02-02 01:07:30 +03:00
|
|
|
PagingSpec<Blacklist> Paged(PagingSpec<Blacklist> pagingSpec);
|
|
|
|
void Delete(int id);
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
public class BlacklistService : IBlacklistService,
|
|
|
|
IExecute<ClearBlacklistCommand>,
|
|
|
|
IHandle<DownloadFailedEvent>,
|
|
|
|
IHandle<SeriesDeletedEvent>
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
|
|
|
private readonly IBlacklistRepository _blacklistRepository;
|
2013-10-24 08:13:04 +03:00
|
|
|
private readonly IRedownloadFailedDownloads _redownloadFailedDownloadService;
|
2013-10-23 09:36:31 +03:00
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
public BlacklistService(IBlacklistRepository blacklistRepository,
|
|
|
|
IRedownloadFailedDownloads redownloadFailedDownloadService)
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
|
|
|
_blacklistRepository = blacklistRepository;
|
2013-10-24 08:13:04 +03:00
|
|
|
_redownloadFailedDownloadService = redownloadFailedDownloadService;
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
public bool Blacklisted(int seriesId, string sourceTitle, DateTime publishedDate)
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
2014-04-01 23:07:41 +03:00
|
|
|
var blacklisted = _blacklistRepository.Blacklisted(seriesId, sourceTitle);
|
|
|
|
|
|
|
|
return blacklisted.Any(item => HasSamePublishedDate(item, publishedDate));
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
|
2014-02-02 01:07:30 +03:00
|
|
|
public PagingSpec<Blacklist> Paged(PagingSpec<Blacklist> pagingSpec)
|
|
|
|
{
|
|
|
|
return _blacklistRepository.GetPaged(pagingSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
{
|
|
|
|
_blacklistRepository.Delete(id);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
private bool HasSamePublishedDate(Blacklist item, DateTime publishedDate)
|
|
|
|
{
|
|
|
|
if (!item.PublishedDate.HasValue) return true;
|
|
|
|
|
|
|
|
return item.PublishedDate.Value.AddDays(-2) <= publishedDate &&
|
|
|
|
item.PublishedDate.Value.AddDays(2) >= publishedDate;
|
|
|
|
}
|
|
|
|
|
2014-02-23 03:17:19 +03:00
|
|
|
public void Execute(ClearBlacklistCommand message)
|
|
|
|
{
|
|
|
|
_blacklistRepository.Purge();
|
|
|
|
}
|
|
|
|
|
2013-10-23 09:36:31 +03:00
|
|
|
public void Handle(DownloadFailedEvent message)
|
|
|
|
{
|
|
|
|
var blacklist = new Blacklist
|
|
|
|
{
|
2013-10-24 08:13:04 +03:00
|
|
|
SeriesId = message.SeriesId,
|
|
|
|
EpisodeIds = message.EpisodeIds,
|
2013-10-23 09:36:31 +03:00
|
|
|
SourceTitle = message.SourceTitle,
|
|
|
|
Quality = message.Quality,
|
2014-04-01 23:07:41 +03:00
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
PublishedDate = DateTime.Parse(message.Data.GetValueOrDefault("publishedDate", null))
|
2013-10-23 09:36:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_blacklistRepository.Insert(blacklist);
|
2013-10-24 08:13:04 +03:00
|
|
|
|
|
|
|
_redownloadFailedDownloadService.Redownload(message.SeriesId, message.EpisodeIds);
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
2014-02-02 01:07:30 +03:00
|
|
|
|
2014-02-23 03:17:19 +03:00
|
|
|
public void Handle(SeriesDeletedEvent message)
|
2014-02-02 01:07:30 +03:00
|
|
|
{
|
2014-02-23 03:17:19 +03:00
|
|
|
var blacklisted = _blacklistRepository.BlacklistedBySeries(message.Series.Id);
|
|
|
|
|
|
|
|
_blacklistRepository.DeleteMany(blacklisted);
|
2014-02-02 01:07:30 +03:00
|
|
|
}
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
}
|