← back to Dear Bubbe Nextjs
archived/social-posters/instagram-business-continuous.js
103 lines
#!/usr/bin/env node
const { postToInstagramBusiness } = require('./instagram-business-poster');
const fs = require('fs').promises;
const path = require('path');
// Configuration
const CONFIG = {
POSTS_PER_DAY: 3,
INTERVAL_HOURS: 6, // Post every 6 hours
STATE_FILE: '/root/Projects/dear-bubbe-admin/logs/instagram-business-state.json'
};
// Load state
async function loadState() {
try {
const data = await fs.readFile(CONFIG.STATE_FILE, 'utf8');
return JSON.parse(data);
} catch {
return {
dailyPosts: 0,
lastResetDate: new Date().toDateString(),
lastPostTime: null,
totalPosts: 0
};
}
}
// Save state
async function saveState(state) {
await fs.writeFile(CONFIG.STATE_FILE, JSON.stringify(state, null, 2));
}
// Continuous posting
async function continuousPost() {
console.log('🚀 Starting Instagram Business Continuous Poster');
console.log(`📸 ${CONFIG.POSTS_PER_DAY} posts per day`);
console.log(`⏱️ Every ${CONFIG.INTERVAL_HOURS} hours`);
console.log('=' .repeat(50));
while (true) {
const state = await loadState();
// Reset daily counter
const today = new Date().toDateString();
if (today !== state.lastResetDate) {
console.log('📅 New day - reset post counter');
state.dailyPosts = 0;
state.lastResetDate = today;
}
// Check limits
if (state.dailyPosts >= CONFIG.POSTS_PER_DAY) {
console.log(`📊 Daily limit reached (${state.dailyPosts}/${CONFIG.POSTS_PER_DAY})`);
const hoursUntilMidnight = 24 - new Date().getHours();
console.log(`⏱️ Waiting ${hoursUntilMidnight} hours until midnight`);
await new Promise(resolve => setTimeout(resolve, hoursUntilMidnight * 60 * 60 * 1000));
continue;
}
// Check interval
if (state.lastPostTime) {
const hoursSince = (Date.now() - new Date(state.lastPostTime).getTime()) / (1000 * 60 * 60);
if (hoursSince < CONFIG.INTERVAL_HOURS) {
const waitHours = CONFIG.INTERVAL_HOURS - hoursSince;
console.log(`⏱️ Wait ${waitHours.toFixed(1)} more hours`);
await new Promise(resolve => setTimeout(resolve, waitHours * 60 * 60 * 1000));
continue;
}
}
console.log(`\n🔄 Attempting post ${state.dailyPosts + 1}/${CONFIG.POSTS_PER_DAY} for today`);
try {
// Post to Instagram Business
const result = await postToInstagramBusiness();
if (result.success) {
// Update state
state.lastPostTime = new Date().toISOString();
state.dailyPosts++;
state.totalPosts++;
await saveState(state);
console.log(`✅ Posted ${state.dailyPosts}/${CONFIG.POSTS_PER_DAY} today`);
console.log(`📈 Total posts: ${state.totalPosts}`);
}
} catch (error) {
console.error('❌ Error posting:', error.message);
}
// Wait before next check (30 minutes)
console.log(`⏱️ Checking again in 30 minutes...`);
await new Promise(resolve => setTimeout(resolve, 30 * 60 * 1000));
}
}
// Run
if (require.main === module) {
continuousPost().catch(console.error);
}
module.exports = { continuousPost };