fix json generator for sourcetree

This commit is contained in:
STAM 2024-12-02 21:29:34 +03:00
parent 5478bd3cbe
commit cf68a3a9c8
No known key found for this signature in database
GPG Key ID: 711526C6938897F1
2 changed files with 22 additions and 8 deletions

View File

@ -1,15 +1,22 @@
#r "nuget: AngleSharp, 1.0.0-alpha-844" #r "nuget: AngleSharp, 1.0.0-alpha-844"
using System.Linq; 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( System.Console.Out.WriteLine(
"downloads(" + System.Text.Json.JsonSerializer.Serialize( "downloads(" + System.Text.Json.JsonSerializer.Serialize(
new AngleSharp.Html.Parser.HtmlParser() new HtmlParser()
.ParseDocument( .ParseDocument(
await new System.Net.Http.HttpClient() await client
.GetStringAsync("https://www.sourcetreeapp.com/download-archives") .GetStringAsync("https://www.sourcetreeapp.com/download-archives")
.ConfigureAwait(false)) .ConfigureAwait(false))
.QuerySelectorAll(".wpl tr div>a") .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()) + ")" .ToArray()) + ")"
); );

View File

@ -3,15 +3,22 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Text.Json; 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") .GetStringAsync("https://www.sourcetreeapp.com")
.ConfigureAwait(false); .ConfigureAwait(false);
var (startText, endText) = ("{ \"type\":\"imkt.components.SourcetreeDownload\"", "</scri"); var (startText, endText) = ("{ \"type\":\"imkt.components.SourcetreeDownload\"", "</scri");
var startIndex = source.IndexOf(startText, System.StringComparison.Ordinal); var startIndex = source.IndexOf(startText, StringComparison.Ordinal);
var endIndex = source.IndexOf(endText, startIndex, System.StringComparison.Ordinal); var endIndex = source.IndexOf(endText, startIndex, StringComparison.Ordinal);
source = source[startIndex..endIndex]; source = source[startIndex..endIndex];
var json = JsonSerializer.Deserialize<Wrap>(source, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); var json = JsonSerializer.Deserialize<Wrap>(source, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
@ -26,11 +33,11 @@ Console.Out.WriteLine(
.Select(a => new .Select(a => new
{ {
ZipUrl = a.ToString(), 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)) .Aggregate(Path.GetFileNameWithoutExtension(a.AbsolutePath), (s, ptr) => s.Replace(ptr, "", StringComparison.OrdinalIgnoreCase))
.TrimStart('-', '_') .TrimStart('-', '_')
}) })
.ToArray())+ ")"); .ToArray()) + ")");
public record Wrap(Params Params); public record Wrap(Params Params);
public record Params(string MacUrl, string WindowsUrl, string EnterpriseUrl); public record Params(string MacUrl, string WindowsUrl, string EnterpriseUrl);