parent
bbcabf0632
commit
6744e0d506
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Parser;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Rarbg
|
||||
{
|
||||
public class Rarbg : HttpIndexerBase<RarbgSettings>
|
||||
{
|
||||
private readonly IRarbgTokenProvider _tokenProvider;
|
||||
private static DateTime _lastFetch;
|
||||
|
||||
public override string Name { get { return "Rarbg"; } }
|
||||
|
||||
public override DownloadProtocol Protocol { get { return DownloadProtocol.Torrent; } }
|
||||
|
||||
public Rarbg(IRarbgTokenProvider tokenProvider, IHttpClient httpClient, IConfigService configService, IParsingService parsingService, Logger logger)
|
||||
: base(httpClient, configService, parsingService, logger)
|
||||
{
|
||||
_tokenProvider = tokenProvider;
|
||||
}
|
||||
|
||||
public override IIndexerRequestGenerator GetRequestGenerator()
|
||||
{
|
||||
return new RarbgRequestGenerator(_tokenProvider) { Settings = Settings };
|
||||
}
|
||||
|
||||
public override IParseIndexerResponse GetParser()
|
||||
{
|
||||
return new RarbgParser();
|
||||
}
|
||||
|
||||
protected override IList<ReleaseInfo> FetchPage(IndexerRequest request, IParseIndexerResponse parser)
|
||||
{
|
||||
var delay = _lastFetch + TimeSpan.FromSeconds(10) - DateTime.UtcNow;
|
||||
if (delay.TotalSeconds > 0)
|
||||
{
|
||||
Thread.Sleep(delay);
|
||||
}
|
||||
|
||||
_lastFetch = DateTime.UtcNow;
|
||||
|
||||
return base.FetchPage(request, parser);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using FluentValidation;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.ThingiProvider;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Rarbg
|
||||
{
|
||||
public class RarbgSettingsValidator : AbstractValidator<RarbgSettings>
|
||||
{
|
||||
public RarbgSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.BaseUrl).ValidRootUrl();
|
||||
}
|
||||
}
|
||||
|
||||
public class RarbgSettings : IProviderConfig
|
||||
{
|
||||
private static readonly RarbgSettingsValidator Validator = new RarbgSettingsValidator();
|
||||
|
||||
public RarbgSettings()
|
||||
{
|
||||
BaseUrl = "https://torrentapi.org";
|
||||
RankedOnly = false;
|
||||
}
|
||||
|
||||
[FieldDefinition(0, Label = "API URL", HelpText = "URL to Rarbg api, not the website.")]
|
||||
public string BaseUrl { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Ranked Only", HelpText = "Only include ranked results.")]
|
||||
public bool RankedOnly { get; set; }
|
||||
|
||||
public NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Rarbg
|
||||
{
|
||||
public interface IRarbgTokenProvider
|
||||
{
|
||||
string GetToken(RarbgSettings settings);
|
||||
}
|
||||
|
||||
public class RarbgTokenProvider : IRarbgTokenProvider
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly ICached<string> _tokenCache;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RarbgTokenProvider(IHttpClient httpClient, ICacheManager cacheManager, Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_tokenCache = cacheManager.GetCache<string>(GetType());
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string GetToken(RarbgSettings settings)
|
||||
{
|
||||
return _tokenCache.Get(settings.BaseUrl, () =>
|
||||
{
|
||||
var url = settings.BaseUrl.Trim('/') + "/pubapi.php?get_token=get_token&format=json&response_type=json";
|
||||
|
||||
var response = _httpClient.Get<JObject>(new HttpRequest(url, HttpAccept.Json));
|
||||
|
||||
return response.Resource["token"].ToString();
|
||||
}, TimeSpan.FromMinutes(14.0));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Rarbg
|
||||
{
|
||||
public class RarbgTorrent
|
||||
{
|
||||
[JsonProperty("f")]
|
||||
public string Title { get; set; }
|
||||
[JsonProperty("c")]
|
||||
public string Category { get; set; }
|
||||
[JsonProperty("d")]
|
||||
public string DownloadUrl { get; set; }
|
||||
[JsonProperty("s")]
|
||||
public int Seeders { get; set; }
|
||||
[JsonProperty("l")]
|
||||
public int Leechers { get; set; }
|
||||
[JsonProperty("t")]
|
||||
public long Size { get; set; }
|
||||
[JsonProperty("u")]
|
||||
public DateTime PublishDate { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in new issue