using System.Text.Json; namespace URLNotesGrabberCORE { // Port of ThreeTxtFileHelper/UpdateBlogPaths.cs. Reads .tumblr / .tmblrpriv metadata // files from a root\Index folder and populates Blogs.TTFolderPath in TL.db. public static class UpdateBlogPathsRunner { public static int Run(string rootPath) { if (string.IsNullOrWhiteSpace(rootPath)) { Console.WriteLine("UpdateBlogPaths: rootPath is required."); return 1; } DataAccess.EnsureTTFileHelperColumnsExist(); string indexPath = Path.Combine(rootPath, "Index"); if (!Directory.Exists(indexPath)) { Console.WriteLine($"Index folder not found at: {indexPath}"); return 1; } Console.WriteLine($"Scanning Index folder: {indexPath}"); var blogFiles = Directory.GetFiles(indexPath, "*.tumblr") .Concat(Directory.GetFiles(indexPath, "*.tmblrpriv")) .ToList(); Console.WriteLine($"Found {blogFiles.Count} blog metadata files"); int updatedCount = 0; int unchangedCount = 0; int noLocationCount = 0; int noRowCount = 0; int errorCount = 0; foreach (var blogFile in blogFiles) { try { string blogName = Path.GetFileNameWithoutExtension(blogFile); string jsonContent = File.ReadAllText(blogFile); using JsonDocument doc = JsonDocument.Parse(jsonContent); JsonElement root = doc.RootElement; if (root.TryGetProperty("FileDownloadLocation", out JsonElement locationElement)) { string? fileDownloadLocation = locationElement.GetString()?.Trim(); if (!string.IsNullOrWhiteSpace(fileDownloadLocation)) { // Report the database's answer, not the fact that the file parsed. if (DataAccess.SetBlogTTFolderPath(blogName, fileDownloadLocation)) { updatedCount++; Console.WriteLine($"Updated {blogName}: {fileDownloadLocation}"); } else if (DataAccess.BlogExists(blogName)) { unchangedCount++; } else { noRowCount++; Console.WriteLine($"No Blogs row named '{blogName}' -- path not stored (name may differ in case)"); } } else { noLocationCount++; Console.WriteLine($"Empty FileDownloadLocation in {blogFile}"); } } else { noLocationCount++; Console.WriteLine($"No FileDownloadLocation found in {blogFile}"); } } catch (Exception ex) { errorCount++; Console.WriteLine($"Error processing {blogFile}: {ex.Message}"); } } Console.WriteLine($"\n========== UpdateBlogPaths summary =========="); Console.WriteLine($"Metadata files: {blogFiles.Count}"); Console.WriteLine($"TTFolderPath written: {updatedCount}"); Console.WriteLine($"Already correct: {unchangedCount}"); Console.WriteLine($"No FileDownloadLocation: {noLocationCount}"); Console.WriteLine($"No matching blog row: {noRowCount}"); Console.WriteLine($"Errors: {errorCount}"); int stored = DataAccess.CountBlogsWithTTFolderPath(); Console.WriteLine($"\nBlogs now holding a TTFolderPath: {stored}"); return errorCount == 0 ? 0 : 2; } } }