2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
2019-10-28 21:30:08 +00:00
|
|
|
using System.Collections.Generic;
|
2014-04-01 20:07:41 +00:00
|
|
|
using System.Linq;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-02-01 22:07:30 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-10-23 06:36:31 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2015-04-20 03:25:59 +00:00
|
|
|
using NzbDrone.Core.Indexers;
|
2014-02-01 22:07:30 +00:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-10-23 06:36:31 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2017-08-16 21:55:59 +00:00
|
|
|
using NzbDrone.Core.Music.Events;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-10-23 06:36:31 +00:00
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
namespace NzbDrone.Core.Blocklisting
|
2013-10-23 06:36:31 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
public interface IBlocklistService
|
2013-10-23 06:36:31 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
bool Blocklisted(int artistId, ReleaseInfo release);
|
2023-10-29 18:23:23 +00:00
|
|
|
bool BlocklistedTorrentHash(int artistId, string hash);
|
2021-08-19 21:35:06 +00:00
|
|
|
PagingSpec<Blocklist> Paged(PagingSpec<Blocklist> pagingSpec);
|
2023-10-21 01:36:51 +00:00
|
|
|
void Block(RemoteAlbum remoteAlbum, string message);
|
2014-02-01 22:07:30 +00:00
|
|
|
void Delete(int id);
|
2020-10-11 22:28:32 +00:00
|
|
|
void Delete(List<int> ids);
|
2013-10-23 06:36:31 +00:00
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
public class BlocklistService : IBlocklistService,
|
2015-04-20 03:25:59 +00:00
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
IExecute<ClearBlocklistCommand>,
|
2014-04-01 20:07:41 +00:00
|
|
|
IHandle<DownloadFailedEvent>,
|
2021-01-24 19:41:48 +00:00
|
|
|
IHandleAsync<ArtistsDeletedEvent>
|
2013-10-23 06:36:31 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
private readonly IBlocklistRepository _blocklistRepository;
|
2013-10-23 06:36:31 +00:00
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
public BlocklistService(IBlocklistRepository blocklistRepository)
|
2013-10-23 06:36:31 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
_blocklistRepository = blocklistRepository;
|
2013-10-23 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
public bool Blocklisted(int artistId, ReleaseInfo release)
|
2013-10-23 06:36:31 +00:00
|
|
|
{
|
2015-04-20 03:25:59 +00:00
|
|
|
if (release.DownloadProtocol == DownloadProtocol.Torrent)
|
|
|
|
{
|
2024-01-12 01:19:31 +00:00
|
|
|
if (release is not TorrentInfo torrentInfo)
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 03:25:59 +00:00
|
|
|
|
2024-01-12 01:19:31 +00:00
|
|
|
if (torrentInfo.InfoHash.IsNotNullOrWhiteSpace())
|
2015-04-20 03:25:59 +00:00
|
|
|
{
|
2024-01-12 01:19:31 +00:00
|
|
|
var blocklistedByTorrentInfohash = _blocklistRepository.BlocklistedByTorrentInfoHash(artistId, torrentInfo.InfoHash);
|
2015-04-20 03:25:59 +00:00
|
|
|
|
2024-01-12 01:19:31 +00:00
|
|
|
return blocklistedByTorrentInfohash.Any(b => SameTorrent(b, torrentInfo));
|
|
|
|
}
|
2015-04-20 03:25:59 +00:00
|
|
|
|
2024-01-12 01:19:31 +00:00
|
|
|
return _blocklistRepository.BlocklistedByTitle(artistId, release.Title)
|
|
|
|
.Where(b => b.Protocol == DownloadProtocol.Torrent)
|
|
|
|
.Any(b => SameTorrent(b, torrentInfo));
|
2015-04-20 03:25:59 +00:00
|
|
|
}
|
2014-04-01 20:07:41 +00:00
|
|
|
|
2024-01-12 01:19:31 +00:00
|
|
|
return _blocklistRepository.BlocklistedByTitle(artistId, release.Title)
|
|
|
|
.Where(b => b.Protocol == DownloadProtocol.Usenet)
|
|
|
|
.Any(b => SameNzb(b, release));
|
2013-10-23 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 18:23:23 +00:00
|
|
|
public bool BlocklistedTorrentHash(int artistId, string hash)
|
|
|
|
{
|
|
|
|
return _blocklistRepository.BlocklistedByTorrentInfoHash(artistId, hash).Any(b =>
|
|
|
|
b.TorrentInfoHash.Equals(hash, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
public PagingSpec<Blocklist> Paged(PagingSpec<Blocklist> pagingSpec)
|
2014-02-01 22:07:30 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
return _blocklistRepository.GetPaged(pagingSpec);
|
2014-02-01 22:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 01:36:51 +00:00
|
|
|
public void Block(RemoteAlbum remoteAlbum, string message)
|
|
|
|
{
|
|
|
|
var blocklist = new Blocklist
|
|
|
|
{
|
|
|
|
ArtistId = remoteAlbum.Artist.Id,
|
|
|
|
AlbumIds = remoteAlbum.Albums.Select(e => e.Id).ToList(),
|
|
|
|
SourceTitle = remoteAlbum.Release.Title,
|
|
|
|
Quality = remoteAlbum.ParsedAlbumInfo.Quality,
|
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
PublishedDate = remoteAlbum.Release.PublishDate,
|
|
|
|
Size = remoteAlbum.Release.Size,
|
|
|
|
Indexer = remoteAlbum.Release.Indexer,
|
|
|
|
Protocol = remoteAlbum.Release.DownloadProtocol,
|
|
|
|
Message = message,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (remoteAlbum.Release is TorrentInfo torrentRelease)
|
|
|
|
{
|
|
|
|
blocklist.TorrentInfoHash = torrentRelease.InfoHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
_blocklistRepository.Insert(blocklist);
|
|
|
|
}
|
|
|
|
|
2014-02-01 22:07:30 +00:00
|
|
|
public void Delete(int id)
|
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
_blocklistRepository.Delete(id);
|
2014-02-01 22:07:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 22:28:32 +00:00
|
|
|
public void Delete(List<int> ids)
|
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
_blocklistRepository.DeleteMany(ids);
|
2020-10-11 22:28:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
private bool SameNzb(Blocklist item, ReleaseInfo release)
|
2015-04-20 03:25:59 +00:00
|
|
|
{
|
|
|
|
if (item.PublishedDate == release.PublishDate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasSameIndexer(item, release.Indexer) &&
|
|
|
|
HasSamePublishedDate(item, release.PublishDate) &&
|
|
|
|
HasSameSize(item, release.Size))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
private bool SameTorrent(Blocklist item, TorrentInfo release)
|
2015-04-20 03:25:59 +00:00
|
|
|
{
|
|
|
|
if (release.InfoHash.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
2023-08-20 20:25:50 +00:00
|
|
|
return release.InfoHash.Equals(item.TorrentInfoHash, StringComparison.InvariantCultureIgnoreCase);
|
2015-04-20 03:25:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 04:57:42 +00:00
|
|
|
return HasSameIndexer(item, release.Indexer);
|
2015-04-20 03:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
private bool HasSameIndexer(Blocklist item, string indexer)
|
2015-04-20 03:25:59 +00:00
|
|
|
{
|
|
|
|
if (item.Indexer.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.Indexer.Equals(indexer, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
private bool HasSamePublishedDate(Blocklist item, DateTime publishedDate)
|
2014-04-01 20:07:41 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
if (!item.PublishedDate.HasValue)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 20:07:41 +00:00
|
|
|
|
2015-04-20 03:25:59 +00:00
|
|
|
return item.PublishedDate.Value.AddMinutes(-2) <= publishedDate &&
|
|
|
|
item.PublishedDate.Value.AddMinutes(2) >= publishedDate;
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
private bool HasSameSize(Blocklist item, long size)
|
2015-04-20 03:25:59 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
if (!item.Size.HasValue)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-20 03:25:59 +00:00
|
|
|
|
|
|
|
var difference = Math.Abs(item.Size.Value - size);
|
|
|
|
|
|
|
|
return difference <= 2.Megabytes();
|
2014-04-01 20:07:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
public void Execute(ClearBlocklistCommand message)
|
2014-02-23 00:17:19 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
_blocklistRepository.Purge();
|
2014-02-23 00:17:19 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 06:36:31 +00:00
|
|
|
public void Handle(DownloadFailedEvent message)
|
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
var blocklist = new Blocklist
|
2020-01-03 12:49:24 +00:00
|
|
|
{
|
|
|
|
ArtistId = message.ArtistId,
|
|
|
|
AlbumIds = message.AlbumIds,
|
|
|
|
SourceTitle = message.SourceTitle,
|
|
|
|
Quality = message.Quality,
|
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
PublishedDate = DateTime.Parse(message.Data.GetValueOrDefault("publishedDate")),
|
|
|
|
Size = long.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")
|
2017-09-04 02:20:56 +00:00
|
|
|
};
|
2013-10-23 06:36:31 +00:00
|
|
|
|
2024-02-21 04:12:45 +00:00
|
|
|
if (Enum.TryParse(message.Data.GetValueOrDefault("indexerFlags"), true, out IndexerFlags flags))
|
|
|
|
{
|
|
|
|
blocklist.IndexerFlags = flags;
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:35:06 +00:00
|
|
|
_blocklistRepository.Insert(blocklist);
|
2013-10-23 06:36:31 +00:00
|
|
|
}
|
2014-02-01 22:07:30 +00:00
|
|
|
|
2021-01-24 19:41:48 +00:00
|
|
|
public void HandleAsync(ArtistsDeletedEvent message)
|
2014-02-01 22:07:30 +00:00
|
|
|
{
|
2021-08-19 21:35:06 +00:00
|
|
|
_blocklistRepository.DeleteForArtists(message.Artists.Select(x => x.Id).ToList());
|
2014-02-01 22:07:30 +00:00
|
|
|
}
|
2013-10-23 06:36:31 +00:00
|
|
|
}
|
|
|
|
}
|