diff --git a/json-backups/sourcetreeapp-archive.csx b/json-backups/sourcetreeapp-archive.csx index 07d5f3a..56f1a91 100644 --- a/json-backups/sourcetreeapp-archive.csx +++ b/json-backups/sourcetreeapp-archive.csx @@ -1,15 +1,22 @@ #r "nuget: AngleSharp, 1.0.0-alpha-844" using System.Linq; +using System.Net.Http; +using AngleSharp.Html.Parser; + +string userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0"; + +var client = new HttpClient(); +client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent); System.Console.Out.WriteLine( "downloads(" + System.Text.Json.JsonSerializer.Serialize( - new AngleSharp.Html.Parser.HtmlParser() + new HtmlParser() .ParseDocument( - await new System.Net.Http.HttpClient() + await client .GetStringAsync("https://www.sourcetreeapp.com/download-archives") .ConfigureAwait(false)) .QuerySelectorAll(".wpl tr div>a") - .Select(row => new { Version = row.TextContent, ZipUrl = row.GetAttribute("href") }) + .Select(row => new { Version = row.TextContent.Trim(), ZipUrl = row.GetAttribute("href") }) .ToArray()) + ")" ); diff --git a/json-backups/sourcetreeapp.csx b/json-backups/sourcetreeapp.csx index 80078f6..811c59d 100644 --- a/json-backups/sourcetreeapp.csx +++ b/json-backups/sourcetreeapp.csx @@ -3,15 +3,22 @@ using System; using System.IO; using System.Linq; +using System.Net.Http; using System.Text.Json; +using System.Threading.Tasks; -var source = await new System.Net.Http.HttpClient() +const string userAgent = "YourCustomUserAgent/1.0"; + +var client = new HttpClient(); +client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent); + +var source = await client .GetStringAsync("https://www.sourcetreeapp.com") .ConfigureAwait(false); var (startText, endText) = ("{ \"type\":\"imkt.components.SourcetreeDownload\"", "(source, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); @@ -26,11 +33,11 @@ Console.Out.WriteLine( .Select(a => new { ZipUrl = a.ToString(), - Version = new[] { "sourcetree", "enterprise", "setup", } + Version = new[] { "sourcetree", "enterprise", "setup" } .Aggregate(Path.GetFileNameWithoutExtension(a.AbsolutePath), (s, ptr) => s.Replace(ptr, "", StringComparison.OrdinalIgnoreCase)) .TrimStart('-', '_') }) - .ToArray())+ ")"); + .ToArray()) + ")"); public record Wrap(Params Params); public record Params(string MacUrl, string WindowsUrl, string EnterpriseUrl);