Skip to content

Commit

Permalink
fix json generator for sourcetree
Browse files Browse the repository at this point in the history
  • Loading branch information
stamepicmorg committed Dec 2, 2024
1 parent 5478bd3 commit cf68a3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 10 additions & 3 deletions json-backups/sourcetreeapp-archive.csx
Original file line number Diff line number Diff line change
@@ -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()) + ")"
);
17 changes: 12 additions & 5 deletions json-backups/sourcetreeapp.csx
Original file line number Diff line number Diff line change
Expand Up @@ -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\"", "</scri");
var startIndex = source.IndexOf(startText, System.StringComparison.Ordinal);
var endIndex = source.IndexOf(endText, startIndex, System.StringComparison.Ordinal);
var startIndex = source.IndexOf(startText, StringComparison.Ordinal);
var endIndex = source.IndexOf(endText, startIndex, StringComparison.Ordinal);
source = source[startIndex..endIndex];

var json = JsonSerializer.Deserialize<Wrap>(source, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
Expand All @@ -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);

0 comments on commit cf68a3a

Please sign in to comment.