2016-12-23 21:45:24 +00:00
|
|
|
|
using System.Net;
|
2017-01-02 18:20:32 +00:00
|
|
|
|
using System;
|
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;
|
|
|
|
|
|
|
|
|
|
protected UsenetClientBase(IHttpClient httpClient,
|
2014-09-11 20:24:00 +00:00
|
|
|
|
IConfigService configService,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
IRemotePathMappingService remotePathMappingService,
|
|
|
|
|
Logger logger)
|
2014-10-13 21:11:35 +00:00
|
|
|
|
: base(configService, diskProvider, remotePathMappingService, logger)
|
2014-09-07 11:39:00 +00:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
}
|
|
|
|
|
|
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(RemoteEpisode remoteEpisode, string filename, byte[] fileContents);
|
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
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
public override string Download(RemoteEpisode remoteEpisode)
|
2014-09-07 11:39:00 +00:00
|
|
|
|
{
|
|
|
|
|
var url = remoteEpisode.Release.DownloadUrl;
|
|
|
|
|
var filename = FileNameBuilder.CleanFileName(remoteEpisode.Release.Title) + ".nzb";
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
byte[] nzbData;
|
2014-09-07 11:39:00 +00:00
|
|
|
|
|
2017-01-02 18:20:32 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
nzbData = _httpClient.Get(new HttpRequest(url)).ResponseData;
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Downloaded nzb for episode '{0}' finished ({1} bytes from {2})", remoteEpisode.Release.Title, nzbData.Length, url);
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
if ((int)ex.Response.StatusCode == 429)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("API Grab Limit reached for {0}", url);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Error(ex, "Downloading nzb for episode '{0}' failed ({1})", remoteEpisode.Release.Title, url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading nzb failed", ex);
|
|
|
|
|
}
|
|
|
|
|
catch (WebException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error(ex, "Downloading nzb for episode '{0}' failed ({1})", remoteEpisode.Release.Title, url);
|
|
|
|
|
|
|
|
|
|
throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading nzb failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Info("Adding report [{0}] to the queue.", remoteEpisode.Release.Title);
|
|
|
|
|
return AddFromNzbFile(remoteEpisode, filename, nzbData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Download(RemoteMovie remoteEpisode)
|
|
|
|
|
{
|
|
|
|
|
var url = remoteEpisode.Release.DownloadUrl;
|
|
|
|
|
var filename = FileNameBuilder.CleanFileName(remoteEpisode.Release.Title) + ".nzb";
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-07-23 04:28:20 +00:00
|
|
|
|
_logger.Debug("Downloaded nzb for episode '{0}' finished ({1} bytes from {2})", remoteEpisode.Release.Title, nzbData.Length, url);
|
2014-09-07 11:39:00 +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}", url);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Error(ex, "Downloading nzb for episode '{0}' failed ({1})", remoteEpisode.Release.Title, url);
|
|
|
|
|
}
|
2015-06-27 09:43:17 +00:00
|
|
|
|
|
|
|
|
|
throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading nzb failed", ex);
|
|
|
|
|
}
|
2014-09-07 11:39:00 +00:00
|
|
|
|
catch (WebException ex)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "Downloading nzb for episode '{0}' failed ({1})", remoteEpisode.Release.Title, url);
|
2014-09-07 11:39:00 +00:00
|
|
|
|
|
|
|
|
|
throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading nzb failed", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Info("Adding report [{0}] to the queue.", remoteEpisode.Release.Title);
|
|
|
|
|
return AddFromNzbFile(remoteEpisode, filename, nzbData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|