2013-10-23 09:36:31 +03:00
|
|
|
using System;
|
2014-04-01 23:07:41 +03:00
|
|
|
using System.Linq;
|
2014-12-02 08:26:25 +02:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-02-02 01:07:30 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-10-23 09:36:31 +03:00
|
|
|
using NzbDrone.Core.Download;
|
2015-04-20 05:25:59 +02:00
|
|
|
using NzbDrone.Core.Indexers;
|
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;
|
2015-04-20 05:25:59 +02:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
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
|
|
|
|
{
|
2015-04-20 05:25:59 +02:00
|
|
|
bool Blacklisted(int seriesId, ReleaseInfo release);
|
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,
|
2015-04-20 05:25:59 +02:00
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
IExecute<ClearBlacklistCommand>,
|
|
|
|
IHandle<DownloadFailedEvent>,
|
2014-12-19 02:26:42 +02:00
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
|
|
|
private readonly IBlacklistRepository _blacklistRepository;
|
|
|
|
|
2014-12-19 02:26:42 +02:00
|
|
|
public BlacklistService(IBlacklistRepository blacklistRepository)
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
|
|
|
_blacklistRepository = blacklistRepository;
|
|
|
|
}
|
|
|
|
|
2015-04-20 05:25:59 +02:00
|
|
|
public bool Blacklisted(int seriesId, ReleaseInfo release)
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
2015-04-20 05:25:59 +02:00
|
|
|
var blacklistedByTitle = _blacklistRepository.BlacklistedByTitle(seriesId, release.Title);
|
|
|
|
|
|
|
|
if (release.DownloadProtocol == DownloadProtocol.Torrent)
|
|
|
|
{
|
|
|
|
var torrentInfo = release as TorrentInfo;
|
|
|
|
|
|
|
|
if (torrentInfo == null) return false;
|
|
|
|
|
|
|
|
if (torrentInfo.InfoHash.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return blacklistedByTitle.Where(b => b.Protocol == DownloadProtocol.Torrent)
|
|
|
|
.Any(b => SameTorrent(b, torrentInfo));
|
|
|
|
}
|
|
|
|
|
2015-05-18 01:20:51 +02:00
|
|
|
var blacklistedByTorrentInfohash = _blacklistRepository.BlacklistedByTorrentInfoHash(seriesId, torrentInfo.InfoHash);
|
2015-04-20 05:25:59 +02:00
|
|
|
|
|
|
|
return blacklistedByTorrentInfohash.Any(b => SameTorrent(b, torrentInfo));
|
|
|
|
}
|
2014-04-01 23:07:41 +03:00
|
|
|
|
2015-04-20 05:25:59 +02:00
|
|
|
return blacklistedByTitle.Where(b => b.Protocol == DownloadProtocol.Usenet)
|
|
|
|
.Any(b => SameNzb(b, release));
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-20 05:25:59 +02:00
|
|
|
private bool SameNzb(Blacklist item, ReleaseInfo release)
|
|
|
|
{
|
|
|
|
if (item.PublishedDate == release.PublishDate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasSameIndexer(item, release.Indexer) &&
|
|
|
|
HasSamePublishedDate(item, release.PublishDate) &&
|
|
|
|
HasSameSize(item, release.Size))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool SameTorrent(Blacklist item, TorrentInfo release)
|
|
|
|
{
|
|
|
|
if (release.InfoHash.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return release.InfoHash.Equals(item.TorrentInfoHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.Indexer.Equals(release.Indexer, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool HasSameIndexer(Blacklist item, string indexer)
|
|
|
|
{
|
|
|
|
if (item.Indexer.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.Indexer.Equals(indexer, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool HasSamePublishedDate(Blacklist item, DateTime publishedDate)
|
2014-04-01 23:07:41 +03:00
|
|
|
{
|
|
|
|
if (!item.PublishedDate.HasValue) return true;
|
|
|
|
|
2015-04-20 05:25:59 +02:00
|
|
|
return item.PublishedDate.Value.AddMinutes(-2) <= publishedDate &&
|
|
|
|
item.PublishedDate.Value.AddMinutes(2) >= publishedDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool HasSameSize(Blacklist item, long size)
|
|
|
|
{
|
|
|
|
if (!item.Size.HasValue) return true;
|
|
|
|
|
|
|
|
var difference = Math.Abs(item.Size.Value - size);
|
|
|
|
|
|
|
|
return difference <= 2.Megabytes();
|
2014-04-01 23:07:41 +03:00
|
|
|
}
|
|
|
|
|
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,
|
2015-04-20 05:25:59 +02:00
|
|
|
PublishedDate = DateTime.Parse(message.Data.GetValueOrDefault("publishedDate")),
|
|
|
|
Size = Int64.Parse(message.Data.GetValueOrDefault("size", "0")),
|
|
|
|
Indexer = message.Data.GetValueOrDefault("indexer"),
|
|
|
|
Protocol = (DownloadProtocol)Convert.ToInt32(message.Data.GetValueOrDefault("protocol")),
|
|
|
|
Message = message.Message,
|
|
|
|
TorrentInfoHash = message.Data.GetValueOrDefault("torrentInfoHash")
|
2013-10-23 09:36:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_blacklistRepository.Insert(blacklist);
|
|
|
|
}
|
2014-02-02 01:07:30 +03:00
|
|
|
|
2014-12-19 02:26:42 +02:00
|
|
|
public void HandleAsync(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
|
|
|
}
|
|
|
|
}
|