summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Web/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-09-24 01:10:17 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-09-24 01:10:17 -0400
commitd3f0c8e4d2d9ada099cba67c359c5df1d69a1257 (patch)
tree992e6bbf83bb19357f7486f902998fec53c9f214 /src/StardewModdingAPI.Web/Framework
parent0863f9b7e5f165f2b1db8750b20ed35bc0c3701a (diff)
downloadSMAPI-d3f0c8e4d2d9ada099cba67c359c5df1d69a1257.tar.gz
SMAPI-d3f0c8e4d2d9ada099cba67c359c5df1d69a1257.tar.bz2
SMAPI-d3f0c8e4d2d9ada099cba67c359c5df1d69a1257.zip
add support for update checks from the Chucklefish mod site (#336)
Diffstat (limited to 'src/StardewModdingAPI.Web/Framework')
-rw-r--r--src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs18
-rw-r--r--src/StardewModdingAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs94
-rw-r--r--src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs4
3 files changed, 113 insertions, 3 deletions
diff --git a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
index 5d55ba18..8ca555a3 100644
--- a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
+++ b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
@@ -13,12 +13,28 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels
public int CacheMinutes { get; set; }
/****
+ ** Chucklefish mod site
+ ****/
+ /// <summary>The repository key for the Chucklefish mod site.</summary>
+ public string ChucklefishKey { get; set; }
+
+ /// <summary>The user agent for the Chucklefish API client, where {0} is the SMAPI version.</summary>
+ public string ChucklefishUserAgent { get; set; }
+
+ /// <summary>The base URL for the Chucklefish mod site.</summary>
+ public string ChucklefishBaseUrl { get; set; }
+
+ /// <summary>The URL for a mod page on the Chucklefish mod site excluding the <see cref="GitHubBaseUrl"/>, where {0} is the mod ID.</summary>
+ public string ChucklefishModPageUrlFormat { get; set; }
+
+
+ /****
** GitHub
****/
/// <summary>The repository key for Nexus Mods.</summary>
public string GitHubKey { get; set; }
- /// <summary>The user agent for the GitHub API client.</summary>
+ /// <summary>The user agent for the GitHub API client, where {0} is the SMAPI version.</summary>
public string GitHubUserAgent { get; set; }
/// <summary>The base URL for the GitHub API.</summary>
diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs
new file mode 100644
index 00000000..59d7f3ba
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using HtmlAgilityPack;
+using Pathoschild.Http.Client;
+using StardewModdingAPI.Models;
+
+namespace StardewModdingAPI.Web.Framework.ModRepositories
+{
+ /// <summary>An HTTP client for fetching mod metadata from the Chucklefish mod site.</summary>
+ internal class ChucklefishRepository : IModRepository
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The underlying HTTP client.</summary>
+ private readonly IClient Client;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The unique key for this vendor.</summary>
+ public string VendorKey { get; }
+
+ /// <summary>The base URL for the Chucklefish mod site.</summary>
+ public string BaseUrl { get; }
+
+ /// <summary>The URL for a mod page excluding the base URL, where {0} is the mod ID.</summary>
+ public string ModPageUrlFormat { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="vendorKey">The unique key for this vendor.</param>
+ /// <param name="userAgent">The user agent for the API client.</param>
+ /// <param name="baseUrl">The base URL for the Chucklefish mod site.</param>
+ /// <param name="modPageUrlFormat">The URL for a mod page excluding the <paramref name="baseUrl"/>, where {0} is the mod ID.</param>
+ public ChucklefishRepository(string vendorKey, string userAgent, string baseUrl, string modPageUrlFormat)
+ {
+ this.VendorKey = vendorKey;
+ this.BaseUrl = baseUrl;
+ this.ModPageUrlFormat = modPageUrlFormat;
+ this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
+ }
+
+ /// <summary>Get metadata about a mod in the repository.</summary>
+ /// <param name="id">The mod ID in this repository.</param>
+ public async Task<ModInfoModel> GetModInfoAsync(string id)
+ {
+ try
+ {
+ // fetch HTML
+ string html;
+ try
+ {
+ html = await this.Client
+ .GetAsync(string.Format(this.ModPageUrlFormat, id))
+ .AsString();
+ }
+ catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
+ {
+ return new ModInfoModel("Found no mod with this ID.");
+ }
+
+ // parse HTML
+ var doc = new HtmlDocument();
+ doc.LoadHtml(html);
+
+ // extract mod info
+ string url = new UriBuilder(new Uri(this.BaseUrl)) { Path = string.Format(this.ModPageUrlFormat, id) }.Uri.ToString();
+ string name = doc.DocumentNode.SelectSingleNode("//meta[@name='twitter:title']").Attributes["content"].Value;
+ if (name.StartsWith("[SMAPI] "))
+ name = name.Substring("[SMAPI] ".Length);
+ string version = doc.DocumentNode.SelectSingleNode("//h1/span").InnerText;
+
+ // create model
+ return new ModInfoModel(name, version, url);
+ }
+ catch (Exception ex)
+ {
+ return new ModInfoModel(ex.ToString());
+ }
+ }
+
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
+ public void Dispose()
+ {
+ this.Client.Dispose();
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs
index 421220de..b08e8b4d 100644
--- a/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs
+++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs
@@ -33,7 +33,7 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
/// <param name="vendorKey">The unique key for this vendor.</param>
/// <param name="baseUrl">The base URL for the Nexus Mods API.</param>
/// <param name="releaseUrlFormat">The URL for a Nexus Mods API query excluding the <paramref name="baseUrl"/>, where {0} is the mod ID.</param>
- /// <param name="userAgent">The user agent for the GitHub API client.</param>
+ /// <param name="userAgent">The user agent for the API client.</param>
/// <param name="acceptHeader">The Accept header value expected by the GitHub API.</param>
/// <param name="username">The username with which to authenticate to the GitHub API.</param>
/// <param name="password">The password with which to authenticate to the GitHub API.</param>
@@ -43,7 +43,7 @@ namespace StardewModdingAPI.Web.Framework.ModRepositories
this.ReleaseUrlFormat = releaseUrlFormat;
this.Client = new FluentClient(baseUrl)
- .SetUserAgent(string.Format(userAgent, this.GetType().Assembly.GetName().Version))
+ .SetUserAgent(userAgent)
.AddDefault(req => req.WithHeader("Accept", acceptHeader));
if (!string.IsNullOrWhiteSpace(username))
this.Client = this.Client.SetBasicAuthentication(username, password);