using AiAgent.Core.Infrastructure; using System.Text.Json; using static AiAgent.Core.Constant.ProgramConstants; namespace AiAgent.Core.Helpers { public static class LandingPageEditor { public static async Task RunAsync(string newArticleFileName, string newImageFileName) { // Download it from the github repo string page = "index.html"; string filename = await GitHubHelper.DownloadFile(page, Output); // Backup the existing file string backupName = filename + $" - Backup - {DateTime.Now:yyyy-MMM-dd HH-mm-ss}.html"; File.Copy(filename, backupName); string todayDate = File.ReadAllText(TodayDate); string articleJson = File.ReadAllText(FullArticle); JsonDocument doc = JsonDocument.Parse(articleJson); if (doc.RootElement.TryGetProperty("Title", out JsonElement titleProperty) is false) throw new Exception("The 'Title' property was not found in the article JSON."); string fullTitle = titleProperty.GetString(); // Split title into primary and secondary parts string shortTitle = fullTitle; string secondaryTitle = ""; var titleParts = fullTitle.Split(':', 2); // Split on the first colon only if (titleParts.Length > 1) { shortTitle = titleParts[0].Trim(); secondaryTitle = titleParts[1].Trim(); } // Use the new title string articleMetadata = File.ReadAllText(ArticleMetadata); var articleMetadataSections = JsonHelpers.SectionParser(articleMetadata); shortTitle = $"{articleMetadataSections["Category"]}: {articleMetadataSections["Title"]}"; secondaryTitle = articleMetadataSections["Description"]; // Conditionally create the HTML for the secondary title string secondaryTitleHtml = !string.IsNullOrWhiteSpace(secondaryTitle) ? $"

{secondaryTitle}

" : ""; string newArticleLink = $"""
{articleMetadataSections[

{shortTitle}

{articleMetadataSections["Category"]}: {articleMetadataSections["Description"]}

"""; //string newArticleLink = $""" //
// // {shortTitle} // //
// //

// // {shortTitle} // //

// {secondaryTitleHtml} //
//
//"""; // string newArticleLink = $"""         //           
        //               
        //                            //                        {shortTitle}         //                            //                   
        //                                //                       

        //                                    //                                {shortTitle}         //                                    //                       

        //                   
        //               
        //                {secondaryTitleHtml}         //           
        //"""; // Insert the new article link after the delimiter string delimiter = ""; string pageFile = Path.Combine(Output, page); string pageContent = File.ReadAllText(pageFile); var pageParts = pageContent.Split(delimiter); string newContent = pageParts[0] + delimiter + newArticleLink + pageParts[1]; await File.WriteAllTextAsync(pageFile, newContent); var result = await GitHubHelper.UploadFile(page, pageFile, $"feat: edit landing page with article '{fullTitle}'"); var urlFile = "Sitemap.txt"; var urls = HtmlUrlExtractor.ExtractUrls(pageContent) .Where(u => u.StartsWith("http") is false) .Where(u => u.EndsWith(".png") is false) .Where(u => u.Length > 5) .Select(u => "https://factcheckednews.com/" + u) .ToList(); var urlFilePath = Path.Combine(Output, urlFile); await File.WriteAllLinesAsync(urlFilePath, urls); result = await GitHubHelper.UploadFile(urlFile, urlFilePath, $"feat: Add Sitemap.txt"); return result; } } }