Skip to content

Commit

Permalink
torrentday: use cookie login method. resolves #8912 (#8931)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngosang committed Jun 9, 2020
1 parent 6949dde commit 39740d1
Showing 1 changed file with 16 additions and 69 deletions.
85 changes: 16 additions & 69 deletions src/Jackett.Common/Indexers/TorrentDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AngleSharp.Html.Parser;
using Jackett.Common.Helpers;
using Jackett.Common.Models;
using Jackett.Common.Models.IndexerConfig;
Expand All @@ -20,8 +19,6 @@ namespace Jackett.Common.Indexers
[ExcludeFromCodeCoverage]
public class TorrentDay : BaseWebIndexer
{
private string StartPageUrl => SiteLink + "login.php";
private string LoginUrl => SiteLink + "tak3login.php";
private string SearchUrl => SiteLink + "t.json";

public override string[] AlternativeSiteLinks { get; protected set; } = {
Expand All @@ -47,7 +44,7 @@ public class TorrentDay : BaseWebIndexer
"https://www.td.af/"
};

private new ConfigurationDataRecaptchaLogin configData => (ConfigurationDataRecaptchaLogin)base.configData;
private new ConfigurationDataCookie configData => (ConfigurationDataCookie)base.configData;

public TorrentDay(IIndexerConfigurationService configService, WebClient wc, Logger l, IProtectionService ps)
: base(id: "torrentday",
Expand All @@ -63,7 +60,7 @@ public TorrentDay(IIndexerConfigurationService configService, WebClient wc, Logg
client: wc,
logger: l,
p: ps,
configData: new ConfigurationDataRecaptchaLogin(
configData: new ConfigurationDataCookie(
"Make sure you get the cookies from the same torrent day domain as configured above."))
{
Encoding = Encoding.UTF8;
Expand Down Expand Up @@ -121,76 +118,26 @@ public TorrentDay(IIndexerConfigurationService configService, WebClient wc, Logg
AddCategoryMapping(15, TorznabCatType.XXXPacks, "XXX/Packs");
}

public override async Task<ConfigurationData> GetConfigurationForSetup()
{
var loginPage = await RequestStringWithCookies(StartPageUrl, string.Empty);
if (loginPage.IsRedirect)
loginPage = await RequestStringWithCookies(loginPage.RedirectingTo, string.Empty);
if (loginPage.IsRedirect)
loginPage = await RequestStringWithCookies(loginPage.RedirectingTo, string.Empty);

var parser = new HtmlParser();
var dom = parser.ParseDocument(loginPage.Content);
var result = configData;

//result.CookieHeader.Value = loginPage.Cookies;
UpdateCookieHeader(loginPage.Cookies); // update cookies instead of replacing them, see #3717
result.Captcha.SiteKey = dom.QuerySelector(".g-recaptcha")?.GetAttribute("data-sitekey");
result.Captcha.Version = "2";
return result;
}

public override async Task<IndexerConfigurationStatus> ApplyConfiguration(JToken configJson)
{
LoadValuesFromJson(configJson);
var pairs = new Dictionary<string, string> {
{ "username", configData.Username.Value },
{ "password", configData.Password.Value },
{ "g-recaptcha-response", configData.Captcha.Value }
};

if (!string.IsNullOrWhiteSpace(configData.Captcha.Cookie))
CookieHeader = configData.Cookie.Value;
try
{
// Cookie was manually supplied
CookieHeader = configData.Captcha.Cookie;
try
{
var results = await PerformQuery(new TorznabQuery());
if (!results.Any())
throw new Exception("Found 0 results in the tracker");
var results = await PerformQuery(new TorznabQuery());
if (!results.Any())
throw new Exception("Found 0 results in the tracker");

IsConfigured = true;
SaveConfig();
return IndexerConfigurationStatus.Completed;
}
catch (Exception e)
{
IsConfigured = false;
throw new Exception("Your cookie did not work: " + e.Message);
}
IsConfigured = true;
SaveConfig();
return IndexerConfigurationStatus.Completed;
}

var result = await RequestLoginAndFollowRedirect(LoginUrl, pairs, configData.CookieHeader.Value, true, null, LoginUrl);
await ConfigureIfOK(result.Cookies, result.Content?.Contains("logout.php") == true, () =>
catch (Exception e)
{
var errorMessage = result.Content;
var parser = new HtmlParser();
var dom = parser.ParseDocument(result.Content);
var messageEl = dom.QuerySelector("#login");
if (messageEl != null)
{
foreach (var child in messageEl.QuerySelectorAll("form"))
child.Remove();
errorMessage = messageEl.TextContent.Trim();
}
if (string.IsNullOrWhiteSpace(errorMessage) && result.IsRedirect)
errorMessage = $"Got a redirect to {result.RedirectingTo}, please adjust your the alternative link";
throw new ExceptionWithConfigData(errorMessage, configData);
});
return IndexerConfigurationStatus.RequiresTesting;
IsConfigured = false;
throw new Exception("Your cookie did not work: " + e.Message);
}
}

protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query)
Expand All @@ -213,9 +160,9 @@ protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuer
// Check for being logged out
if (results.IsRedirect)
if (results.RedirectingTo.Contains("login.php"))
throw new ExceptionWithConfigData("Login failed, please reconfigure the tracker to update the cookies", configData);
throw new Exception("The user is not logged in. It is possible that the cookie has expired or you made a mistake when copying it. Please check the settings.");
else
throw new ExceptionWithConfigData($"Got a redirect to {results.RedirectingTo}, please adjust your the alternative link", configData);
throw new Exception($"Got a redirect to {results.RedirectingTo}, please adjust your the alternative link");

try
{
Expand Down

0 comments on commit 39740d1

Please sign in to comment.