1
0
Fork 0
mirror of https://github.com/Jackett/Jackett synced 2024-12-23 08:17:25 +00:00

cardigann: use CheckIfLoginIsNeeded in response type is json (#14444)

This commit is contained in:
Bogdan 2023-06-11 09:54:01 +03:00 committed by GitHub
parent bcc00f91bb
commit 5640a86838
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -878,40 +878,52 @@ namespace Jackett.Common.Indexers
return true; return true;
} }
protected bool CheckIfLoginIsNeeded(WebResult Result, IHtmlDocument document) private bool CheckIfLoginIsNeeded(WebResult response)
{ {
if (Result.IsRedirect) if (response.IsRedirect)
{ {
var DomainHint = getRedirectDomainHint(Result); var domainHint = getRedirectDomainHint(response);
if (DomainHint != null)
if (domainHint != null)
{ {
var errormessage = "Got redirected to another domain. Try changing the indexer URL to " + DomainHint + "."; var errorMessage = "Got redirected to another domain. Try changing the indexer URL to " + domainHint + ".";
if (Definition.Followredirect) if (Definition.Followredirect)
{ {
configData.SiteLink.Value = DomainHint; configData.SiteLink.Value = domainHint;
SiteLink = configData.SiteLink.Value; SiteLink = configData.SiteLink.Value;
SaveConfig(); SaveConfig();
errormessage += " Updated site link, please try again."; errorMessage += " Updated site link, please try again.";
} }
throw new ExceptionWithConfigData(errormessage, configData);
throw new ExceptionWithConfigData(errorMessage, configData);
} }
logger.Error($"Redirected to: {Result.RedirectingTo}"); logger.Error($"Redirected to: {response.RedirectingTo}");
return true; return true;
} }
if (Definition.Login == null || Definition.Login.Test == null) if (Definition.Login == null || Definition.Login.Test == null)
return false;
if (Definition.Login.Test.Selector != null)
{ {
return false;
}
var contentType = response.Headers.TryGetValue("Content-Type", out var header) ? header.FirstOrDefault() : null;
if (Definition.Login.Test.Selector != null && (contentType?.Contains("text/html") ?? true))
{
var parser = new HtmlParser();
var document = parser.ParseDocument(response.ContentString);
var selection = document.QuerySelectorAll(Definition.Login.Test.Selector); var selection = document.QuerySelectorAll(Definition.Login.Test.Selector);
if (selection.Length == 0) if (selection.Length == 0)
{ {
return true; return true;
} }
} }
return false; return false;
} }
@ -1468,12 +1480,40 @@ namespace Jackett.Common.Indexers
searchUrl, method: method, headers: headers, data: queryCollection); searchUrl, method: method, headers: headers, data: queryCollection);
if (response.IsRedirect && SearchPath.Followredirect) if (response.IsRedirect && SearchPath.Followredirect)
{
await FollowIfRedirect(response); await FollowIfRedirect(response);
}
var results = response.ContentString; var results = response.ContentString;
if (SearchPath.Response != null && SearchPath.Response.Type.Equals("json")) if (SearchPath.Response != null && SearchPath.Response.Type.Equals("json"))
{ {
// check if we need to login again
var loginNeeded = CheckIfLoginIsNeeded(response);
if (loginNeeded)
{
logger.Info("CardigannIndexer({0}): Relogin required", Id);
var loginResult = await DoLogin();
if (!loginResult)
{
throw new Exception("Relogin failed");
}
await TestLogin();
response = await RequestWithCookiesAsync(searchUrl, method: method, data: queryCollection, headers: headers);
if (response.IsRedirect && SearchPath.Followredirect)
{
await FollowIfRedirect(response);
}
results = response.ContentString;
}
if (response.Status != HttpStatusCode.OK) if (response.Status != HttpStatusCode.OK)
{ {
throw new Exception($"Error Parsing Json Response: Status={response.Status} Response={results}"); throw new Exception($"Error Parsing Json Response: Status={response.Status} Response={results}");
@ -1495,7 +1535,7 @@ namespace Jackett.Common.Indexers
} }
catch (JsonReaderException ex) catch (JsonReaderException ex)
{ {
logger.Debug("Unexpected response content ({0} bytes): {1}", response.ContentBytes.Length, response.ContentString); logger.Warn("Unexpected response content ({0} bytes): {1}", response.ContentBytes.Length, response.ContentString);
throw new Exception("Error Parsing Json Response", ex); throw new Exception("Error Parsing Json Response", ex);
} }
@ -1639,37 +1679,46 @@ namespace Jackett.Common.Indexers
} }
else else
{ {
var SearchResultParser = new HtmlParser();
var SearchResultDocument = SearchResultParser.ParseDocument(results);
// check if we need to login again // check if we need to login again
var loginNeeded = CheckIfLoginIsNeeded(response, SearchResultDocument); var loginNeeded = CheckIfLoginIsNeeded(response);
if (loginNeeded) if (loginNeeded)
{ {
logger.Info(string.Format("CardigannIndexer ({0}): Relogin required", Id)); logger.Info("CardigannIndexer({0}): Relogin required", Id);
var LoginResult = await DoLogin();
if (!LoginResult) var loginResult = await DoLogin();
throw new Exception(string.Format("Relogin failed"));
if (!loginResult)
{
throw new Exception("Relogin failed");
}
await TestLogin(); await TestLogin();
response = await RequestWithCookiesAsync(searchUrl, method: method, data: queryCollection, headers: headers); response = await RequestWithCookiesAsync(searchUrl, method: method, data: queryCollection, headers: headers);
if (response.IsRedirect && SearchPath.Followredirect) if (response.IsRedirect && SearchPath.Followredirect)
{
await FollowIfRedirect(response); await FollowIfRedirect(response);
}
results = response.ContentString; results = response.ContentString;
SearchResultDocument = SearchResultParser.ParseDocument(results);
} }
var searchResultParser = new HtmlParser();
var searchResultDocument = searchResultParser.ParseDocument(results);
checkForError(response, Definition.Search.Error); checkForError(response, Definition.Search.Error);
if (Search.Preprocessingfilters != null) if (Search.Preprocessingfilters != null)
{ {
results = applyFilters(results, Search.Preprocessingfilters, variables); results = applyFilters(results, Search.Preprocessingfilters, variables);
SearchResultDocument = SearchResultParser.ParseDocument(results); searchResultDocument = searchResultParser.ParseDocument(results);
logger.Debug(string.Format("CardigannIndexer ({0}): result after preprocessingfilters: {1}", Id, results)); logger.Debug(string.Format("CardigannIndexer ({0}): result after preprocessingfilters: {1}", Id, results));
} }
var rowsSelector = applyGoTemplateText(Search.Rows.Selector, variables); var rowsSelector = applyGoTemplateText(Search.Rows.Selector, variables);
rowsDom = SearchResultDocument.QuerySelectorAll(rowsSelector); rowsDom = searchResultDocument.QuerySelectorAll(rowsSelector);
} }
var Rows = rowsDom.ToList(); var Rows = rowsDom.ToList();