summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Clients')
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs2
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs4
-rw-r--r--src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs93
3 files changed, 98 insertions, 1 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs
index 1a7011e2..adec41be 100644
--- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs
+++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusClient.cs
@@ -3,7 +3,7 @@ using Pathoschild.Http.Client;
namespace StardewModdingAPI.Web.Framework.Clients.Nexus
{
- /// <summary>An HTTP client for fetching mod metadata from Nexus Mods.</summary>
+ /// <summary>An HTTP client for fetching mod metadata from the Nexus Mods API.</summary>
internal class NexusClient : INexusClient
{
/*********
diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs
index 2b04104f..cd52c72b 100644
--- a/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs
+++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusMod.cs
@@ -17,5 +17,9 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
/// <summary>The mod's web URL.</summary>
[JsonProperty("mod_page_uri")]
public string Url { get; set; }
+
+ /// <summary>A user-friendly error which indicates why fetching the mod info failed (if applicable).</summary>
+ [JsonIgnore]
+ public string Error { get; set; }
}
}
diff --git a/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs b/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs
new file mode 100644
index 00000000..1e941e3d
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Clients/Nexus/NexusWebScrapeClient.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using HtmlAgilityPack;
+using Pathoschild.Http.Client;
+
+namespace StardewModdingAPI.Web.Framework.Clients.Nexus
+{
+ /// <summary>An HTTP client for fetching mod metadata from the Nexus website.</summary>
+ internal class NexusWebScrapeClient : INexusClient
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The URL for a Nexus web page excluding the base URL, where {0} is the mod ID.</summary>
+ private readonly string ModUrlFormat;
+
+ /// <summary>The underlying HTTP client.</summary>
+ private readonly IClient Client;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <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)
+ {
+ this.ModUrlFormat = modUrlFormat;
+ this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
+ }
+
+ /// <summary>Get metadata about a mod.</summary>
+ /// <param name="id">The Nexus mod ID.</param>
+ /// <returns>Returns the mod info if found, else <c>null</c>.</returns>
+ public async Task<NexusMod> GetModAsync(uint id)
+ {
+ // fetch HTML
+ string html;
+ try
+ {
+ html = await this.Client
+ .GetAsync(string.Format(this.ModUrlFormat, id))
+ .AsString();
+ }
+ catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
+ {
+ return null;
+ }
+
+ // parse HTML
+ var doc = new HtmlDocument();
+ doc.LoadHtml(html);
+
+ // handle Nexus error message
+ HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]");
+ if (node != null)
+ {
+ string[] errorParts = node.InnerText.Trim().Split(new[] { '\n' }, 2, System.StringSplitOptions.RemoveEmptyEntries);
+ string errorCode = errorParts[0];
+ string errorText = errorParts.Length > 1 ? errorParts[1] : null;
+ switch (errorCode.Trim().ToLower())
+ {
+ case "not found":
+ return null;
+
+ default:
+ return new NexusMod { Error = $"Nexus error: {errorCode} ({errorText})." };
+ }
+ }
+
+ // extract mod info
+ string url = new UriBuilder(this.Client.BaseClient.BaseAddress) { Path = string.Format(this.ModUrlFormat, id) }.Uri.ToString();
+ 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();
+
+ return new NexusMod
+ {
+ Name = name,
+ Version = version,
+ Url = url
+ };
+ }
+
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
+ public void Dispose()
+ {
+ this.Client?.Dispose();
+ }
+ }
+}