From d3f0c8e4d2d9ada099cba67c359c5df1d69a1257 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 Sep 2017 01:10:17 -0400 Subject: add support for update checks from the Chucklefish mod site (#336) --- .../Framework/ConfigModels/ModUpdateCheckConfig.cs | 18 ++++- .../ModRepositories/ChucklefishRepository.cs | 94 ++++++++++++++++++++++ .../Framework/ModRepositories/GitHubRepository.cs | 4 +- 3 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/StardewModdingAPI.Web/Framework/ModRepositories/ChucklefishRepository.cs (limited to 'src/StardewModdingAPI.Web/Framework') 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 @@ -12,13 +12,29 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /// The number of minutes update checks should be cached before refetching them. public int CacheMinutes { get; set; } + /**** + ** Chucklefish mod site + ****/ + /// The repository key for the Chucklefish mod site. + public string ChucklefishKey { get; set; } + + /// The user agent for the Chucklefish API client, where {0} is the SMAPI version. + public string ChucklefishUserAgent { get; set; } + + /// The base URL for the Chucklefish mod site. + public string ChucklefishBaseUrl { get; set; } + + /// The URL for a mod page on the Chucklefish mod site excluding the , where {0} is the mod ID. + public string ChucklefishModPageUrlFormat { get; set; } + + /**** ** GitHub ****/ /// The repository key for Nexus Mods. public string GitHubKey { get; set; } - /// The user agent for the GitHub API client. + /// The user agent for the GitHub API client, where {0} is the SMAPI version. public string GitHubUserAgent { get; set; } /// The base URL for the GitHub API. 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 +{ + /// An HTTP client for fetching mod metadata from the Chucklefish mod site. + internal class ChucklefishRepository : IModRepository + { + /********* + ** Properties + *********/ + /// The underlying HTTP client. + private readonly IClient Client; + + + /********* + ** Accessors + *********/ + /// The unique key for this vendor. + public string VendorKey { get; } + + /// The base URL for the Chucklefish mod site. + public string BaseUrl { get; } + + /// The URL for a mod page excluding the base URL, where {0} is the mod ID. + public string ModPageUrlFormat { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The unique key for this vendor. + /// The user agent for the API client. + /// The base URL for the Chucklefish mod site. + /// The URL for a mod page excluding the , where {0} is the mod ID. + 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); + } + + /// Get metadata about a mod in the repository. + /// The mod ID in this repository. + public async Task 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()); + } + } + + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + 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 /// The unique key for this vendor. /// The base URL for the Nexus Mods API. /// The URL for a Nexus Mods API query excluding the , where {0} is the mod ID. - /// The user agent for the GitHub API client. + /// The user agent for the API client. /// The Accept header value expected by the GitHub API. /// The username with which to authenticate to the GitHub API. /// The password with which to authenticate to the GitHub API. @@ -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); -- cgit