Skip to content

Commit

Permalink
Rewrite threadmark locating logic again, due to changes in threadmark…
Browse files Browse the repository at this point in the history
… code.
  • Loading branch information
Kinematics committed Jun 26, 2017
1 parent 0b956d3 commit d7b37a2
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions NetTally.Core/Input/Forums/Adapters/XenForoAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,36 +455,49 @@ static IEnumerable<HtmlNode> GetThreadmarksList(IQuest quest, HtmlDocument page)

var threadmarksDiv = content.GetDescendantWithClass("div", "threadmarks");

HtmlNode mainList = threadmarksDiv.Elements("ol").FirstOrDefault(e => e.HasClass("threadmarkList"));
HtmlNode listOfThreadmarks = null;

if (mainList == null)
HtmlNode threadmarkList = threadmarksDiv.GetDescendantWithClass("threadmarkList");

if (threadmarkList != null)
{
mainList = threadmarksDiv.Elements("ul").FirstOrDefault(e => e.Elements("li").FirstOrDefault(a => a.HasClass("threadmarkItem")) != null);
// We have a .threadmarkList node. This is either an ol itself, or it will contain a ThreadmarkCategory_# ol node. We want category 1.

if (threadmarkList.Name == "ol")
{
if (threadmarkList.GetAttributeValue("class", "").Contains("ThreadmarkCategory"))
{
if (!threadmarkList.HasClass("ThreadmarkCategory_1"))
return new List<HtmlNode>();
}

listOfThreadmarks = threadmarkList;
}
else
{
listOfThreadmarks = threadmarkList.GetDescendantWithClass("ol", "ThreadmarkCategory_1");
}
}

// Return empty list if no threadmark ul or ol found.
if (mainList == null)
return new List<HtmlNode>();

// Ensure that if we have a page with a threadmark category, that it is the primary category.
if (mainList.GetAttributeValue("class", "").Contains("ThreadmarkCategory"))
else
{
if (!mainList.HasClass("ThreadmarkCategory_1"))
return new List<HtmlNode>();
// threadmarkList was null. There is no .threadmarkList node, so check for undecorated ul that contains .threadmarkItem list items.
listOfThreadmarks = threadmarksDiv.Descendants("ul").FirstOrDefault(e => e.Elements("li").Any(a => a.HasClass("threadmarkItem")));
}

if (listOfThreadmarks != null)
{
Predicate<HtmlNode> filterLambda = (n) =>
(quest.UseCustomThreadmarkFilters && quest.ThreadmarkFilter.Match(n.InnerText)) ||
(!quest.UseCustomThreadmarkFilters && DefaultThreadmarkFilter.Match(n.InnerText));

Predicate<HtmlNode> filterLambda = (n) =>
(quest.UseCustomThreadmarkFilters && quest.ThreadmarkFilter.Match(n.InnerText)) ||
(!quest.UseCustomThreadmarkFilters && DefaultThreadmarkFilter.Match(n.InnerText));

Func<HtmlNode, HtmlNode> nodeSelector = (n) => n.Element("a");
Func<HtmlNode, HtmlNode> nodeSelector = (n) => n.Element("a");

Func<HtmlNode, IEnumerable<HtmlNode>> childSelector = (i) => i.Element("ul")?.Elements("li") ?? new List<HtmlNode>();
Func<HtmlNode, IEnumerable<HtmlNode>> childSelector = (i) => i.Element("ul")?.Elements("li") ?? new List<HtmlNode>();

var results = mainList.Elements("li").TraverseList(childSelector, nodeSelector, filterLambda);
var results = listOfThreadmarks.Elements("li").TraverseList(childSelector, nodeSelector, filterLambda);

return results;
return results;
}
}
catch (ArgumentNullException e)
{
Expand Down

0 comments on commit d7b37a2

Please sign in to comment.