2020-01-20 21:15:22 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-05-03 23:35:52 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-01-20 21:15:22 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jackett.Common.Models;
|
2020-02-19 20:23:55 +00:00
|
|
|
using Jackett.Common.Models.IndexerConfig.Bespoke;
|
2020-01-20 21:15:22 +00:00
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
using Jackett.Common.Utils;
|
|
|
|
using Jackett.Common.Utils.Clients;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
namespace Jackett.Common.Indexers
|
|
|
|
{
|
2020-05-03 23:35:52 +00:00
|
|
|
[ExcludeFromCodeCoverage]
|
2020-01-20 21:15:22 +00:00
|
|
|
public class HDBitsApi : BaseWebIndexer
|
|
|
|
{
|
2020-02-25 16:08:03 +00:00
|
|
|
private string APIUrl => SiteLink + "api/";
|
2020-01-20 21:15:22 +00:00
|
|
|
|
2020-02-19 20:23:55 +00:00
|
|
|
private new ConfigurationDataHDBitsApi configData
|
2020-01-20 21:15:22 +00:00
|
|
|
{
|
2020-02-25 16:08:03 +00:00
|
|
|
get => (ConfigurationDataHDBitsApi)base.configData;
|
|
|
|
set => base.configData = value;
|
2020-01-20 21:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 22:14:21 +00:00
|
|
|
public HDBitsApi(IIndexerConfigurationService configService, WebClient wc, Logger l, IProtectionService ps,
|
|
|
|
ICacheService cs)
|
2020-05-11 19:59:28 +00:00
|
|
|
: base(id: "hdbitsapi",
|
|
|
|
name: "HDBits (API)",
|
|
|
|
description: "The HighDefinition Bittorrent Community",
|
|
|
|
link: "https://hdbits.org/",
|
|
|
|
caps: new TorznabCapabilities
|
|
|
|
{
|
2020-10-18 20:47:36 +00:00
|
|
|
TvSearchParams = new List<TvSearchParam>
|
|
|
|
{
|
|
|
|
TvSearchParam.Q, TvSearchParam.Season, TvSearchParam.Ep, TvSearchParam.TvdbId
|
|
|
|
},
|
|
|
|
MovieSearchParams = new List<MovieSearchParam>
|
|
|
|
{
|
|
|
|
MovieSearchParam.Q, MovieSearchParam.ImdbId
|
2020-10-19 21:19:10 +00:00
|
|
|
}
|
2020-05-11 19:59:28 +00:00
|
|
|
},
|
|
|
|
configService: configService,
|
|
|
|
client: wc,
|
|
|
|
logger: l,
|
|
|
|
p: ps,
|
2020-12-11 22:14:21 +00:00
|
|
|
cacheService: cs,
|
2020-05-11 19:59:28 +00:00
|
|
|
configData: new ConfigurationDataHDBitsApi())
|
2020-01-20 21:15:22 +00:00
|
|
|
{
|
|
|
|
Encoding = Encoding.UTF8;
|
|
|
|
Language = "en-us";
|
|
|
|
Type = "private";
|
|
|
|
|
|
|
|
AddCategoryMapping(6, TorznabCatType.Audio, "Audio Track");
|
|
|
|
AddCategoryMapping(3, TorznabCatType.TVDocumentary, "Documentary");
|
|
|
|
AddCategoryMapping(8, TorznabCatType.Other, "Misc/Demo");
|
|
|
|
AddCategoryMapping(1, TorznabCatType.Movies, "Movie");
|
|
|
|
AddCategoryMapping(4, TorznabCatType.Audio, "Music");
|
|
|
|
AddCategoryMapping(5, TorznabCatType.TVSport, "Sport");
|
|
|
|
AddCategoryMapping(2, TorznabCatType.TV, "TV");
|
|
|
|
AddCategoryMapping(7, TorznabCatType.XXX, "XXX");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task<IndexerConfigurationStatus> ApplyConfiguration(JToken configJson)
|
|
|
|
{
|
|
|
|
LoadValuesFromJson(configJson);
|
|
|
|
|
|
|
|
dynamic requestData = new JObject();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await MakeApiRequest("test", requestData);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
throw new ExceptionWithConfigData(e.Message, configData);
|
|
|
|
}
|
|
|
|
|
|
|
|
IsConfigured = true;
|
|
|
|
|
|
|
|
SaveConfig();
|
|
|
|
|
|
|
|
return IndexerConfigurationStatus.Completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query)
|
|
|
|
{
|
2020-04-01 19:05:33 +00:00
|
|
|
var requestData = new JObject();
|
2020-01-20 21:15:22 +00:00
|
|
|
var queryString = query.GetQueryString();
|
2020-02-10 22:16:19 +00:00
|
|
|
var imdbId = ParseUtil.GetImdbID(query.ImdbID);
|
2020-01-20 21:15:22 +00:00
|
|
|
|
|
|
|
if (imdbId != null)
|
2020-10-01 18:27:01 +00:00
|
|
|
requestData["imdb"] = new JObject
|
|
|
|
{
|
|
|
|
["id"] = imdbId
|
|
|
|
};
|
2020-09-26 23:34:20 +00:00
|
|
|
else if (query.TvdbID != null)
|
|
|
|
{
|
2020-10-01 18:27:01 +00:00
|
|
|
requestData["tvdb"] = new JObject
|
|
|
|
{
|
|
|
|
["id"] = query.TvdbID
|
|
|
|
};
|
2020-09-26 23:34:20 +00:00
|
|
|
|
|
|
|
if (query.Season != 0)
|
|
|
|
requestData["tvdb"]["season"] = query.Season;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(query.Episode))
|
|
|
|
requestData["tvdb"]["episode"] = query.Episode;
|
|
|
|
}
|
2020-01-20 21:15:22 +00:00
|
|
|
else if (!string.IsNullOrWhiteSpace(queryString))
|
|
|
|
requestData["search"] = queryString;
|
|
|
|
|
|
|
|
var categories = MapTorznabCapsToTrackers(query);
|
|
|
|
|
2020-04-07 16:17:17 +00:00
|
|
|
if (categories.Any())
|
2020-04-01 19:05:33 +00:00
|
|
|
requestData.Add("category", JToken.FromObject(categories));
|
2020-02-19 20:23:55 +00:00
|
|
|
|
2020-04-07 16:17:17 +00:00
|
|
|
if (configData.Codecs.Values.Any())
|
2020-04-01 19:05:33 +00:00
|
|
|
requestData.Add("codec", JToken.FromObject(configData.Codecs.Values.Select(int.Parse)));
|
2020-02-19 20:23:55 +00:00
|
|
|
|
2020-04-07 16:17:17 +00:00
|
|
|
if (configData.Mediums.Values.Any())
|
2020-04-01 19:05:33 +00:00
|
|
|
requestData.Add("medium", JToken.FromObject(configData.Mediums.Values.Select(int.Parse)));
|
2020-02-19 20:23:55 +00:00
|
|
|
|
2020-04-07 16:17:17 +00:00
|
|
|
if (configData.Origins.Values.Any())
|
2020-04-01 19:05:33 +00:00
|
|
|
requestData.Add("origin", JToken.FromObject(configData.Origins.Values.Select(int.Parse)));
|
2020-02-19 20:23:55 +00:00
|
|
|
|
2020-01-20 21:15:22 +00:00
|
|
|
requestData["limit"] = 100;
|
|
|
|
|
|
|
|
var response = await MakeApiRequest("torrents", requestData);
|
|
|
|
var releases = new List<ReleaseInfo>();
|
|
|
|
foreach (JObject r in response["data"])
|
|
|
|
{
|
2020-03-26 22:15:28 +00:00
|
|
|
var link = new Uri(
|
|
|
|
SiteLink + "download.php/" + (string)r["filename"] + "?id=" + (string)r["id"] + "&passkey=" +
|
|
|
|
configData.Passkey.Value);
|
|
|
|
var seeders = (int)r["seeders"];
|
|
|
|
var publishDate = DateTimeUtil.UnixTimestampToDateTime((int)r["utadded"]);
|
2020-11-08 02:11:27 +00:00
|
|
|
var details = new Uri(SiteLink + "details.php?id=" + (string)r["id"]);
|
2020-03-26 22:15:28 +00:00
|
|
|
var release = new ReleaseInfo
|
|
|
|
{
|
|
|
|
Title = (string)r["name"],
|
2020-11-08 02:11:27 +00:00
|
|
|
Details = details,
|
2020-03-26 22:15:28 +00:00
|
|
|
Link = link,
|
|
|
|
Category = MapTrackerCatToNewznab((string)r["type_category"]),
|
|
|
|
Size = (long)r["size"],
|
|
|
|
Files = (long)r["numfiles"],
|
|
|
|
Grabs = (long)r["times_completed"],
|
|
|
|
Seeders = seeders,
|
|
|
|
PublishDate = publishDate,
|
|
|
|
UploadVolumeFactor = GetUploadFactor(r),
|
|
|
|
DownloadVolumeFactor = GetDownloadFactor(r),
|
|
|
|
Guid = link,
|
|
|
|
Peers = seeders + (int)r["leechers"]
|
|
|
|
};
|
2020-01-20 21:15:22 +00:00
|
|
|
|
|
|
|
if (r.ContainsKey("imdb"))
|
|
|
|
release.Imdb = ParseUtil.GetImdbID((string)r["imdb"]["id"]);
|
|
|
|
|
|
|
|
if (r.ContainsKey("tvdb"))
|
|
|
|
release.TVDBId = (long)r["tvdb"]["id"];
|
|
|
|
|
|
|
|
releases.Add(release);
|
|
|
|
}
|
|
|
|
|
|
|
|
return releases;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:15:28 +00:00
|
|
|
private static double GetUploadFactor(JObject r) => (int)r["type_category"] == 7 ? 0 : 1;
|
|
|
|
|
|
|
|
private static double GetDownloadFactor(JObject r)
|
|
|
|
{
|
|
|
|
var halfLeechMediums = new[] { 1, 5, 4 };
|
|
|
|
// 100% Neutral Leech: all XXX content.
|
|
|
|
if ((int)r["type_category"] == 7)
|
|
|
|
return 0;
|
|
|
|
// 100% Free Leech: all blue torrents.
|
|
|
|
if ((string)r["freeleech"] == "yes")
|
|
|
|
return 0;
|
|
|
|
// 50% Free Leech: all full discs, remuxes, caps and all internal encodes.
|
|
|
|
if (halfLeechMediums.Contains((int)r["type_medium"]) || (int)r["type_origin"] == 1)
|
|
|
|
return 0.5;
|
2020-04-01 19:05:33 +00:00
|
|
|
// 25% Free Leech: all TV content that is not an internal encode.
|
2020-03-26 22:15:28 +00:00
|
|
|
if ((int)r["type_category"] == 2 && (int)r["type_origin"] != 1)
|
|
|
|
return 0.75;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-20 21:15:22 +00:00
|
|
|
private async Task<JObject> MakeApiRequest(string url, JObject requestData)
|
|
|
|
{
|
|
|
|
requestData["username"] = configData.Username.Value;
|
|
|
|
requestData["passkey"] = configData.Passkey.Value;
|
2020-10-01 18:27:01 +00:00
|
|
|
|
2020-06-11 15:09:27 +00:00
|
|
|
var response = await RequestWithCookiesAndRetryAsync(
|
|
|
|
APIUrl + url, null, RequestType.POST, null, null,
|
|
|
|
new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{"Accept", "application/json"},
|
|
|
|
{"Content-Type", "application/json"}
|
|
|
|
}, requestData.ToString(), false);
|
|
|
|
CheckSiteDown(response);
|
2020-01-20 21:15:22 +00:00
|
|
|
|
2020-10-01 18:27:01 +00:00
|
|
|
JObject json;
|
2020-01-20 21:15:22 +00:00
|
|
|
try
|
|
|
|
{
|
2020-06-09 17:36:57 +00:00
|
|
|
json = JObject.Parse(response.ContentString);
|
2020-01-20 21:15:22 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-06-09 17:36:57 +00:00
|
|
|
throw new Exception("Error while parsing json: " + response.ContentString, ex);
|
2020-01-20 21:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)json["status"] != 0)
|
|
|
|
throw new Exception("HDBits returned an error with status code " + (int)json["status"] + ": " + (string)json["message"]);
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|