using System.Text.Json; using System.Text.RegularExpressions; if (args.Length != 1) { Console.WriteLine("Usage: Program "); return; } string discourseTopic = args[0]; int pageNumber = 0; HttpClient client = new(); client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; Chrome)"); Console.WriteLine(); while (true) { pageNumber++; string currentPage = $"{discourseTopic}.json?page={pageNumber}"; try { string response = await client.GetStringAsync(currentPage); using JsonDocument document = JsonDocument.Parse(response); JsonElement allPosts = document.RootElement.GetProperty("post_stream").GetProperty("posts"); foreach (var post in allPosts.EnumerateArray()) { if (!post.TryGetProperty("link_counts", out var allLinks) || allLinks.ValueKind != JsonValueKind.Array) continue; foreach (var link in allLinks.EnumerateArray()) { ProcessLink(link, post); } } } catch (HttpRequestException) { break; } } return; static void ProcessLink(JsonElement link, JsonElement post) { if (!link.TryGetProperty("internal", out var internalLink) || internalLink.ValueKind != JsonValueKind.False) return; int postNumber = post.GetProperty("post_number").GetInt32(); string? url = link.GetProperty("url").GetString(); if (url == null) throw new NullReferenceException($"link: {link} url property from the parsed json post: {post} is null!"); string? username = post.GetProperty("username").GetString(); if (username == null) throw new NullReferenceException($"link: {link} username property from the parsed json post: {post} is null!"); string? postBody = post.GetProperty("cooked").GetString(); if (postBody == null) throw new NullReferenceException($"link: {link} cooked property from the parsed json post: {post} is null!"); string escapedLink = Regex.Escape(url); string pattern = $"""]*href=\"{escapedLink}\"[^>]*>(.*?)"""; Console.WriteLine($"External link found in post number: {postNumber} by {username}"); Console.WriteLine(url); Match match = Regex.Match(postBody, pattern); if (match.Success) { string linkText = match.Groups[1].Value; Console.WriteLine($"Link Text: {linkText}\n"); } else { Console.WriteLine("No link text found\n"); } }