← back to Watches

utils/notify-steve-on-crawl.js

87 lines

/**
 * Notify Steve (CEO) when slow crawler completes successfully
 * Sends message to CEO agent with museum page link
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const STEVE_WEBHOOK = 'http://localhost:9800/api/webhook/message'; // CEO agent port
const MUSEUM_URL = 'http://45.61.58.125:7600/museum/';
const PROGRESS_FILE = path.join(__dirname, '../museum/crawl-data/progress.json');

async function notifySteve() {
  try {
    // Check if we have crawl results
    if (!fs.existsSync(PROGRESS_FILE)) {
      console.log('❌ No progress file found yet');
      return;
    }

    const progress = JSON.parse(fs.readFileSync(PROGRESS_FILE, 'utf8'));
    const totalResults = progress.results?.length || 0;

    if (totalResults === 0) {
      console.log('⚠️ No results yet, skipping notification');
      return;
    }

    // Calculate total value
    const totalValue = progress.results.reduce((sum, r) => sum + (r.price || 0), 0);

    // Build message
    const message = {
      from: 'Omega Watch Museum Crawler',
      type: 'success',
      priority: 'normal',
      subject: `🏛️ Museum Update: ${totalResults} Omega Watches Cataloged`,
      body: `
**Slow Crawler Progress Update**

✅ **${totalResults} museum-quality pieces** found
💰 **Total collection value:** $${totalValue.toLocaleString()}
📊 **Current page:** ${progress.currentPage}
🔗 **View museum:** ${MUSEUM_URL}

**Top 5 Most Expensive:**
${progress.results
  .sort((a, b) => b.price - a.price)
  .slice(0, 5)
  .map((w, i) => `${i + 1}. ${w.title} - $${w.price.toLocaleString()} (${w.source})`)
  .join('\n')}

Last updated: ${new Date(progress.lastUpdate).toLocaleString()}
      `.trim(),
      timestamp: new Date().toISOString()
    };

    // Send to Steve via webhook
    const response = await fetch(STEVE_WEBHOOK, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(message)
    });

    if (response.ok) {
      console.log('✅ Notification sent to Steve (CEO)');
      console.log(`   ${totalResults} results, $${totalValue.toLocaleString()} total value`);
    } else {
      console.error('❌ Failed to send notification:', response.statusText);
    }

  } catch (error) {
    console.error('❌ Error notifying Steve:', error.message);
  }
}

// Run immediately if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
  notifySteve();
}

export default notifySteve;