From 84c406ce36254d639e15cadf901f414e3c13e3d1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 30 Nov 2018 00:15:24 -0500 Subject: add ModDrop support in API & update checks (#604) --- src/SMAPI.Web/Controllers/ModsApiController.cs | 5 +- .../Framework/Clients/ModDrop/IModDropClient.cs | 17 ++++ .../Framework/Clients/ModDrop/ModDropClient.cs | 96 ++++++++++++++++++++++ .../Framework/Clients/ModDrop/ModDropMod.cs | 24 ++++++ .../ModDrop/ResponseModels/FileDataModel.cs | 21 +++++ .../Clients/ModDrop/ResponseModels/ModDataModel.cs | 15 ++++ .../Clients/ModDrop/ResponseModels/ModListModel.cs | 11 +++ .../Clients/ModDrop/ResponseModels/ModModel.cs | 12 +++ .../Framework/ConfigModels/ApiClientsConfig.cs | 9 ++ .../Framework/ModRepositories/ModDropRepository.cs | 59 +++++++++++++ src/SMAPI.Web/Startup.cs | 7 ++ src/SMAPI.Web/appsettings.json | 3 + 12 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs create mode 100644 src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs create mode 100644 src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs (limited to 'src/SMAPI.Web') diff --git a/src/SMAPI.Web/Controllers/ModsApiController.cs b/src/SMAPI.Web/Controllers/ModsApiController.cs index f0835592..d1e81c3c 100644 --- a/src/SMAPI.Web/Controllers/ModsApiController.cs +++ b/src/SMAPI.Web/Controllers/ModsApiController.cs @@ -15,6 +15,7 @@ using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; +using StardewModdingAPI.Web.Framework.Clients.ModDrop; using StardewModdingAPI.Web.Framework.Clients.Nexus; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.Framework.ModRepositories; @@ -60,8 +61,9 @@ namespace StardewModdingAPI.Web.Controllers /// The config settings for mod update checks. /// The Chucklefish API client. /// The GitHub API client. + /// The ModDrop API client. /// The Nexus API client. - public ModsApiController(IHostingEnvironment environment, IMemoryCache cache, IOptions configProvider, IChucklefishClient chucklefish, IGitHubClient github, INexusClient nexus) + public ModsApiController(IHostingEnvironment environment, IMemoryCache cache, IOptions configProvider, IChucklefishClient chucklefish, IGitHubClient github, IModDropClient modDrop, INexusClient nexus) { this.ModDatabase = new ModToolkit().GetModDatabase(Path.Combine(environment.WebRootPath, "StardewModdingAPI.metadata.json")); ModUpdateCheckConfig config = configProvider.Value; @@ -76,6 +78,7 @@ namespace StardewModdingAPI.Web.Controllers { new ChucklefishRepository(chucklefish), new GitHubRepository(github), + new ModDropRepository(modDrop), new NexusRepository(nexus) } .ToDictionary(p => p.VendorKey); diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs new file mode 100644 index 00000000..3ede46e2 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop +{ + /// An HTTP client for fetching mod metadata from the ModDrop API. + internal interface IModDropClient : IDisposable + { + /********* + ** Methods + *********/ + /// Get metadata about a mod. + /// The ModDrop mod ID. + /// Returns the mod info if found, else null. + Task GetModAsync(long id); + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs new file mode 100644 index 00000000..19b0b24d --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs @@ -0,0 +1,96 @@ +using System.Threading.Tasks; +using Pathoschild.Http.Client; +using StardewModdingAPI.Toolkit; +using StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels; + +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop +{ + /// An HTTP client for fetching mod metadata from the ModDrop API. + internal class ModDropClient : IModDropClient + { + /********* + ** Properties + *********/ + /// The underlying HTTP client. + private readonly IClient Client; + + /// The URL for a ModDrop mod page for the user, where {0} is the mod ID. + private readonly string ModUrlFormat; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The user agent for the API client. + /// The base URL for the ModDrop API. + /// The URL for a ModDrop mod page for the user, where {0} is the mod ID. + public ModDropClient(string userAgent, string apiUrl, string modUrlFormat) + { + this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent); + this.ModUrlFormat = modUrlFormat; + } + + /// Get metadata about a mod. + /// The ModDrop mod ID. + /// Returns the mod info if found, else null. + public async Task GetModAsync(long id) + { + // get raw data + ModListModel response = await this.Client + .PostAsync("") + .WithBody(new + { + ModIDs = new[] { id }, + Files = true, + Mods = true + }) + .As(); + ModModel mod = response.Mods[id]; + if (mod.Mod?.Title == null || mod.Mod.ErrorCode.HasValue) + return null; + + // get latest versions + ISemanticVersion latest = null; + ISemanticVersion optional = null; + foreach (FileDataModel file in mod.Files) + { + if (file.IsOld || file.IsDeleted || file.IsHidden) + continue; + + if (!SemanticVersion.TryParse(file.Version, out ISemanticVersion version)) + continue; + + if (file.IsDefault) + { + if (latest == null || version.IsNewerThan(latest)) + latest = version; + } + else if (optional == null || version.IsNewerThan(optional)) + optional = version; + } + if (latest == null) + { + latest = optional; + optional = null; + } + if (optional != null && latest.IsNewerThan(optional)) + optional = null; + + // generate result + return new ModDropMod + { + Name = mod.Mod?.Title, + LatestDefaultVersion = latest, + LatestOptionalVersion = optional, + Url = string.Format(this.ModUrlFormat, id) + }; + } + + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + public void Dispose() + { + this.Client?.Dispose(); + } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs new file mode 100644 index 00000000..291fb353 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs @@ -0,0 +1,24 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop +{ + /// Mod metadata from the ModDrop API. + internal class ModDropMod + { + /********* + ** Accessors + *********/ + /// The mod name. + public string Name { get; set; } + + /// The latest default file version. + public ISemanticVersion LatestDefaultVersion { get; set; } + + /// The latest optional file version. + public ISemanticVersion LatestOptionalVersion { get; set; } + + /// The mod's web URL. + public string Url { get; set; } + + /// A user-friendly error which indicates why fetching the mod info failed (if applicable). + public string Error { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs new file mode 100644 index 00000000..fa84b287 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs @@ -0,0 +1,21 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// Metadata from the ModDrop API about a mod file. + public class FileDataModel + { + /// Whether the file is deleted. + public bool IsDeleted { get; set; } + + /// Whether the file is hidden from users. + public bool IsHidden { get; set; } + + /// Whether this is the default file for the mod. + public bool IsDefault { get; set; } + + /// Whether this is an archived file. + public bool IsOld { get; set; } + + /// The file version. + public string Version { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs new file mode 100644 index 00000000..cfdd6a4e --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs @@ -0,0 +1,15 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// Metadata about a mod from the ModDrop API. + public class ModDataModel + { + /// The mod's unique ID on ModDrop. + public int ID { get; set; } + + /// The error code, if any. + public int? ErrorCode { get; set; } + + /// The mod name. + public string Title { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs new file mode 100644 index 00000000..7f692ca1 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// A list of mods from the ModDrop API. + public class ModListModel + { + /// The mod data. + public IDictionary Mods { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs new file mode 100644 index 00000000..9f4b2c6f --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs @@ -0,0 +1,12 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// An entry in a mod list from the ModDrop API. + public class ModModel + { + /// The available file downloads. + public FileDataModel[] Files { get; set; } + + /// The mod metadata. + public ModDataModel Mod { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs index ae8f18d2..c27cadab 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs @@ -44,6 +44,15 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels /// The password with which to authenticate to the GitHub API (if any). public string GitHubPassword { get; set; } + /**** + ** ModDrop + ****/ + /// The base URL for the ModDrop API. + public string ModDropApiUrl { get; set; } + + /// The URL for a ModDrop mod page for the user, where {0} is the mod ID. + public string ModDropModPageUrl { get; set; } + /**** ** Nexus Mods ****/ diff --git a/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs new file mode 100644 index 00000000..09484aa8 --- /dev/null +++ b/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs @@ -0,0 +1,59 @@ +using System; +using System.Threading.Tasks; +using StardewModdingAPI.Toolkit.Framework.UpdateData; +using StardewModdingAPI.Web.Framework.Clients.ModDrop; + +namespace StardewModdingAPI.Web.Framework.ModRepositories +{ + /// An HTTP client for fetching mod metadata from the ModDrop API. + internal class ModDropRepository : RepositoryBase + { + /********* + ** Properties + *********/ + /// The underlying ModDrop API client. + private readonly IModDropClient Client; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The underlying Nexus Mods API client. + public ModDropRepository(IModDropClient client) + : base(ModRepositoryKey.ModDrop) + { + this.Client = client; + } + + /// Get metadata about a mod in the repository. + /// The mod ID in this repository. + public override async Task GetModInfoAsync(string id) + { + // validate ID format + if (!long.TryParse(id, out long modDropID)) + return new ModInfoModel($"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID."); + + // fetch info + try + { + ModDropMod mod = await this.Client.GetModAsync(modDropID); + if (mod == null) + 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: mod.LatestDefaultVersion?.ToString(), previewVersion: mod.LatestOptionalVersion?.ToString(), url: mod.Url); + } + catch (Exception ex) + { + return new ModInfoModel(ex.ToString()); + } + } + + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + public override void Dispose() + { + this.Client.Dispose(); + } + } +} diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs index 60a16053..8ebd9024 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -11,6 +11,7 @@ using StardewModdingAPI.Toolkit.Serialisation; using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; +using StardewModdingAPI.Web.Framework.Clients.ModDrop; using StardewModdingAPI.Web.Framework.Clients.Nexus; using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.ConfigModels; @@ -86,6 +87,12 @@ namespace StardewModdingAPI.Web password: api.GitHubPassword )); + services.AddSingleton(new ModDropClient( + userAgent: userAgent, + apiUrl: api.ModDropApiUrl, + modUrlFormat: api.ModDropModPageUrl + )); + services.AddSingleton(new NexusWebScrapeClient( userAgent: userAgent, baseUrl: api.NexusBaseUrl, diff --git a/src/SMAPI.Web/appsettings.json b/src/SMAPI.Web/appsettings.json index aba8c448..89505a45 100644 --- a/src/SMAPI.Web/appsettings.json +++ b/src/SMAPI.Web/appsettings.json @@ -35,6 +35,9 @@ "GitHubUsername": null, // see top note "GitHubPassword": null, // see top note + "ModDropApiUrl": "https://www.moddrop.com/api/mods/data", + "ModDropModPageUrl": "https://www.moddrop.com/sdv/mod/{0}", + "NexusBaseUrl": "https://www.nexusmods.com/stardewvalley/", "NexusModUrlFormat": "mods/{0}", "NexusModScrapeUrlFormat": "mods/{0}?tab=files", -- cgit