using System; using System.Net; using System.Threading.Tasks; using HtmlAgilityPack; using Pathoschild.Http.Client; using StardewModdingAPI.Common.Models; namespace StardewModdingAPI.Web.Framework.ModRepositories { /// An HTTP client for fetching mod metadata from the Chucklefish mod site. internal class ChucklefishRepository : RepositoryBase { /********* ** Properties *********/ /// The base URL for the Chucklefish mod site. private readonly string BaseUrl; /// The URL for a mod page excluding the base URL, where {0} is the mod ID. private readonly string ModPageUrlFormat; /// The underlying HTTP client. private readonly IClient Client; /********* ** 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) : base(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 override async Task GetModInfoAsync(string id) { // validate ID format if (!uint.TryParse(id, out uint _)) return new ModInfoModel($"The value '{id}' isn't a valid Chucklefish mod ID, must be an integer ID."); // fetch info 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, this.NormaliseVersion(version), 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(); } } }