torrentscsv: removed fake categories, fix link, code clean up (#8469)

This commit is contained in:
Diego Heras 2020-05-05 09:09:53 +02:00 committed by GitHub
parent cfd70ca1a7
commit 6d7ca6b4f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 109 deletions

View File

@ -16,44 +16,32 @@ using NLog;
namespace Jackett.Common.Indexers
{
[ExcludeFromCodeCoverage]
public class Torrentscsv : BaseWebIndexer
public class TorrentsCSV : BaseWebIndexer
{
private string ApiEndpoint => SiteLink + "service/search";
private new ConfigurationData configData
{
get => base.configData;
set => base.configData = value;
}
private new ConfigurationData configData => base.configData;
public Torrentscsv(IIndexerConfigurationService configService, WebClient wc, Logger l, IProtectionService ps) : base(
name: "Torrents.csv",
description: "Torrents.csv is a self-hostable, open source torrent search engine and database",
link: "https://torrents-csv.ml/",
caps: new TorznabCapabilities
{
SupportsImdbMovieSearch = true
},
configService: configService,
client: wc,
logger: l,
p: ps,
configData: new ConfigurationData())
public TorrentsCSV(IIndexerConfigurationService configService, WebClient wc, Logger l, IProtectionService ps)
: base("Torrents.csv",
description: "Torrents.csv is a self-hostable, open source torrent search engine and database",
link: "https://torrents-csv.ml/",
caps: new TorznabCapabilities
{
SupportsImdbMovieSearch = true
},
configService: configService,
client: wc,
logger: l,
p: ps,
configData: new ConfigurationData())
{
Encoding = Encoding.UTF8;
Language = "en-us";
Type = "public";
// dummy mappings for sonarr, radarr, etc since torrents.csv doesnt return categories
AddCategoryMapping(1000, TorznabCatType.Console);
AddCategoryMapping(2000, TorznabCatType.Movies);
AddCategoryMapping(3000, TorznabCatType.Audio);
AddCategoryMapping(4000, TorznabCatType.PC);
AddCategoryMapping(5000, TorznabCatType.TV);
AddCategoryMapping(6000, TorznabCatType.XXX);
AddCategoryMapping(7000, TorznabCatType.Other);
AddCategoryMapping(8000, TorznabCatType.Books);
// torrents.csv doesn't return categories
AddCategoryMapping(1, TorznabCatType.Other);
}
public override async Task<IndexerConfigurationStatus> ApplyConfiguration(JToken configJson)
@ -61,32 +49,30 @@ namespace Jackett.Common.Indexers
configData.LoadValuesFromJson(configJson);
var releases = await PerformQuery(new TorznabQuery());
await ConfigureIfOK(string.Empty, releases.Count() > 0, () =>
{
throw new Exception("Error: No data returned!");
});
await ConfigureIfOK(string.Empty, releases.Any(),
() => throw new Exception("Error: 0 results found!"));
return IndexerConfigurationStatus.Completed;
}
protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query) => await PerformQuery(query, 0);
public async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query, int attempts)
{
protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query) {
var releases = new List<ReleaseInfo>();
var searchString = query.GetQueryString();
if (string.IsNullOrEmpty(searchString))
searchString = "2020";
var queryCollection = new NameValueCollection
var searchString = query.GetQueryString();
if (!string.IsNullOrWhiteSpace(searchString) && searchString.Length < 3)
return releases; // search needs at least 3 characters
if (string.IsNullOrEmpty(searchString))
searchString = DateTime.Now.Year.ToString();
var qc = new NameValueCollection
{
{ "q", searchString },
{ "size", "500" },
{ "size", "100" },
{ "type_", "torrent" }
};
var searchUrl = ApiEndpoint + "?" + queryCollection.GetQueryString();
var response = await RequestStringWithCookiesAndRetry(searchUrl, string.Empty);
var searchUrl = ApiEndpoint + "?" + qc.GetQueryString();
var response = await RequestStringWithCookiesAndRetry(searchUrl);
try
{
@ -95,16 +81,25 @@ namespace Jackett.Common.Indexers
foreach (var torrent in jsonContent)
{
if (torrent == null)
throw new Exception("Error: No data returned!");
// construct magnet link from infohash with all public trackers known to man
var title = torrent.Value<string>("name");
var size = torrent.Value<long>("size_bytes");
var seeders = torrent.Value<int>("seeders");
var leechers = torrent.Value<int>("leechers");
var grabs = ParseUtil.CoerceInt(torrent.Value<string>("completed") ?? "0");
var infohash = torrent.Value<JToken>("infohash").ToString();
// convert unix timestamp to human readable date
var publishDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
publishDate = publishDate.AddSeconds(torrent.Value<long>("created_unix"));
// construct magnet link from infohash with public trackers
// TODO move trackers to List for reuse elsewhere
// TODO dynamically generate list periodically from online tracker repositories like
// https://torrents.io/tracker-list/
// https://github.com/ngosang/trackerslist
var magnet = new Uri("magnet:?xt=urn:btih:" + torrent.Value<JToken>("infohash") +
var magnet = new Uri("magnet:?xt=urn:btih:" + infohash +
"&tr=udp://tracker.coppersurfer.tk:6969/announce" +
"&tr=udp://tracker.leechers-paradise.org:6969/announce" +
"&tr=udp://tracker.internetwarriors.net:1337/announce" +
@ -139,75 +134,25 @@ namespace Jackett.Common.Indexers
"&tr=udp://tracker.open-tracker.org:1337/announce" +
"&tr=udp://tracker.justseed.it:1337/announce");
// convert unix timestamp to human readable date
double createdunix = torrent.Value<long>("created_unix");
var dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(createdunix);
var seeders = torrent.Value<int>("seeders");
var grabs = ParseUtil.CoerceInt(torrent.Value<string>("completed") ?? "0");
var release = new ReleaseInfo
{
Title = torrent.Value<string>("name"),
MagnetUri = magnet,
// there is no comments or details link so we point to the web site instead
Comments = new Uri(SiteLink),
Title = title,
Comments = new Uri(SiteLink), // there is no comments or details link
Guid = magnet,
Link = magnet,
InfoHash = torrent.Value<JToken>("infohash").ToString(),
PublishDate = dateTime,
Seeders = seeders,
Peers = torrent.Value<int>("leechers") + seeders,
Size = torrent.Value<long>("size_bytes"),
MagnetUri = magnet,
InfoHash = infohash,
Category = new List<int> { TorznabCatType.Other.ID },
PublishDate = publishDate,
Size = size,
Grabs = grabs,
Seeders = seeders,
Peers = leechers + seeders,
DownloadVolumeFactor = 0,
UploadVolumeFactor = 1,
MinimumRatio = 1,
MinimumSeedTime = 172800, // 48 hours
DownloadVolumeFactor = 0,
UploadVolumeFactor = 1
};
//TODO isn't there already a function for this?
// dummy mappings for sonarr, radarr, etc
var categories = string.Join(";", MapTorznabCapsToTrackers(query));
if (!string.IsNullOrEmpty(categories))
{
if (categories.Contains("1000"))
{
release.Category = new List<int> { TorznabCatType.Console.ID };
}
if (categories.Contains("2000"))
{
release.Category = new List<int> { TorznabCatType.Movies.ID };
}
if (categories.Contains("3000"))
{
release.Category = new List<int> { TorznabCatType.Audio.ID };
}
if (categories.Contains("4000"))
{
release.Category = new List<int> { TorznabCatType.PC.ID };
}
if (categories.Contains("5000"))
{
release.Category = new List<int> { TorznabCatType.TV.ID };
}
if (categories.Contains("6000"))
{
release.Category = new List<int> { TorznabCatType.XXX.ID };
}
if (categories.Contains("7000"))
{
release.Category = new List<int> { TorznabCatType.Other.ID };
}
if (categories.Contains("8000"))
{
release.Category = new List<int> { TorznabCatType.Books.ID };
}
}
// for null category
else
{
release.Category = new List<int> { TorznabCatType.Other.ID };
}
releases.Add(release);
}
}