2025-03-11 16:09:39 -04:00
|
|
|
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
2025-03-11 16:01:46 -04:00
|
|
|
const string banner = "=== Publish Times ===";
|
|
|
|
|
|
|
|
var numberOfArticles = 5; // Define how many articles to schedule
|
|
|
|
var startTime = new TimeSpan(9, 0, 0); // Starting time at 9:00 AM
|
2025-03-12 01:55:06 -04:00
|
|
|
var rng = new Random();
|
2025-03-11 16:01:46 -04:00
|
|
|
var scheduledTimes = new List<TimeSpan>();
|
2025-03-11 16:51:25 -04:00
|
|
|
var storeSchedule = new List<String>();
|
2025-03-11 23:30:11 -04:00
|
|
|
// App directory is used for config file
|
|
|
|
var appDir = Directory.GetCurrentDirectory();
|
|
|
|
// File directory is used for file location set in config
|
|
|
|
var fileDir = Directory.GetCurrentDirectory();
|
2025-03-12 14:28:36 -04:00
|
|
|
var isRestart = false;
|
2025-03-12 01:55:06 -04:00
|
|
|
var communities = new[] { "Games", "Politics", "Research", "Technology" };
|
2025-03-11 23:30:11 -04:00
|
|
|
var scheduleFile = "schedule.txt";
|
|
|
|
var cfgFile = "config.toml";
|
2025-03-11 16:01:46 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < numberOfArticles; i++)
|
|
|
|
{
|
2025-03-12 01:55:06 -04:00
|
|
|
var baseDelayHours = rng.Next(2, 4); // Randomly choose between 2-3 hours delay
|
|
|
|
var minutesToAdd = rng.Next(0, 60); // Randomly choose minutes (0-59)
|
2025-03-11 16:01:46 -04:00
|
|
|
|
|
|
|
// Calculate new time by adding base delay and random minutes
|
|
|
|
var nextTime = startTime.Add(new TimeSpan(baseDelayHours, minutesToAdd, 0));
|
|
|
|
|
|
|
|
// Check if the new time is within 30 minutes of any existing time
|
|
|
|
while (scheduledTimes.Exists(previousTime => Math.Abs((nextTime - previousTime).TotalMinutes) < 30))
|
|
|
|
{
|
|
|
|
// If the new time is within 30 minutes of an existing time, adjust it
|
|
|
|
nextTime = nextTime.Add(new TimeSpan(0, 30, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduledTimes.Add(nextTime);
|
|
|
|
startTime = nextTime; // Update start time for the next article
|
|
|
|
}
|
|
|
|
|
|
|
|
string TimeSpanToAMPM(TimeSpan time)
|
|
|
|
{
|
|
|
|
var minutes = time.TotalMinutes;
|
|
|
|
var hours12 = time.Hours % 12;
|
|
|
|
|
|
|
|
if (hours12 == 0)
|
|
|
|
hours12 = 1;
|
|
|
|
|
|
|
|
var period = time.Hours >= 12 ? "PM" : "AM";
|
|
|
|
return $"{hours12}:{time.Minutes:D2} {period}";
|
|
|
|
}
|
|
|
|
|
2025-03-11 16:51:25 -04:00
|
|
|
|
2025-03-12 14:28:36 -04:00
|
|
|
void ExportSchedule()
|
2025-03-11 16:51:25 -04:00
|
|
|
{
|
2025-03-11 23:30:11 -04:00
|
|
|
var cfgPath = Path.Combine(appDir, cfgFile);
|
|
|
|
var filePath = Path.Combine(fileDir, scheduleFile);
|
2025-03-11 16:51:25 -04:00
|
|
|
var appendSchedule = false;
|
2025-03-12 01:55:06 -04:00
|
|
|
var topic = "";
|
|
|
|
|
|
|
|
var chooseTopic = rng.Next(0, communities.Length);
|
|
|
|
topic = communities[chooseTopic];
|
2025-03-11 16:51:25 -04:00
|
|
|
|
2025-03-11 23:30:11 -04:00
|
|
|
// If the config file exists, read from that but don't assume anything is filled
|
|
|
|
if (File.Exists(cfgPath))
|
|
|
|
{
|
|
|
|
var toml = File.ReadAllText(cfgPath);
|
2025-03-12 14:28:36 -04:00
|
|
|
var model = Toml.ToModel<Config>(toml);
|
|
|
|
var usrDir = model.Path;
|
|
|
|
var usrFileName = model.File;
|
|
|
|
var tomlList = string.Join(", ", model.Topics);
|
2025-03-12 02:11:58 -04:00
|
|
|
var usrList = tomlList.Split(',');
|
2025-03-11 23:30:11 -04:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(usrDir))
|
|
|
|
fileDir = usrDir;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(usrFileName))
|
|
|
|
scheduleFile = usrFileName;
|
2025-03-12 02:11:58 -04:00
|
|
|
|
|
|
|
if (usrList.Length > 0)
|
|
|
|
{
|
|
|
|
var chooseUsrTopic = rng.Next(0, usrList.Length);
|
|
|
|
topic = usrList[chooseUsrTopic];
|
|
|
|
}
|
|
|
|
|
2025-03-11 23:30:11 -04:00
|
|
|
|
|
|
|
// Set new file Path
|
|
|
|
filePath = Path.Combine(fileDir, scheduleFile);
|
|
|
|
}
|
|
|
|
|
2025-03-11 18:42:14 -04:00
|
|
|
// If the file already exists, assume a previous schedule was written
|
2025-03-11 16:51:25 -04:00
|
|
|
if (File.Exists(filePath))
|
|
|
|
{
|
|
|
|
Console.WriteLine($"{Environment.NewLine}Add another schedule? Y/N");
|
|
|
|
if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
|
|
appendSchedule = true;
|
2025-03-12 14:28:36 -04:00
|
|
|
|
|
|
|
// Write to file.
|
|
|
|
using (var outputFile = new StreamWriter(filePath, appendSchedule))
|
|
|
|
{
|
|
|
|
outputFile.WriteLine($" === {topic} ===");
|
|
|
|
foreach (var line in storeSchedule)
|
|
|
|
outputFile.WriteLine(line);
|
|
|
|
}
|
2025-03-11 16:51:25 -04:00
|
|
|
}
|
|
|
|
|
2025-03-12 14:28:36 -04:00
|
|
|
// Clear list from memory before exit
|
|
|
|
storeSchedule.Clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintSchedule()
|
|
|
|
{
|
|
|
|
if (isRestart)
|
|
|
|
Console.Clear();
|
|
|
|
|
|
|
|
Console.WriteLine(banner);
|
|
|
|
foreach (var time in scheduledTimes)
|
2025-03-11 16:51:25 -04:00
|
|
|
{
|
2025-03-12 14:28:36 -04:00
|
|
|
var articleTime = $"Article {scheduledTimes.IndexOf(time) + 1} Scheduled at: {TimeSpanToAMPM(time)}";
|
|
|
|
// Correct format string to display time in 12-hour format with AM/PM
|
|
|
|
Console.WriteLine(articleTime);
|
|
|
|
// Store the schedule to memory for option export
|
|
|
|
storeSchedule.Add(articleTime);
|
|
|
|
}
|
2025-03-11 16:51:25 -04:00
|
|
|
|
2025-03-12 14:28:36 -04:00
|
|
|
// Give the user an option to export the schedule
|
|
|
|
Console.WriteLine($"{Environment.NewLine}Export? Y/N");
|
|
|
|
if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
|
|
ExportSchedule();
|
|
|
|
|
|
|
|
Console.WriteLine($"{Environment.NewLine}Start Over? Y/N");
|
|
|
|
if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
|
|
{
|
|
|
|
isRestart = true;
|
|
|
|
PrintSchedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.Clear();
|
|
|
|
Environment.Exit(Environment.ExitCode);
|
2025-03-11 16:51:25 -04:00
|
|
|
}
|
2025-03-11 16:01:46 -04:00
|
|
|
}
|
2025-03-11 23:30:11 -04:00
|
|
|
|
2025-03-12 14:28:36 -04:00
|
|
|
PrintSchedule();
|