summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web')
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs4
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs44
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs5
-rw-r--r--src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs2
-rw-r--r--src/SMAPI.Web/Startup.cs3
-rw-r--r--src/SMAPI.Web/appsettings.json1
6 files changed, 52 insertions, 7 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs
index cd52c72b..4ecf2f76 100644
--- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs
+++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
+using StardewModdingAPI.Toolkit;
namespace StardewModdingAPI.Web.Framework.Clients.Nexus
{
@@ -14,6 +15,9 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
/// <summary>The mod's semantic version number.</summary>
public string Version { get; set; }
+ /// <summary>The latest file version.</summary>
+ public ISemanticVersion LatestFileVersion { get; set; }
+
/// <summary>The mod's web URL.</summary>
[JsonProperty("mod_page_uri")]
public string Url { get; set; }
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
*********/
- /// <summary>The URL for a Nexus web page excluding the base URL, where {0} is the mod ID.</summary>
+ /// <summary>The URL for a Nexus mod page for the user, excluding the base URL, where {0} is the mod ID.</summary>
private readonly string ModUrlFormat;
+ /// <summary>The URL for a Nexus mod page to scrape for versions, excluding the base URL, where {0} is the mod ID.</summary>
+ public string ModScrapeUrlFormat { get; set; }
+
/// <summary>The underlying HTTP client.</summary>
private readonly IClient Client;
@@ -25,10 +31,12 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
/// <summary>Construct an instance.</summary>
/// <param name="userAgent">The user agent for the Nexus Mods API client.</param>
/// <param name="baseUrl">The base URL for the Nexus Mods site.</param>
- /// <param name="modUrlFormat">The URL for a Nexus Mods web page excluding the <paramref name="baseUrl"/>, where {0} is the mod ID.</param>
- public NexusWebScrapeClient(string userAgent, string baseUrl, string modUrlFormat)
+ /// <param name="modUrlFormat">The URL for a Nexus Mods mod page for the user, excluding the <paramref name="baseUrl"/>, where {0} is the mod ID.</param>
+ /// <param name="modScrapeUrlFormat">The URL for a Nexus mod page to scrape for versions, excluding the base URL, where {0} is the mod ID.</param>
+ 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<string> rawVersions = new List<string>();
+ 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
};
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
index 9452fdf9..ae8f18d2 100644
--- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
+++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs
@@ -50,9 +50,12 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
/// <summary>The base URL for the Nexus Mods API.</summary>
public string NexusBaseUrl { get; set; }
- /// <summary>The URL for a Nexus Mods API query excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary>
+ /// <summary>The URL for a Nexus mod page for the user, excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary>
public string NexusModUrlFormat { get; set; }
+ /// <summary>The URL for a Nexus mod page to scrape for versions, excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary>
+ public string NexusModScrapeUrlFormat { get; set; }
+
/****
** Pastebin
****/
diff --git a/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs
index 6cac6b8f..4afcda10 100644
--- a/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs
+++ b/src/SMAPI.Web/Framework/ModRepositories/NexusRepository.cs
@@ -43,7 +43,7 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
return new ModInfoModel("Found no mod with this ID.");
if (mod.Error != null)
return new ModInfoModel(mod.Error);
- return new ModInfoModel(name: mod.Name, version: this.NormaliseVersion(mod.Version), url: mod.Url);
+ return new ModInfoModel(name: mod.Name, version: this.NormaliseVersion(mod.Version), previewVersion: mod.LatestFileVersion?.ToString(), url: mod.Url);
}
catch (Exception ex)
{
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index 2019d6db..58584348 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -85,7 +85,8 @@ namespace StardewModdingAPI.Web
services.AddSingleton<INexusClient>(new NexusWebScrapeClient(
userAgent: userAgent,
baseUrl: api.NexusBaseUrl,
- modUrlFormat: api.NexusModUrlFormat
+ modUrlFormat: api.NexusModUrlFormat,
+ modScrapeUrlFormat: api.NexusModScrapeUrlFormat
));
services.AddSingleton<IPastebinClient>(new PastebinClient(
diff --git a/src/SMAPI.Web/appsettings.json b/src/SMAPI.Web/appsettings.json
index fda77183..2f87dbe5 100644
--- a/src/SMAPI.Web/appsettings.json
+++ b/src/SMAPI.Web/appsettings.json
@@ -32,6 +32,7 @@
"NexusBaseUrl": "https://www.nexusmods.com/stardewvalley/",
"NexusModUrlFormat": "mods/{0}",
+ "NexusModScrapeUrlFormat": "mods/{0}?tab=files",
"PastebinBaseUrl": "https://pastebin.com/",
"PastebinUserKey": null, // see top note