← back to Dear Bubbe Nextjs
lib/instagram-prepare.js
207 lines
#!/usr/bin/env node
const fs = require('fs').promises;
const path = require('path');
const { exec } = require('child_process');
const util = require('util');
const execPromise = util.promisify(exec);
const { generateInstagramImage, generateCaption } = require('./instagram-image-generator');
// Get user messages
async function getUserMessages() {
try {
const { stdout } = await execPromise(
`pm2 logs bubbe-ai --lines 1000 --nostream | grep -E "message:" | grep -v "__init__" | grep -v "__returning" | head -50`
);
const messages = [];
const lines = stdout.split('\n').filter(line => line.trim());
for (const line of lines) {
const match = line.match(/message:\s*["'](.+?)["']/);
if (match && match[1]) {
const msg = match[1].trim();
if (msg.length > 20 && !msg.includes('__') && !msg.startsWith('test') && !msg.includes('favicon')) {
messages.push(msg);
}
}
}
return [...new Set(messages)];
} catch (error) {
console.error('Error getting messages:', error);
return [];
}
}
// Prepare Instagram post
async function prepareInstagramPost() {
console.log('📸 Preparing Instagram Post');
console.log('=' .repeat(40));
// Get messages
const messages = await getUserMessages();
if (messages.length === 0) {
console.log('❌ No messages found');
return;
}
// Pick random message
const selected = messages[Math.floor(Math.random() * messages.length)];
console.log(`\n📝 Selected: "${selected}"`);
// Generate image
const timestamp = Date.now();
const imagePath = `/root/Projects/dear-bubbe-admin/public/instagram-${timestamp}.png`;
await generateInstagramImage(selected, imagePath);
// Generate caption
const caption = generateCaption(selected);
// Save caption to file for easy copying
const captionPath = `/root/Projects/dear-bubbe-admin/public/instagram-caption-${timestamp}.txt`;
await fs.writeFile(captionPath, caption);
// Create HTML preview page
const htmlContent = `<!DOCTYPE html>
<html>
<head>
<title>Instagram Post Ready</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
background: #fafafa;
}
.container {
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}
h1 {
color: #262626;
font-size: 24px;
margin-bottom: 20px;
}
.image-container {
width: 100%;
margin-bottom: 20px;
}
img {
width: 100%;
border-radius: 8px;
}
.caption-box {
background: #f7f7f7;
padding: 15px;
border-radius: 8px;
white-space: pre-wrap;
font-size: 14px;
line-height: 1.5;
margin-bottom: 20px;
}
.download-btn {
display: inline-block;
background: #0095f6;
color: white;
padding: 12px 24px;
border-radius: 8px;
text-decoration: none;
font-weight: 600;
margin-right: 10px;
}
.instructions {
background: #fff3cd;
padding: 15px;
border-radius: 8px;
margin-top: 20px;
}
.instructions h2 {
margin-top: 0;
color: #856404;
}
.instructions ol {
margin: 10px 0;
padding-left: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>📸 Instagram Post Ready!</h1>
<div class="image-container">
<img src="/instagram-${timestamp}.png" alt="Instagram Post">
</div>
<h2>Caption:</h2>
<div class="caption-box">${caption.replace(/</g, '<').replace(/>/g, '>')}</div>
<a href="/instagram-${timestamp}.png" download="instagram-post.png" class="download-btn">⬇️ Download Image</a>
<a href="/instagram-caption-${timestamp}.txt" download="instagram-caption.txt" class="download-btn">📋 Download Caption</a>
<div class="instructions">
<h2>📱 How to Post:</h2>
<ol>
<li>Download the image above</li>
<li>Open Instagram app or <a href="https://instagram.com" target="_blank">instagram.com</a></li>
<li>Click "New Post" (+)</li>
<li>Upload the downloaded image</li>
<li>Copy and paste the caption</li>
<li>Share!</li>
</ol>
<h2>🔗 Quick Links:</h2>
<ul>
<li><a href="https://instagram.com" target="_blank">Instagram Web</a></li>
<li><a href="https://business.facebook.com/creatorstudio" target="_blank">Creator Studio (Schedule Posts)</a></li>
<li><a href="https://later.com" target="_blank">Later (Free Scheduling)</a></li>
</ul>
</div>
<p style="text-align: center; margin-top: 30px; color: #8e8e8e;">
Generated at ${new Date().toLocaleString()}
</p>
</div>
</body>
</html>`;
const htmlPath = `/root/Projects/dear-bubbe-admin/public/instagram-post.html`;
await fs.writeFile(htmlPath, htmlContent);
console.log('\n✅ Instagram post prepared!');
console.log('\n📸 Image:');
console.log(` http://45.61.58.125:7902/instagram-${timestamp}.png`);
console.log('\n📝 Caption:');
console.log(` http://45.61.58.125:7902/instagram-caption-${timestamp}.txt`);
console.log('\n🌐 Preview Page:');
console.log(` http://45.61.58.125:7902/instagram-post.html`);
console.log('\n📱 Direct Instagram Link:');
console.log(' https://instagram.com/dearBubbe');
// Also output caption for manual copying
console.log('\n📋 CAPTION TO COPY:');
console.log('=' .repeat(40));
console.log(caption);
console.log('=' .repeat(40));
return {
imagePath,
captionPath,
imageUrl: `http://45.61.58.125:7902/instagram-${timestamp}.png`,
captionUrl: `http://45.61.58.125:7902/instagram-caption-${timestamp}.txt`,
previewUrl: 'http://45.61.58.125:7902/instagram-post.html'
};
}
// Run if called directly
if (require.main === module) {
prepareInstagramPost().catch(console.error);
}
module.exports = { prepareInstagramPost };