From a63708bffff43224f0054a71a58df0285597a88e Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sun, 19 Mar 2023 21:12:35 +0200 Subject: [PATCH] sharewoodapi: refactor (#14169) --- src/Jackett.Common/Indexers/Sharewood.cs | 132 +++++++++++------------ 1 file changed, 61 insertions(+), 71 deletions(-) diff --git a/src/Jackett.Common/Indexers/Sharewood.cs b/src/Jackett.Common/Indexers/Sharewood.cs index 392aeb276..8087fb680 100644 --- a/src/Jackett.Common/Indexers/Sharewood.cs +++ b/src/Jackett.Common/Indexers/Sharewood.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Threading.Tasks; +using Jackett.Common.Extensions; using Jackett.Common.Models; using Jackett.Common.Models.IndexerConfig; using Jackett.Common.Services.Interfaces; @@ -15,8 +17,6 @@ using NLog; using static Jackett.Common.Models.IndexerConfig.ConfigurationData; using WebClient = Jackett.Common.Utils.Clients.WebClient; - - namespace Jackett.Common.Indexers { [ExcludeFromCodeCoverage] @@ -51,11 +51,11 @@ namespace Jackett.Common.Indexers // requestDelay for API Limit (1 request per 2 seconds) webclient.requestDelay = 2.1; - var FreeLeechOnly = new BoolConfigurationItem("Search freeleech only"); - configData.AddDynamic("freeleechonly", FreeLeechOnly); + var freeLeechOnly = new BoolConfigurationItem("Search freeleech only"); + configData.AddDynamic("freeleechonly", freeLeechOnly); - var ReplaceMulti = new BoolConfigurationItem("Replace MULTi by another language in release name"); - configData.AddDynamic("replacemulti", ReplaceMulti); + var replaceMulti = new BoolConfigurationItem("Replace MULTi by another language in release name"); + configData.AddDynamic("replacemulti", replaceMulti); // Configure the language select option for MULTI var languageSelect = new SingleSelectConfigurationItem("Replace MULTi by this language", new Dictionary @@ -68,11 +68,10 @@ namespace Jackett.Common.Indexers {"MULTi VOSTFR", "MULTi VOSTFR"} }) { Value = "FRENCH" }; - ; configData.AddDynamic("languageid", languageSelect); - var ReplaceVostfr = new BoolConfigurationItem("Replace VOSTFR and SUBFRENCH with ENGLISH"); - configData.AddDynamic("replacevostfr", ReplaceVostfr); + var replaceVostfr = new BoolConfigurationItem("Replace VOSTFR and SUBFRENCH with ENGLISH"); + configData.AddDynamic("replacevostfr", replaceVostfr); EnableConfigurableRetryAttempts(); } @@ -119,24 +118,24 @@ namespace Jackett.Common.Indexers caps.Categories.AddCategoryMapping(14, TorznabCatType.TVOther, "Emission TV"); caps.Categories.AddCategoryMapping(15, TorznabCatType.TVOther, "Spectacle/Concert"); caps.Categories.AddCategoryMapping(16, TorznabCatType.TVSport, "Sport"); - caps.Categories.AddCategoryMapping(17, TorznabCatType.AudioOther, "Karaoké Vidéo"); + caps.Categories.AddCategoryMapping(17, TorznabCatType.AudioVideo, "Karaoké Vidéo"); caps.Categories.AddCategoryMapping(18, TorznabCatType.AudioOther, "Karaoké"); caps.Categories.AddCategoryMapping(20, TorznabCatType.Audio, "Musique"); caps.Categories.AddCategoryMapping(21, TorznabCatType.AudioOther, "Podcast"); - caps.Categories.AddCategoryMapping(22, TorznabCatType.Audio, "Sample"); + caps.Categories.AddCategoryMapping(22, TorznabCatType.AudioOther, "Sample"); caps.Categories.AddCategoryMapping(23, TorznabCatType.AudioAudiobook, "Ebook Audio"); - caps.Categories.AddCategoryMapping(24, TorznabCatType.Books, "BD"); + caps.Categories.AddCategoryMapping(24, TorznabCatType.BooksEBook, "BD"); caps.Categories.AddCategoryMapping(25, TorznabCatType.BooksComics, "Comic"); caps.Categories.AddCategoryMapping(26, TorznabCatType.BooksOther, "Manga"); caps.Categories.AddCategoryMapping(27, TorznabCatType.Books, "Livre"); caps.Categories.AddCategoryMapping(28, TorznabCatType.BooksMags, "Presse"); - caps.Categories.AddCategoryMapping(29, TorznabCatType.Audio, "Application Linux"); - caps.Categories.AddCategoryMapping(30, TorznabCatType.PC, "Application Window"); + caps.Categories.AddCategoryMapping(29, TorznabCatType.PC, "Application Linux"); + caps.Categories.AddCategoryMapping(30, TorznabCatType.PC0day, "Application Window"); caps.Categories.AddCategoryMapping(31, TorznabCatType.PCMac, "Application Mac"); caps.Categories.AddCategoryMapping(34, TorznabCatType.PCMobileiOS, "Application Smartphone/Tablette"); caps.Categories.AddCategoryMapping(34, TorznabCatType.PCMobileAndroid, "Application Smartphone/Tablette"); - caps.Categories.AddCategoryMapping(35, TorznabCatType.Other, "GPS"); - caps.Categories.AddCategoryMapping(36, TorznabCatType.Audio, "Jeux Linux"); + caps.Categories.AddCategoryMapping(35, TorznabCatType.PCMobileOther, "GPS"); + caps.Categories.AddCategoryMapping(36, TorznabCatType.PCGames, "Jeux Linux"); caps.Categories.AddCategoryMapping(37, TorznabCatType.PCGames, "Jeux Windows"); caps.Categories.AddCategoryMapping(39, TorznabCatType.ConsoleNDS, "Jeux Nintendo"); caps.Categories.AddCategoryMapping(39, TorznabCatType.ConsoleWii, "Jeux Nintendo"); @@ -149,12 +148,10 @@ namespace Jackett.Common.Indexers caps.Categories.AddCategoryMapping(45, TorznabCatType.XXXOther, "XXX Hentai"); caps.Categories.AddCategoryMapping(47, TorznabCatType.XXXImageSet, "XXX Images"); caps.Categories.AddCategoryMapping(48, TorznabCatType.XXXOther, "XXX Jeu-Vidéo"); - caps.Categories.AddCategoryMapping(50, TorznabCatType.OtherMisc, "Formation Logiciels"); caps.Categories.AddCategoryMapping(49, TorznabCatType.OtherMisc, "Formations Vidéos"); + caps.Categories.AddCategoryMapping(50, TorznabCatType.OtherMisc, "Formation Logiciels"); caps.Categories.AddCategoryMapping(51, TorznabCatType.XXXOther, "XXX Ebooks"); caps.Categories.AddCategoryMapping(52, TorznabCatType.AudioVideo, "Vidéos-Clips"); - caps.Categories.AddCategoryMapping(51, TorznabCatType.XXXOther, "XXX Ebooks"); - caps.Categories.AddCategoryMapping(51, TorznabCatType.XXXOther, "XXX Ebooks"); return caps; } @@ -176,6 +173,7 @@ namespace Jackett.Common.Indexers private bool GetReplaceMulti => ((BoolConfigurationItem)configData.GetDynamic("replacemulti")).Value; private string GetLang => ((SingleSelectConfigurationItem)configData.GetDynamic("languageid")).Value; private bool GetReplaceVostfr => ((BoolConfigurationItem)configData.GetDynamic("replacevostfr")).Value; + public override async Task ApplyConfiguration(JToken configJson) { LoadValuesFromJson(configJson); @@ -185,48 +183,53 @@ namespace Jackett.Common.Indexers var releases = await PerformQuery(new TorznabQuery()); - await ConfigureIfOK(string.Empty, releases.Any(), - () => throw new Exception("Could not find releases.")); + await ConfigureIfOK(string.Empty, releases.Any(), () => throw new Exception("Could not find releases.")); return IndexerConfigurationStatus.Completed; } protected override async Task> PerformQuery(TorznabQuery query) { - var releases = new List(); - var Mapcats = MapTorznabCapsToTrackers(query); - if (Mapcats.Count == 0) - { // NO CATEGORIES ==> RSS SEARCH - Mapcats.Add("1000"); - } - foreach (var cats in Mapcats) + var categoryMapping = MapTorznabCapsToTrackers(query).Distinct().ToList(); + + if (!categoryMapping.Any()) { + // NO CATEGORIES ==> RSS SEARCH + categoryMapping.Add("1000"); + } + var term = query.GetQueryString().Trim(); + + foreach (var categoryId in categoryMapping) + { var searchUrl = SearchUrl; - var qc = new List> // NameValueCollection don't support cat[]=19&cat[]=6 + var parameters = new NameValueCollection { - {"limit", "25"} + { "limit", categoryId != "1000" ? "25" : "100" } }; - if (Convert.ToInt32(cats) != 1000) + if (categoryId != "1000") { - qc.Add("subcategory", cats); + parameters.Set("subcategory", categoryId); } - if (query.GetQueryString() != "") + if (term.IsNotNullOrWhiteSpace()) { - qc.Add("name", query.GetQueryString()); - searchUrl = searchUrl + "/search"; + parameters.Set("name", term); + searchUrl += "/search"; } else { - searchUrl = searchUrl + "/last-torrents"; + searchUrl += "/last-torrents"; } - searchUrl = searchUrl + "?" + qc.GetQueryString(); + if (parameters.Count > 0) + { + searchUrl += $"?{parameters.GetQueryString()}"; + } var response = await RequestWithCookiesAsync(searchUrl); if (response.Status == HttpStatusCode.Unauthorized) @@ -234,7 +237,9 @@ namespace Jackett.Common.Indexers response = await RequestWithCookiesAsync(searchUrl); } else if (response.Status != HttpStatusCode.OK) + { throw new Exception($"Unknown error in search: {response.ContentString}"); + } try { @@ -243,61 +248,44 @@ namespace Jackett.Common.Indexers { var id = row.Value("id"); var link = new Uri($"{SearchUrl}/{id}/download"); - var urlStr = row.Value("slug"); - var details = new Uri($"{SiteLink}torrents/{urlStr}.{id}"); - DateTime publishDate = DateTime.Parse(row.Value("created_at"), CultureInfo.InvariantCulture); + var slug = row.Value("slug"); + var details = new Uri($"{SiteLink}torrents/{slug}.{id}"); + var cat = row.Value("subcategory_id"); - - if (Convert.ToInt32(cats) != 1000) + if (Convert.ToInt32(categoryId) != 1000) { - if (Convert.ToInt32(cats) < 8) //USE CATEGORIES - { - cat = row.Value("category_id"); - } - else //USE SUBCATEGORIES - { - cat = row.Value("subcategory_id"); - } + // USE CATEGORIES OR SUBCATEGORIES + cat = row.Value(Convert.ToInt32(categoryId) < 8 ? "category_id" : "subcategory_id"); } - long dlVolumeFactor = 1; - long ulVolumeFactor = 1; - if (row.Value("free") == true) - { - dlVolumeFactor = 0; - } - if (row.Value("doubleup") == true) - { - ulVolumeFactor = 2; - } - var sizeString = row.Value("size"); + var dlVolumeFactor = row.Value("free") ? 0 : 1; + var ulVolumeFactor = row.Value("doubleup") ? 2 : 1; + var title = row.Value("name"); //SPECIAL CASES - - if (GetFreeLeech == true && dlVolumeFactor == 1) + if (GetFreeLeech && dlVolumeFactor == 1) continue; - if (GetReplaceMulti == true) + if (GetReplaceMulti) { title = MultiRename(title, GetLang); } - if (GetReplaceVostfr == true) + if (GetReplaceVostfr) { title = VostfrRename(title, "ENGLISH"); } var release = new ReleaseInfo { - - Title = title, - Link = link, - Details = details, Guid = details, + Details = details, + Link = link, + Title = title, Category = MapTrackerCatToNewznab(cat), - PublishDate = publishDate, - Size = ParseUtil.GetBytes(sizeString), + PublishDate = DateTime.Parse(row.Value("created_at"), CultureInfo.InvariantCulture), + Size = ParseUtil.GetBytes(row.Value("size")), Grabs = row.Value("times_completed"), Seeders = row.Value("seeders"), Peers = row.Value("leechers") + row.Value("seeders"), @@ -321,12 +309,14 @@ namespace Jackett.Common.Indexers public override async Task Download(Uri link) { var response = await RequestWithCookiesAsync(link.ToString()); + if (response.Status == HttpStatusCode.Unauthorized) { response = await RequestWithCookiesAsync(link.ToString()); } else if (response.Status != HttpStatusCode.OK) throw new Exception($"Unknown error in download: {response.ContentBytes}"); + return response.ContentBytes; } }