From 53a6833ab22fb41e909ed8ef50aa9262735818d9 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Jun 2018 00:16:39 -0400 Subject: return file versions from Nexus in web API (#532) --- .../Clients/Nexus/NexusWebScrapeClient.cs | 44 ++++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs') diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs index d0597965..df5a437d 100644 --- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs +++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs @@ -1,8 +1,11 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Net; using System.Threading.Tasks; using HtmlAgilityPack; using Pathoschild.Http.Client; +using StardewModdingAPI.Toolkit; namespace StardewModdingAPI.Web.Framework.Clients.Nexus { @@ -12,9 +15,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus /********* ** Properties *********/ - /// The URL for a Nexus web page excluding the base URL, where {0} is the mod ID. + /// The URL for a Nexus mod page for the user, excluding the base URL, where {0} is the mod ID. private readonly string ModUrlFormat; + /// The URL for a Nexus mod page to scrape for versions, excluding the base URL, where {0} is the mod ID. + public string ModScrapeUrlFormat { get; set; } + /// The underlying HTTP client. private readonly IClient Client; @@ -25,10 +31,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus /// Construct an instance. /// The user agent for the Nexus Mods API client. /// The base URL for the Nexus Mods site. - /// The URL for a Nexus Mods web page excluding the , where {0} is the mod ID. - public NexusWebScrapeClient(string userAgent, string baseUrl, string modUrlFormat) + /// The URL for a Nexus Mods mod page for the user, excluding the , where {0} is the mod ID. + /// The URL for a Nexus mod page to scrape for versions, excluding the base URL, where {0} is the mod ID. + public NexusWebScrapeClient(string userAgent, string baseUrl, string modUrlFormat, string modScrapeUrlFormat) { this.ModUrlFormat = modUrlFormat; + this.ModScrapeUrlFormat = modScrapeUrlFormat; this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent); } @@ -42,7 +50,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus try { html = await this.Client - .GetAsync(string.Format(this.ModUrlFormat, id)) + .GetAsync(string.Format(this.ModScrapeUrlFormat, id)) .AsString(); } catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound) @@ -76,10 +84,38 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus string name = doc.DocumentNode.SelectSingleNode("//h1")?.InnerText.Trim(); string version = doc.DocumentNode.SelectSingleNode("//ul[contains(@class, 'stats')]//li[@class='stat-version']//div[@class='stat']")?.InnerText.Trim(); + // extract file versions + List rawVersions = new List(); + foreach (var fileSection in doc.DocumentNode.SelectNodes("//div[contains(@class, 'files-tabs')]")) + { + string sectionName = fileSection.Descendants("h2").First().InnerText; + if (sectionName != "Main files" && sectionName != "Optional files") + continue; + + rawVersions.AddRange( + from statBox in fileSection.Descendants().Where(p => p.HasClass("stat-version")) + from versionStat in statBox.Descendants().Where(p => p.HasClass("stat")) + select versionStat.InnerText.Trim() + ); + } + + // choose latest file version + ISemanticVersion latestFileVersion = null; + foreach (string rawVersion in rawVersions) + { + if (!SemanticVersion.TryParse(rawVersion, out ISemanticVersion cur)) + continue; + + if (latestFileVersion == null || cur.IsNewerThan(latestFileVersion)) + latestFileVersion = cur; + } + + // yield info return new NexusMod { Name = name, Version = version, + LatestFileVersion = latestFileVersion, Url = url }; } -- cgit From 18906de0f4a31b70f21f386f65bd3fda3ec9fb13 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Jun 2018 00:33:13 -0400 Subject: only return optional Nexus version if newer (#532) --- src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs') diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs index df5a437d..1b3fa195 100644 --- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs +++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs @@ -83,6 +83,7 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus string url = this.GetModUrl(id); string name = doc.DocumentNode.SelectSingleNode("//h1")?.InnerText.Trim(); string version = doc.DocumentNode.SelectSingleNode("//ul[contains(@class, 'stats')]//li[@class='stat-version']//div[@class='stat']")?.InnerText.Trim(); + SemanticVersion.TryParse(version, out ISemanticVersion parsedVersion); // extract file versions List rawVersions = new List(); @@ -105,16 +106,19 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus { if (!SemanticVersion.TryParse(rawVersion, out ISemanticVersion cur)) continue; + if (parsedVersion != null && !cur.IsNewerThan(parsedVersion)) + continue; + if (latestFileVersion != null && !cur.IsNewerThan(latestFileVersion)) + continue; - if (latestFileVersion == null || cur.IsNewerThan(latestFileVersion)) - latestFileVersion = cur; + latestFileVersion = cur; } // yield info return new NexusMod { Name = name, - Version = version, + Version = parsedVersion?.ToString() ?? version, LatestFileVersion = latestFileVersion, Url = url }; -- cgit