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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override DownloadProtocol Protocol
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DownloadProtocol.Torrent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
protected abstract string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink);
|
|
|
|
|
protected abstract string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
public override string Download(RemoteEpisode remoteEpisode)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
|
|
|
|
var torrentInfo = remoteEpisode.Release as TorrentInfo;
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
string magnetUrl = null;
|
|
|
|
|
string torrentUrl = null;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
|
|
|
|
if (remoteEpisode.Release.DownloadUrl.StartsWith("magnet:"))
|
|
|
|
|
{
|
|
|
|
|
magnetUrl = remoteEpisode.Release.DownloadUrl;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
torrentUrl = remoteEpisode.Release.DownloadUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (torrentInfo != null && !torrentInfo.MagnetUrl.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
magnetUrl = torrentInfo.MagnetUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
string hash = null;
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2014-12-13 23:56:11 +00:00
|
|
|
|
if (magnetUrl.IsNotNullOrWhiteSpace())
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
2015-03-15 00:49:11 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
hash = DownloadFromMagnetUrl(remoteEpisode, magnetUrl);
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Magnet not supported by download client, trying torrent. ({0})", ex.Message);
|
|
|
|
|
}
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hash == null && !torrentUrl.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
hash = DownloadFromWebUrl(remoteEpisode, torrentUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
private string DownloadFromWebUrl(RemoteEpisode remoteEpisode, 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);
|
|
|
|
|
|
2014-12-13 23:56:11 +00:00
|
|
|
|
if (response.StatusCode == HttpStatusCode.SeeOther || response.StatusCode == HttpStatusCode.Found)
|
2014-05-13 17:57:46 +00:00
|
|
|
|
{
|
|
|
|
|
var locationHeader = (string)response.Headers.GetValueOrDefault("Location", null);
|
|
|
|
|
|
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:"))
|
|
|
|
|
{
|
|
|
|
|
return DownloadFromMagnetUrl(remoteEpisode, locationHeader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DownloadFromWebUrl(remoteEpisode, 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
|
|
|
|
|
2015-07-23 04:28:20 +00:00
|
|
|
|
_logger.Debug("Downloading torrent for episode '{0}' finished ({1} bytes from {2})", remoteEpisode.Release.Title, torrentFile.Length, torrentUrl);
|
2014-05-13 17:57:46 +00:00
|
|
|
|
}
|
2015-06-27 09:43:17 +00:00
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
_logger.ErrorException(string.Format("Downloading torrent file for episode '{0}' failed ({1})",
|
2015-06-27 09:43:17 +00:00
|
|
|
|
remoteEpisode.Release.Title, torrentUrl), ex);
|
2014-12-13 23:56:11 +00:00
|
|
|
|
|
2015-06-27 09:43:17 +00:00
|
|
|
|
throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading torrent failed", ex);
|
|
|
|
|
}
|
2014-05-13 17:57:46 +00:00
|
|
|
|
catch (WebException ex)
|
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
_logger.ErrorException(string.Format("Downloading torrent file for episode '{0}' failed ({1})",
|
2014-05-13 17:57:46 +00:00
|
|
|
|
remoteEpisode.Release.Title, torrentUrl), ex);
|
|
|
|
|
|
|
|
|
|
throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading torrent failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var filename = string.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteEpisode.Release.Title));
|
2014-05-13 17:57:46 +00:00
|
|
|
|
var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile);
|
|
|
|
|
var actualHash = AddFromTorrentFile(remoteEpisode, hash, filename, torrentFile);
|
|
|
|
|
|
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(
|
2015-03-21 11:05:06 +00:00
|
|
|
|
"{0} did not return the expected InfoHash for '{1}', Sonarr could potentially lose track of the download in progress.",
|
2014-05-13 17:57:46 +00:00
|
|
|
|
Definition.Implementation, remoteEpisode.Release.DownloadUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return actualHash;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
private string DownloadFromMagnetUrl(RemoteEpisode remoteEpisode, 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)
|
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
_logger.ErrorException(string.Format("Failed to parse magnetlink for episode '{0}': '{1}'",
|
2014-05-13 17:57:46 +00:00
|
|
|
|
remoteEpisode.Release.Title, magnetUrl), ex);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hash != null)
|
|
|
|
|
{
|
|
|
|
|
actualHash = AddFromMagnetLink(remoteEpisode, hash, magnetUrl);
|
|
|
|
|
}
|
|
|
|
|
|
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(
|
2015-03-21 11:05:06 +00:00
|
|
|
|
"{0} did not return the expected InfoHash for '{1}', Sonarr could potentially lose track of the download in progress.",
|
2014-05-13 17:57:46 +00:00
|
|
|
|
Definition.Implementation, remoteEpisode.Release.DownloadUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return actualHash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|