// I hereby waive this project under the public domain - see UNLICENSE for details. namespace StaggerPost; internal static class Export { /// /// Retrieves configuration settings from a TOML file if it exists; otherwise, returns a default configuration. /// /// The name of the configuration file (defaults to "config.toml"). /// A Config object populated with values from the file, or a default Config instance if the file is not found. static Config GetConfig(string file) { var cfgPath = Path.Combine(Tracer.AppDirectory, file); if (!File.Exists(cfgPath)) { Tracer.LogLine("Config file not found. Switching to defaults."); var defaultList = new[] { "games@lemmy.world", "politics@lemmy.world", "science@lemmy.world", "technology@lemmy.world", }; var config = new Config() { File = "schedule.json", Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Communities = defaultList.ToList(), }; return config; } Tracer.LogLine($"Discovered config file: {cfgPath}"); var toml = File.ReadAllText(cfgPath); var model = Toml.ToModel(toml); return model; } /// /// Exports the scheduled articles to a file, allowing the user to modify /// the directory, filename, and list of topics based on /// a configuration file if available. /// public static void ToJSON(List storeTimes, string cfgPath) { // File directory is used for file location set in config var outputDir = Directory.GetCurrentDirectory(); var topics = new List(); var config = GetConfig(cfgPath); var outputFile = config.File; var filePath = Path.Combine(outputDir, outputFile!); var chosenTopic = ""; var times = new List(); // If the config file exists, read from that but don't assume anything is filled if (File.Exists(cfgPath)) { var toml = File.ReadAllText(cfgPath); var usrDir = config.Path; var usrFileName = config.File; // Convert list into array var list = config.Communities; var tomlList = string.Join(", ", list); var usrTopics = tomlList.Split(','); if (string.IsNullOrEmpty(usrDir)) return; outputDir = usrDir; if (string.IsNullOrEmpty(usrFileName)) return; outputFile = usrFileName; // If array is empty, return; otherwise, apply config if (usrTopics.Length < 0) return; foreach (var usrTopic in usrTopics) topics.Add(usrTopic); // Set new file Path filePath = Path.Combine(outputDir, outputFile!); } if (!File.Exists(filePath)) File.WriteAllText(filePath, "[]"); foreach (var time in storeTimes) times.Add(time.Trim()); // Set new topic topics = config.Communities.ToList(); Console.Clear(); chosenTopic = Interactive.SelectTopics(topics); var date = Interactive.SelectDate(); // Write to file. var jsonFile = File.ReadAllText(filePath); var jsonList = string.IsNullOrWhiteSpace(jsonFile) ? new List() : JsonSerializer.Deserialize>(jsonFile) ?? new List(); jsonList.Add( new Schedule() { Community = chosenTopic.Trim(), Date = date.Trim(), Times = times, } ); var jsonOptions = new JsonSerializerOptions() { WriteIndented = true }; var json = JsonSerializer.Serialize(jsonList, jsonOptions); File.WriteAllText(filePath, json); Tracer.LogLine($"{json}{Environment.NewLine}Written to: {filePath}"); // Clear list from memory storeTimes.Clear(); } }