mirror of
https://github.com/tonytins/PublishTimes.git
synced 2025-03-15 04:01:20 +00:00
Compare commits
2 commits
aebe0c7040
...
2f41109477
Author | SHA1 | Date | |
---|---|---|---|
2f41109477 | |||
77e9db562a |
2 changed files with 48 additions and 39 deletions
83
Program.cs
83
Program.cs
|
@ -1,37 +1,37 @@
|
||||||
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
||||||
const string banner = "=== Publish Times ===";
|
|
||||||
|
|
||||||
var numberOfArticles = 5; // Define how many articles to schedule
|
/// <summary>
|
||||||
var startTime = new TimeSpan(9, 0, 0); // Starting time at 9:00 AM
|
/// Generates a schedule of article publishing times, ensuring a randomized
|
||||||
var rng = new Random();
|
/// delay between each while avoiding time conflicts within a 30-minute window.
|
||||||
var scheduledTimes = new List<TimeSpan>();
|
/// </summary>
|
||||||
var storeSchedule = new List<String>();
|
/// <returns>A list of TimeSpan objects representing scheduled article times.</returns>
|
||||||
// App directory is used for config file
|
List<TimeSpan> GenerateSchedule()
|
||||||
var appDir = Directory.GetCurrentDirectory();
|
|
||||||
// File directory is used for file location set in config
|
|
||||||
var fileDir = Directory.GetCurrentDirectory();
|
|
||||||
var isRestart = false;
|
|
||||||
var communities = new[] { "Games", "Politics", "Research", "Technology" };
|
|
||||||
var scheduleFile = "schedule.txt";
|
|
||||||
var cfgFile = "config.toml";
|
|
||||||
|
|
||||||
for (int i = 0; i < numberOfArticles; i++)
|
|
||||||
{
|
{
|
||||||
var baseDelayHours = rng.Next(2, 4); // Randomly choose between 2-3 hours delay
|
var numberOfArticles = 5; // Define how many articles to schedule
|
||||||
var minutesToAdd = rng.Next(0, 60); // Randomly choose minutes (0-59)
|
var startTime = new TimeSpan(9, 0, 0); // Starting time at 9:00 AM
|
||||||
|
var rng = new Random();
|
||||||
|
var scheduledTimes = new List<TimeSpan>();
|
||||||
|
|
||||||
// Calculate new time by adding base delay and random minutes
|
for (int i = 0; i < numberOfArticles; i++)
|
||||||
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
|
var baseDelayHours = rng.Next(2, 4); // Randomly choose between 2-3 hours delay
|
||||||
nextTime = nextTime.Add(new TimeSpan(0, 30, 0));
|
var minutesToAdd = rng.Next(0, 60); // Randomly choose minutes (0-59)
|
||||||
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduledTimes.Add(nextTime);
|
return scheduledTimes;
|
||||||
startTime = nextTime; // Update start time for the next article
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -39,7 +39,7 @@ for (int i = 0; i < numberOfArticles; i++)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The TimeSpan representing the time of day.</param>
|
/// <param name="time">The TimeSpan representing the time of day.</param>
|
||||||
/// <returns>A formatted string representing the time in AM/PM format.</returns>
|
/// <returns>A formatted string representing the time in AM/PM format.</returns>
|
||||||
string TimeSpanToAMPM(TimeSpan time)
|
string ConvertTo12Hour(TimeSpan time)
|
||||||
{
|
{
|
||||||
var minutes = time.TotalMinutes;
|
var minutes = time.TotalMinutes;
|
||||||
var hours12 = time.Hours % 12;
|
var hours12 = time.Hours % 12;
|
||||||
|
@ -56,13 +56,22 @@ string TimeSpanToAMPM(TimeSpan time)
|
||||||
/// the directory, filename, and list of topics based on
|
/// the directory, filename, and list of topics based on
|
||||||
/// a configuration file if available.
|
/// a configuration file if available.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void ExportSchedule()
|
void ExportSchedule(List<String> storeSchedule)
|
||||||
{
|
{
|
||||||
|
// 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();
|
||||||
|
var communities = new[] { "Games", "Politics", "Research", "Technology" };
|
||||||
|
var scheduleFile = "schedule.txt";
|
||||||
|
var cfgFile = "config.toml";
|
||||||
|
|
||||||
var cfgPath = Path.Combine(appDir, cfgFile);
|
var cfgPath = Path.Combine(appDir, cfgFile);
|
||||||
var filePath = Path.Combine(fileDir, scheduleFile);
|
var filePath = Path.Combine(fileDir, scheduleFile);
|
||||||
var appendSchedule = false;
|
var appendSchedule = false;
|
||||||
var topic = "";
|
var topic = "";
|
||||||
|
|
||||||
|
var rng = new Random();
|
||||||
var chooseTopic = rng.Next(0, communities.Length);
|
var chooseTopic = rng.Next(0, communities.Length);
|
||||||
topic = communities[chooseTopic];
|
topic = communities[chooseTopic];
|
||||||
|
|
||||||
|
@ -118,15 +127,18 @@ void ExportSchedule()
|
||||||
/// Displays the scheduled article times in a formatted manner and provides
|
/// Displays the scheduled article times in a formatted manner and provides
|
||||||
/// options to export the schedule or restart the scheduling process.
|
/// options to export the schedule or restart the scheduling process.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void PrintSchedule()
|
void PrintSchedule(bool isRestart = false)
|
||||||
{
|
{
|
||||||
|
var storeSchedule = new List<String>();
|
||||||
|
var scheduledTimes = GenerateSchedule();
|
||||||
|
|
||||||
if (isRestart)
|
if (isRestart)
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|
||||||
Console.WriteLine(banner);
|
Console.WriteLine("=== Publish Times ===");
|
||||||
foreach (var time in scheduledTimes)
|
foreach (var time in scheduledTimes)
|
||||||
{
|
{
|
||||||
var articleTime = $"Article {scheduledTimes.IndexOf(time) + 1} Scheduled at: {TimeSpanToAMPM(time)}";
|
var articleTime = $"Article {scheduledTimes.IndexOf(time) + 1} Scheduled at: {ConvertTo12Hour(time)}";
|
||||||
// Correct format string to display time in 12-hour format with AM/PM
|
// Correct format string to display time in 12-hour format with AM/PM
|
||||||
Console.WriteLine(articleTime);
|
Console.WriteLine(articleTime);
|
||||||
// Store the schedule to memory for option export
|
// Store the schedule to memory for option export
|
||||||
|
@ -136,14 +148,11 @@ void PrintSchedule()
|
||||||
// Give the user an option to export the schedule
|
// Give the user an option to export the schedule
|
||||||
Console.WriteLine($"{Environment.NewLine}Export? Y/N");
|
Console.WriteLine($"{Environment.NewLine}Export? Y/N");
|
||||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
if (Console.ReadKey().Key == ConsoleKey.Y)
|
||||||
ExportSchedule();
|
ExportSchedule(storeSchedule);
|
||||||
|
|
||||||
Console.WriteLine($"{Environment.NewLine}Start Over? Y/N");
|
Console.WriteLine($"{Environment.NewLine}Start Over? Y/N");
|
||||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
if (Console.ReadKey().Key == ConsoleKey.Y)
|
||||||
{
|
PrintSchedule(true);
|
||||||
isRestart = true;
|
|
||||||
PrintSchedule();
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
# Publish Times
|
# Publish Times
|
||||||
|
|
||||||
This is a very simple console application that generates a list of times to schedule news with a 2-3 hour delay and a 30 minute time gap per-article.
|
This is a very simple console application that generates a list of times to publish news articles within a randomized 2-3 hour delay while avoiding time conflicts within a 30-minute window.
|
||||||
|
|
||||||
## Background
|
## Background
|
||||||
|
|
||||||
A while back, I [found a tool](https://schedule.lemmings.world) to schedule articles on Lemmy. I've been posting within a few hour apart at random minutes and I wanted to something decide that for me. I had AI write the base algorithm everything else is my own touches.
|
A while back, I [found a tool](https://schedule.lemmings.world) to schedule articles on Lemmy. I've been posting within a few hours apart at random minutes and I wanted to something decide that for me. I had AI write the base algorithm, everything else is my own touches.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue