From e55e894586a1990fd0e9e38205328a09700de97b Mon Sep 17 00:00:00 2001 From: David Smith Date: Mon, 21 Sep 2015 11:40:44 -0500 Subject: [PATCH] Add handling of parsing the post ID from threadmark links that don't include the post ID at all (ie: the very first post in the thread). If no ID is provided when requesting a post from a page, return the first post. Fixes issue where the only threadmark of a thread is the very first post of the thread. --- TallyCore/Adapters/XenForoAdapter.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/TallyCore/Adapters/XenForoAdapter.cs b/TallyCore/Adapters/XenForoAdapter.cs index e541318e..24566880 100644 --- a/TallyCore/Adapters/XenForoAdapter.cs +++ b/TallyCore/Adapters/XenForoAdapter.cs @@ -437,11 +437,14 @@ private HtmlNode GetPostFromPageById(HtmlDocument page, string id) if (id == null) throw new ArgumentNullException(nameof(id)); + var postsList = GetPostsFromPage(page); + + if (id == "") + return postsList.FirstOrDefault(); + if (!id.StartsWith("post-")) id = "post-" + id; - var postsList = GetPostsFromPage(page); - return postsList.FirstOrDefault(a => a.Id == id); } #endregion @@ -576,18 +579,28 @@ private string GetPostIdFromUrl(string url) if (url == null) throw new ArgumentNullException(nameof(url)); + // For links directly to post ID // Format: https://forums.sufficientvelocity.com/posts/4062860/ Regex postLinkRegex = new Regex(@"posts/(?\d+)/"); var m = postLinkRegex.Match(url); if (m.Success) return m.Groups["postId"].Value; + // For long-form links. May not include page number if the post is on the first page of the thread. // Format: https://forums.sufficientvelocity.com/threads/a-villain-in-a-world-of-heroes.18001/page-109#post-4062860 + // Format: https://forums.sufficientvelocity.com/threads/a-villain-in-a-world-of-heroes.18001/#post-4062860 postLinkRegex = new Regex(@"/(page-\d+)?#post-(?\d+)"); m = postLinkRegex.Match(url); if (m.Success) return m.Groups["postId"].Value; + // Long form link for the very first post in the thread. + // Format: https://forums.sufficientvelocity.com/threads/a-villain-in-a-world-of-heroes.18001/ + postLinkRegex = new Regex(@"/threads/[^\/]+/$"); + m = postLinkRegex.Match(url); + if (m.Success) + return ""; + throw new ArgumentException("Unable to extract post ID from link:\n" + url, nameof(url)); }