2018-03-14 20:41:36 +00:00
|
|
|
using System.Net;
|
2014-09-07 11:39:00 +00:00
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
using NzbDrone.Common.Http;
|
|
|
|
using NzbDrone.Core.Exceptions;
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
using NzbDrone.Core.Organizer;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
using NzbDrone.Core.ThingiProvider;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
using NLog;
|
2014-09-11 20:24:00 +00:00
|
|
|
using NzbDrone.Core.RemotePathMappings;
|
2014-09-07 11:39:00 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download
|
|
|
|
{
|
|
|
|
public abstract class UsenetClientBase<TSettings> : DownloadClientBase<TSettings>
|
|
|
|
where TSettings : IProviderConfig, new()
|
|
|
|
{
|
|
|
|
protected readonly IHttpClient _httpClient;
|
2019-02-24 17:16:12 +00:00
|
|
|
private readonly IValidateNzbs _nzbValidationService;
|
2014-09-07 11:39:00 +00:00
|
|
|
|
|
|
|
protected UsenetClientBase(IHttpClient httpClient,
|
2014-09-11 20:24:00 +00:00
|
|
|
IConfigService configService,
|
2018-04-16 22:22:54 +00:00
|
|
|
INamingConfigService namingConfigService,
|
2014-09-11 20:24:00 +00:00
|
|
|
IDiskProvider diskProvider,
|
|
|
|
IRemotePathMappingService remotePathMappingService,
|
2019-02-24 17:16:12 +00:00
|
|
|
IValidateNzbs nzbValidationService,
|
2014-09-11 20:24:00 +00:00
|
|
|
Logger logger)
|
2018-04-16 22:22:54 +00:00
|
|
|
: base(configService, namingConfigService, diskProvider, remotePathMappingService, logger)
|
2014-09-07 11:39:00 +00:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
2019-02-24 17:16:12 +00:00
|
|
|
_nzbValidationService = nzbValidationService;
|
2014-09-07 11:39:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public override DownloadProtocol Protocol => DownloadProtocol.Usenet;
|
2014-09-07 11:39:00 +00:00
|
|
|
|
2017-01-05 00:06:18 +00:00
|
|
|
protected abstract string AddFromNzbFile(RemoteMovie remoteMovie, string filename, byte[] fileContents);
|
2017-01-02 18:20:32 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
public override string Download(RemoteMovie remoteMovie)
|
2017-01-02 18:20:32 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var url = remoteMovie.Release.DownloadUrl;
|
2018-08-05 14:28:05 +00:00
|
|
|
var filename = FileNameBuilder.CleanFileName(remoteMovie.Release.Title) + ".nzb";
|
2017-01-02 18:20:32 +00:00
|
|
|
|
|
|
|
byte[] nzbData;
|
|
|
|
|
2014-09-07 11:39:00 +00:00
|
|
|
try
|
|
|
|
{
|
2014-09-12 22:39:42 +00:00
|
|
|
nzbData = _httpClient.Get(new HttpRequest(url)).ResponseData;
|
2015-07-22 18:33:20 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Debug("Downloaded nzb for movie '{0}' finished ({1} bytes from {2})", remoteMovie.Release.Title, nzbData.Length, url);
|
2014-09-07 11:39:00 +00:00
|
|
|
}
|
2015-06-27 09:43:17 +00:00
|
|
|
catch (HttpException ex)
|
|
|
|
{
|
2019-06-14 03:54:25 +00:00
|
|
|
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, "Downloading nzb file for movie '{0}' failed since it no longer exists ({1})", remoteMovie.Release.Title, url);
|
|
|
|
throw new ReleaseUnavailableException(remoteMovie.Release, "Downloading torrent failed", ex);
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:15:41 +00:00
|
|
|
if ((int)ex.Response.StatusCode == 429)
|
|
|
|
{
|
|
|
|
_logger.Error("API Grab Limit reached for {0}", url);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Error(ex, "Downloading nzb for movie '{0}' failed ({1})", remoteMovie.Release.Title, url);
|
2016-06-14 21:15:41 +00:00
|
|
|
}
|
2015-06-27 09:43:17 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
throw new ReleaseDownloadException(remoteMovie.Release, "Downloading nzb failed", ex);
|
2015-06-27 09:43:17 +00:00
|
|
|
}
|
2014-09-07 11:39:00 +00:00
|
|
|
catch (WebException ex)
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Error(ex, "Downloading nzb for movie '{0}' failed ({1})", remoteMovie.Release.Title, url);
|
2014-09-07 11:39:00 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
throw new ReleaseDownloadException(remoteMovie.Release, "Downloading nzb failed", ex);
|
2014-09-07 11:39:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 17:16:12 +00:00
|
|
|
_nzbValidationService.Validate(filename, nzbData);
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Info("Adding report [{0}] to the queue.", remoteMovie.Release.Title);
|
|
|
|
return AddFromNzbFile(remoteMovie, filename, nzbData);
|
2014-09-07 11:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|