Add automated content planning and image overlays
This commit is contained in:
@@ -1,13 +1,40 @@
|
||||
type Schedule = {
|
||||
weekdays: number[];
|
||||
times: string[];
|
||||
mode?: "FIXED" | "RANDOM";
|
||||
randomPostsPerDay?: number;
|
||||
seed?: string;
|
||||
};
|
||||
|
||||
function seededMinutes(seed: string, count: number): number[] {
|
||||
let state = 2166136261;
|
||||
for (const char of seed) state = Math.imul(state ^ char.charCodeAt(0), 16777619) >>> 0;
|
||||
const slots = new Set<number>();
|
||||
while (slots.size < count) {
|
||||
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
||||
// 08:00–20:00, snapped to 5-minute increments for readable schedules.
|
||||
slots.add(8 * 60 + (state % 145) * 5);
|
||||
}
|
||||
return [...slots].sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
/** Weekly slots in Asia/Bangkok (UTC+7, no daylight saving), strictly after now. */
|
||||
export function nextContentRun(weekdays: number[], times: string[], now = new Date()): Date {
|
||||
export function nextContentRun(schedule: Schedule, now = new Date()): Date {
|
||||
const { weekdays, times, mode = "FIXED", randomPostsPerDay = 1, seed = "content-auto" } = schedule;
|
||||
const offset = 7 * 60 * 60_000;
|
||||
const local = new Date(now.getTime() + offset);
|
||||
for (let day = 0; day <= 7; day++) {
|
||||
const midnight = Date.UTC(local.getUTCFullYear(), local.getUTCMonth(), local.getUTCDate() + day);
|
||||
if (!weekdays.includes(new Date(midnight).getUTCDay())) continue;
|
||||
for (const time of [...times].sort()) {
|
||||
const [hours, minutes] = time.split(":").map(Number);
|
||||
const candidate = new Date(midnight + (hours * 60 + minutes) * 60_000 - offset);
|
||||
const dateKey = new Date(midnight).toISOString().slice(0, 10);
|
||||
const minutesOfDay = mode === "RANDOM"
|
||||
? seededMinutes(`${seed}:${dateKey}`, Math.min(3, Math.max(1, randomPostsPerDay)))
|
||||
: [...times].sort().map((time) => {
|
||||
const [hours, minutes] = time.split(":").map(Number);
|
||||
return hours * 60 + minutes;
|
||||
});
|
||||
for (const minutes of minutesOfDay) {
|
||||
const candidate = new Date(midnight + minutes * 60_000 - offset);
|
||||
if (candidate > now) return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user