2014-05-13 17:57:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using MonoTorrent;
|
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
|
using NzbDrone.Common.Http;
|
|
|
|
|
using NzbDrone.Core.Exceptions;
|
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.TorrentInfo;
|
|
|
|
|
using NzbDrone.Core.Organizer;
|
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
using NzbDrone.Core.ThingiProvider;
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.RemotePathMappings;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download
|
|
|
|
|
{
|
|
|
|
|
public abstract class TorrentClientBase<TSettings> : DownloadClientBase<TSettings>
|
|
|
|
|
where TSettings : IProviderConfig, new()
|
|
|
|
|
{
|
|
|
|
|
protected readonly IHttpClient _httpClient;
|
|
|
|
|
protected readonly ITorrentFileInfoReader _torrentFileInfoReader;
|
|
|
|
|
|
|
|
|
|
protected TorrentClientBase(ITorrentFileInfoReader torrentFileInfoReader,
|
|
|
|
|
IHttpClient httpClient,
|
|
|
|
|
IConfigService configService,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
IRemotePathMappingService remotePathMappingService,
|
|
|
|
|
Logger logger)
|
|
|
|
|
: base(configService, diskProvider, remotePathMappingService, logger)
|
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_torrentFileInfoReader = torrentFileInfoReader;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
public override DownloadProtocol Protocol => DownloadProtocol.Torrent;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
public virtual bool PreferTorrentFile => false;
|
2016-08-05 21:09:45 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
protected abstract string AddFromMagnetLink(RemoteAlbum remoteAlbum, string hash, string magnetLink);
|
|
|
|
|
protected abstract string AddFromTorrentFile(RemoteAlbum remoteAlbum, string hash, string filename, byte[] fileContent);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
public override string Download(RemoteAlbum remoteAlbum)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var torrentInfo = remoteAlbum.Release as TorrentInfo;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
string magnetUrl = null;
|
|
|
|
|
string torrentUrl = null;
|
2015-11-03 06:21:27 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
if (remoteAlbum.Release.DownloadUrl.IsNotNullOrWhiteSpace() && remoteAlbum.Release.DownloadUrl.StartsWith("magnet:"))
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
magnetUrl = remoteAlbum.Release.DownloadUrl;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
torrentUrl = remoteAlbum.Release.DownloadUrl;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (torrentInfo != null && !torrentInfo.MagnetUrl.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
magnetUrl = torrentInfo.MagnetUrl;
|
|
|
|
|
}
|
2016-08-05 21:09:45 +00:00
|
|
|
|
|
|
|
|
|
if (PreferTorrentFile)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2016-08-05 21:09:45 +00:00
|
|
|
|
if (torrentUrl.IsNotNullOrWhiteSpace())
|
2015-03-15 00:49:11 +00:00
|
|
|
|
{
|
2016-08-05 21:09:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return DownloadFromWebUrl(remoteAlbum, torrentUrl);
|
2016-08-05 21:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (!magnetUrl.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Torrent download failed, trying magnet. ({0})", ex.Message);
|
|
|
|
|
}
|
2015-03-15 00:49:11 +00:00
|
|
|
|
}
|
2016-08-05 21:09:45 +00:00
|
|
|
|
|
|
|
|
|
if (magnetUrl.IsNotNullOrWhiteSpace())
|
2015-03-15 00:49:11 +00:00
|
|
|
|
{
|
2016-08-05 21:09:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return DownloadFromMagnetUrl(remoteAlbum, magnetUrl);
|
2016-08-05 21:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException ex)
|
2015-11-03 06:21:27 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
throw new ReleaseDownloadException(remoteAlbum.Release, "Magnet not supported by download client. ({0})", ex.Message);
|
2015-11-03 06:21:27 +00:00
|
|
|
|
}
|
2015-03-15 00:49:11 +00:00
|
|
|
|
}
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
2016-08-05 21:09:45 +00:00
|
|
|
|
else
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2016-08-05 21:09:45 +00:00
|
|
|
|
if (magnetUrl.IsNotNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return DownloadFromMagnetUrl(remoteAlbum, magnetUrl);
|
2016-08-05 21:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException ex)
|
|
|
|
|
{
|
|
|
|
|
if (torrentUrl.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
throw new ReleaseDownloadException(remoteAlbum.Release, "Magnet not supported by download client. ({0})", ex.Message);
|
2016-08-05 21:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Magnet not supported by download client, trying torrent. ({0})", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (torrentUrl.IsNotNullOrWhiteSpace())
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return DownloadFromWebUrl(remoteAlbum, torrentUrl);
|
2016-08-05 21:09:45 +00:00
|
|
|
|
}
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 21:09:45 +00:00
|
|
|
|
return null;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
private string DownloadFromWebUrl(RemoteAlbum remoteAlbum, string torrentUrl)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
byte[] torrentFile = null;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequest(torrentUrl);
|
|
|
|
|
request.Headers.Accept = "application/x-bittorrent";
|
|
|
|
|
request.AllowAutoRedirect = false;
|
|
|
|
|
|
|
|
|
|
var response = _httpClient.Get(request);
|
|
|
|
|
|
2017-07-08 12:08:47 +00:00
|
|
|
|
if (response.StatusCode == HttpStatusCode.MovedPermanently ||
|
|
|
|
|
response.StatusCode == HttpStatusCode.Found ||
|
|
|
|
|
response.StatusCode == HttpStatusCode.SeeOther)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2016-02-28 15:41:22 +00:00
|
|
|
|
var locationHeader = response.Headers.GetSingleValue("Location");
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2015-01-06 15:12:17 +00:00
|
|
|
|
_logger.Trace("Torrent request is being redirected to: {0}", locationHeader);
|
|
|
|
|
|
2014-12-13 23:56:11 +00:00
|
|
|
|
if (locationHeader != null)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2014-12-13 23:56:11 +00:00
|
|
|
|
if (locationHeader.StartsWith("magnet:"))
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return DownloadFromMagnetUrl(remoteAlbum, locationHeader);
|
2014-12-13 23:56:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return DownloadFromWebUrl(remoteAlbum, locationHeader);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
2014-12-13 23:56:11 +00:00
|
|
|
|
|
2014-12-07 07:23:11 +00:00
|
|
|
|
throw new WebException("Remote website tried to redirect without providing a location.");
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torrentFile = response.ResponseData;
|
2015-07-22 18:33:20 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
_logger.Debug("Downloading torrent for release '{0}' finished ({1} bytes from {2})", remoteAlbum.Release.Title, torrentFile.Length, torrentUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
2015-06-27 09:43:17 +00:00
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
2016-06-14 21:15:41 +00:00
|
|
|
|
if ((int)ex.Response.StatusCode == 429)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("API Grab Limit reached for {0}", torrentUrl);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
_logger.Error(ex, "Downloading torrent file for release '{0}' failed ({1})", remoteAlbum.Release.Title, torrentUrl);
|
2016-06-14 21:15:41 +00:00
|
|
|
|
}
|
2014-12-13 23:56:11 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
throw new ReleaseDownloadException(remoteAlbum.Release, "Downloading torrent failed", ex);
|
2015-06-27 09:43:17 +00:00
|
|
|
|
}
|
2014-05-13 17:57:46 +00:00
|
|
|
|
catch (WebException ex)
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
_logger.Error(ex, "Downloading torrent file for release '{0}' failed ({1})", remoteAlbum.Release.Title, torrentUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
throw new ReleaseDownloadException(remoteAlbum.Release, "Downloading torrent failed", ex);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var filename = string.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteAlbum.Release.Title));
|
2014-05-13 17:57:46 +00:00
|
|
|
|
var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile);
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var actualHash = AddFromTorrentFile(remoteAlbum, hash, filename, torrentFile);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2015-10-16 06:11:48 +00:00
|
|
|
|
if (actualHash.IsNotNullOrWhiteSpace() && hash != actualHash)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2015-10-16 06:11:48 +00:00
|
|
|
|
_logger.Debug(
|
2017-03-30 03:49:38 +00:00
|
|
|
|
"{0} did not return the expected InfoHash for '{1}', Lidarr could potentially lose track of the download in progress.",
|
2017-08-14 02:58:42 +00:00
|
|
|
|
Definition.Implementation, remoteAlbum.Release.DownloadUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return actualHash;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
private string DownloadFromMagnetUrl(RemoteAlbum remoteAlbum, string magnetUrl)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
string hash = null;
|
|
|
|
|
string actualHash = null;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
hash = new MagnetLink(magnetUrl).InfoHash.ToHex();
|
|
|
|
|
}
|
|
|
|
|
catch (FormatException ex)
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
_logger.Error(ex, "Failed to parse magnetlink for release '{0}': '{1}'", remoteAlbum.Release.Title, magnetUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hash != null)
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
actualHash = AddFromMagnetLink(remoteAlbum, hash, magnetUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 06:11:48 +00:00
|
|
|
|
if (actualHash.IsNotNullOrWhiteSpace() && hash != actualHash)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2015-10-16 06:11:48 +00:00
|
|
|
|
_logger.Debug(
|
2017-03-30 03:49:38 +00:00
|
|
|
|
"{0} did not return the expected InfoHash for '{1}', Lidarr could potentially lose track of the download in progress.",
|
2017-08-14 02:58:42 +00:00
|
|
|
|
Definition.Implementation, remoteAlbum.Release.DownloadUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return actualHash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|