Fixed: (PreToMe) Improved parsing, login and settings to extend UserPassTorrentBaseSettings

pull/1386/head
Bogdan 2 years ago
parent 626d777d3c
commit 6b62504916

@ -20,28 +20,31 @@ using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Indexers.Definitions
namespace NzbDrone.Core.Indexers.Definitions;
public class PreToMe : TorrentIndexerBase<PreToMeSettings>
{
public class PreToMe : TorrentIndexerBase<PreToMeSettings>
{
public override string Name => "PreToMe";
public override string[] IndexerUrls => new string[] { "https://pretome.info/" };
public override string[] IndexerUrls => new[] { "https://pretome.info/" };
public override string Description => "PreToMe is a ratioless 0Day/General tracker.";
private string LoginUrl => Settings.BaseUrl + "takelogin.php";
public override string Language => "en-US";
public override Encoding Encoding => Encoding.GetEncoding("iso-8859-1");
public override DownloadProtocol Protocol => DownloadProtocol.Torrent;
public override IndexerPrivacy Privacy => IndexerPrivacy.Private;
public override IndexerCapabilities Capabilities => SetCapabilities();
public PreToMe(IIndexerHttpClient httpClient, IEventAggregator eventAggregator, IIndexerStatusService indexerStatusService, IConfigService configService, Logger logger)
public PreToMe(IIndexerHttpClient httpClient,
IEventAggregator eventAggregator,
IIndexerStatusService indexerStatusService,
IConfigService configService,
Logger logger)
: base(httpClient, eventAggregator, indexerStatusService, configService, logger)
{
}
public override IIndexerRequestGenerator GetRequestGenerator()
{
return new PreToMeRequestGenerator() { Settings = Settings, Capabilities = Capabilities };
return new PreToMeRequestGenerator(Settings, Capabilities);
}
public override IParseIndexerResponse GetParser()
@ -53,48 +56,51 @@ namespace NzbDrone.Core.Indexers.Definitions
{
UpdateCookies(null, null);
var requestBuilder = new HttpRequestBuilder(LoginUrl)
var loginPage = await ExecuteAuth(new HttpRequest(Settings.BaseUrl + "login.php"));
var loginUrl = Settings.BaseUrl + "takelogin.php";
var requestBuilder = new HttpRequestBuilder(loginUrl)
{
LogResponseContent = true,
AllowAutoRedirect = true
AllowAutoRedirect = true,
Method = HttpMethod.Post
};
var loginPage = await ExecuteAuth(new HttpRequest(Settings.BaseUrl + "login.php"));
requestBuilder.Method = HttpMethod.Post;
requestBuilder.PostProcess += r => r.RequestTimeout = TimeSpan.FromSeconds(15);
requestBuilder.SetCookies(loginPage.GetCookies());
var authLoginRequest = requestBuilder
.AddFormParameter("returnto", "%2F")
.AddFormParameter("login_pin", Settings.Pin)
.AddFormParameter("login", "Login")
.SetCookies(loginPage.GetCookies())
.AddFormParameter("username", Settings.Username)
.AddFormParameter("password", Settings.Password)
.SetHeader("Content-Type", "multipart/form-data")
.AddFormParameter("login_pin", Settings.Pin)
.AddFormParameter("returnto", "%2F")
.AddFormParameter("login", "Login")
.SetHeader("Content-Type", "application/x-www-form-urlencoded")
.Build();
var response = await ExecuteAuth(authLoginRequest);
if (response.Content != null && response.Content.Contains("logout.php"))
if (response.Content == null)
{
UpdateCookies(response.GetCookies(), DateTime.Now + TimeSpan.FromDays(30));
_logger.Debug("PreToMe authentication succeeded");
throw new IndexerAuthException("Authentication failed. Reason: empty response.");
}
else
if (CheckIfLoginNeeded(response))
{
throw new IndexerAuthException("PreToMe authentication failed");
var parser = new HtmlParser();
var dom = parser.ParseDocument(response.Content);
var errorMessage = dom.QuerySelector("table.body_table font[color~=\"red\"]")?.TextContent.Trim();
throw new IndexerAuthException(errorMessage ?? "Unknown error message, please report.");
}
UpdateCookies(response.GetCookies(), DateTime.Now + TimeSpan.FromDays(30));
_logger.Debug("Authentication succeeded");
}
protected override bool CheckIfLoginNeeded(HttpResponse httpResponse)
{
if (httpResponse.HasHttpRedirect || !httpResponse.Content.Contains("logout.php"))
{
return true;
}
return false;
return httpResponse.HasHttpRedirect || !httpResponse.Content.Contains("logout.php");
}
private IndexerCapabilities SetCapabilities()
@ -180,40 +186,42 @@ namespace NzbDrone.Core.Indexers.Definitions
return caps;
}
}
}
public class PreToMeRequestGenerator : IIndexerRequestGenerator
{
public PreToMeSettings Settings { get; set; }
public IndexerCapabilities Capabilities { get; set; }
public class PreToMeRequestGenerator : IIndexerRequestGenerator
{
private readonly PreToMeSettings _settings;
private readonly IndexerCapabilities _capabilities;
public PreToMeRequestGenerator()
public PreToMeRequestGenerator(PreToMeSettings settings, IndexerCapabilities capabilities)
{
_settings = settings;
_capabilities = capabilities;
}
private IEnumerable<IndexerRequest> GetPagedRequests(string term, int[] categories, string imdbId = null)
{
var searchUrl = string.Format("{0}/browse.php", Settings.BaseUrl.TrimEnd('/'));
var qc = new List<KeyValuePair<string, string>> // NameValueCollection don't support cat[]=19&cat[]=6
// NameValueCollection don't support cat[]=19&cat[]=6
var parameters = new List<KeyValuePair<string, string>>
{
{ "st", "1" } // search in title
};
if (imdbId.IsNotNullOrWhiteSpace())
{
qc.Add("search", imdbId);
qc.Add("sd", "1"); // search in description
parameters.Add("search", imdbId);
parameters.Add("sd", "1"); // search in description
}
else
{
qc.Add("search", term);
parameters.Add("search", term);
}
// parse categories and tags
var catGroups = new HashSet<string>(); // HashSet instead of List to avoid duplicates
var catGroups = new HashSet<string>();
var tagGroups = new HashSet<string>();
var cats = Capabilities.Categories.MapTorznabCapsToTrackers(categories);
var cats = _capabilities.Categories.MapTorznabCapsToTrackers(categories);
foreach (var cat in cats)
{
// "cat[]=7&tags=x264"
@ -238,24 +246,33 @@ namespace NzbDrone.Core.Indexers.Definitions
// add categories
foreach (var cat in catGroups)
{
qc.Add("cat[]", cat);
parameters.Add("cat[]", cat);
}
// do not include too many tags as it'll mess with their servers
if (tagGroups.Count < 7)
{
qc.Add("tags", string.Join(",", tagGroups));
parameters.Add("tags", string.Join(",", tagGroups));
// if tags are specified match any
// if no tags are specified match all, with any we get random results
qc.Add("tf", tagGroups.Any() ? "any" : "all");
parameters.Add("tf", tagGroups.Any() ? "any" : "all");
}
searchUrl = searchUrl + "?" + qc.GetQueryString();
var searchUrl = $"{_settings.BaseUrl}browse.php";
var request = new IndexerRequest(searchUrl, HttpAccept.Html);
if (parameters.Count > 0)
{
searchUrl += $"?{parameters.GetQueryString()}";
}
request.HttpRequest.AllowAutoRedirect = false;
var request = new IndexerRequest(searchUrl, HttpAccept.Html)
{
HttpRequest =
{
AllowAutoRedirect = false
}
};
yield return request;
}
@ -264,7 +281,7 @@ namespace NzbDrone.Core.Indexers.Definitions
{
var pageableRequests = new IndexerPageableRequestChain();
pageableRequests.Add(GetPagedRequests(string.Format("{0}", searchCriteria.SanitizedSearchTerm), searchCriteria.Categories, searchCriteria.FullImdbId));
pageableRequests.Add(GetPagedRequests($"{searchCriteria.SanitizedSearchTerm}", searchCriteria.Categories, searchCriteria.FullImdbId));
return pageableRequests;
}
@ -273,7 +290,7 @@ namespace NzbDrone.Core.Indexers.Definitions
{
var pageableRequests = new IndexerPageableRequestChain();
pageableRequests.Add(GetPagedRequests(string.Format("{0}", searchCriteria.SanitizedSearchTerm), searchCriteria.Categories));
pageableRequests.Add(GetPagedRequests($"{searchCriteria.SanitizedSearchTerm}", searchCriteria.Categories));
return pageableRequests;
}
@ -282,7 +299,7 @@ namespace NzbDrone.Core.Indexers.Definitions
{
var pageableRequests = new IndexerPageableRequestChain();
pageableRequests.Add(GetPagedRequests(string.Format("{0}", searchCriteria.SanitizedTvSearchString), searchCriteria.Categories, searchCriteria.FullImdbId));
pageableRequests.Add(GetPagedRequests($"{searchCriteria.SanitizedTvSearchString}", searchCriteria.Categories, searchCriteria.FullImdbId));
return pageableRequests;
}
@ -291,7 +308,7 @@ namespace NzbDrone.Core.Indexers.Definitions
{
var pageableRequests = new IndexerPageableRequestChain();
pageableRequests.Add(GetPagedRequests(string.Format("{0}", searchCriteria.SanitizedSearchTerm), searchCriteria.Categories));
pageableRequests.Add(GetPagedRequests($"{searchCriteria.SanitizedSearchTerm}", searchCriteria.Categories));
return pageableRequests;
}
@ -300,17 +317,17 @@ namespace NzbDrone.Core.Indexers.Definitions
{
var pageableRequests = new IndexerPageableRequestChain();
pageableRequests.Add(GetPagedRequests(string.Format("{0}", searchCriteria.SanitizedSearchTerm), searchCriteria.Categories));
pageableRequests.Add(GetPagedRequests($"{searchCriteria.SanitizedSearchTerm}", searchCriteria.Categories));
return pageableRequests;
}
public Func<IDictionary<string, string>> GetCookies { get; set; }
public Action<IDictionary<string, string>, DateTime?> CookiesUpdater { get; set; }
}
}
public class PreToMeParser : IParseIndexerResponse
{
public class PreToMeParser : IParseIndexerResponse
{
private readonly PreToMeSettings _settings;
private readonly IndexerCapabilitiesCategories _categories;
@ -322,97 +339,72 @@ namespace NzbDrone.Core.Indexers.Definitions
public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
{
var torrentInfos = new List<ReleaseInfo>();
var releaseInfos = new List<ReleaseInfo>();
var parser = new HtmlParser();
var dom = parser.ParseDocument(indexerResponse.Content);
var rows = dom.QuerySelectorAll("table > tbody > tr.browse");
foreach (var row in rows)
{
var qLink = row.Children[1].QuerySelector("a");
var title = qLink.GetAttribute("title");
if (qLink.QuerySelectorAll("span").Length == 1 && title.StartsWith("NEW! |"))
{
title = title.Substring(6).Trim();
}
var qDetails = row.QuerySelector("a[href^=\"details.php?id=\"]");
var title = qDetails?.GetAttribute("title");
var infoUrl = _settings.BaseUrl + qDetails.GetAttribute("href");
var downloadUrl = _settings.BaseUrl + row.QuerySelector("a[href^=\"download.php\"]")?.GetAttribute("href");
var dateAdded = Regex.Replace(row.QuerySelector("td:nth-of-type(6)").InnerHtml, @"\<br[\s]{0,1}[\/]{0,1}\>", " ").Trim();
var seeders = ParseUtil.CoerceInt(row.QuerySelector("td:nth-of-type(10)")?.TextContent);
var leechers = ParseUtil.CoerceInt(row.QuerySelector("td:nth-of-type(11)")?.TextContent);
// TODO: Asses if we should be throwing this out
//if (!query.MatchQueryStringAND(title))
//{
// continue; // we have to skip bad titles due to tags + any word search
//}
var details = _settings.BaseUrl + qLink.GetAttribute("href");
var link = _settings.BaseUrl + row.Children[2].QuerySelector("a").GetAttribute("href");
var dateStr = Regex.Replace(row.Children[5].InnerHtml, @"\<br[\s]{0,1}[\/]{0,1}\>", " ");
var publishDate = DateTimeUtil.FromTimeAgo(dateStr);
var files = ParseUtil.CoerceInt(row.Children[3].TextContent);
var size = ParseUtil.GetBytes(row.Children[7].TextContent);
var grabs = ParseUtil.CoerceInt(row.Children[8].TextContent);
var seeders = ParseUtil.CoerceInt(row.Children[9].TextContent);
var leechers = ParseUtil.CoerceInt(row.Children[10].TextContent);
var cat = row.FirstElementChild.FirstElementChild.GetAttribute("href").Replace("browse.php?", string.Empty);
var cat = row.QuerySelector("td:nth-of-type(1) a[href^=\"browse.php\"]")?.GetAttribute("href")?.Split('?').Last();
var release = new TorrentInfo
{
Guid = infoUrl,
InfoUrl = infoUrl,
DownloadUrl = downloadUrl,
Title = title,
InfoUrl = details,
Guid = details,
DownloadUrl = link,
PublishDate = publishDate,
Size = size,
Categories = _categories.MapTrackerCatToNewznab(cat),
Files = files,
Grabs = grabs,
PublishDate = DateTimeUtil.FromTimeAgo(dateAdded),
Size = ParseUtil.GetBytes(row.QuerySelector("td:nth-of-type(8)")?.TextContent),
Files = ParseUtil.CoerceInt(row.QuerySelector("td:nth-of-type(4)")?.TextContent),
Grabs = ParseUtil.CoerceInt(row.QuerySelector("td:nth-of-type(9)")?.TextContent),
Seeders = seeders,
Peers = leechers + seeders,
MinimumRatio = 0.75,
MinimumSeedTime = 216000, // 60 hours
DownloadVolumeFactor = 0, // ratioless
UploadVolumeFactor = 1
UploadVolumeFactor = 1,
MinimumRatio = 0.75,
MinimumSeedTime = 216000 // 60 hours
};
torrentInfos.Add(release);
releaseInfos.Add(release);
}
return torrentInfos.ToArray();
return releaseInfos.ToArray();
}
public Action<IDictionary<string, string>, DateTime?> CookiesUpdater { get; set; }
}
}
public class PreToMeSettingsValidator : NoAuthSettingsValidator<PreToMeSettings>
{
public class PreToMeSettingsValidator : UserPassBaseSettingsValidator<PreToMeSettings>
{
public PreToMeSettingsValidator()
{
RuleFor(c => c.Pin).NotEmpty();
RuleFor(c => c.Username).NotEmpty();
RuleFor(c => c.Password).NotEmpty();
}
}
}
public class PreToMeSettings : NoAuthTorrentBaseSettings
{
public class PreToMeSettings : UserPassTorrentBaseSettings
{
private static readonly PreToMeSettingsValidator Validator = new ();
public PreToMeSettings()
{
Pin = "";
Username = "";
Password = "";
}
[FieldDefinition(2, Label = "Pin", HelpText = "Site Pin", Privacy = PrivacyLevel.Password)]
[FieldDefinition(4, Label = "Pin", HelpText = "Site Pin", Privacy = PrivacyLevel.Password)]
public string Pin { get; set; }
[FieldDefinition(3, Label = "Username", HelpText = "Site Username", Privacy = PrivacyLevel.UserName)]
public string Username { get; set; }
[FieldDefinition(4, Label = "Password", HelpText = "Site Password", Privacy = PrivacyLevel.Password, Type = FieldType.Password)]
public string Password { get; set; }
public override NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

Loading…
Cancel
Save