UNPKG

screenshot-monitor-pro

Version:

A Node.js package for automated screenshot monitoring with SQLite database storage and configurable capture intervals

366 lines (300 loc) • 11.5 kB
#!/usr/bin/env node const ScreenshotMonitor = require('./ScreenshotMonitor'); const path = require('path'); const os = require('os'); // Parse command line arguments const args = process.argv.slice(2); const command = args[0]; // Default configuration const defaultConfig = { frequency: 30000, screenshotQuality: 90, thumbnailWidth: 300, thumbnailHeight: 200, userDataPath: path.join(os.homedir(), '.screenshot-monitor-pro') }; // Help function function showHelp() { console.log(` Screenshot Monitor Pro - CLI Tool Usage: node cli.js <command> [options] Commands: start Start screenshot monitoring stop Stop screenshot monitoring capture Capture a single screenshot list List recent screenshots stats Show statistics monitors Show available monitors config Show current configuration Options: --frequency <ms> Screenshot interval in milliseconds (default: 30000) --quality <1-100> Screenshot quality (default: 90) --thumb-width <px> Thumbnail width (default: 300) --thumb-height <px> Thumbnail height (default: 200) --path <directory> Custom storage path --limit <number> Number of screenshots to list (default: 10) --monitor <id> Specific monitor ID to capture/list Examples: node cli.js start --frequency 60000 node cli.js capture node cli.js list --limit 5 node cli.js list --monitor 0 node cli.js stats node cli.js monitors `); } // Parse options function parseOptions(args) { const options = { ...defaultConfig }; for (let i = 0; i < args.length; i++) { const arg = args[i]; switch (arg) { case '--frequency': options.frequency = parseInt(args[++i]) || 30000; break; case '--quality': options.screenshotQuality = parseInt(args[++i]) || 90; break; case '--thumb-width': options.thumbnailWidth = parseInt(args[++i]) || 300; break; case '--thumb-height': options.thumbnailHeight = parseInt(args[++i]) || 200; break; case '--path': options.userDataPath = args[++i]; break; case '--limit': options.limit = parseInt(args[++i]) || 10; break; case '--monitor': options.monitorId = parseInt(args[++i]); break; } } return options; } // Start monitoring async function startMonitoring(options) { console.log('šŸš€ Starting Screenshot Monitor Pro...'); console.log(`šŸ“ Storage path: ${options.userDataPath}`); console.log(`ā±ļø Frequency: ${options.frequency}ms`); console.log(`šŸ–¼ļø Quality: ${options.screenshotQuality}%`); console.log(`šŸ“ Thumbnail size: ${options.thumbnailWidth}x${options.thumbnailHeight}`); console.log(''); const monitor = new ScreenshotMonitor(options); try { await monitor.init(); console.log('āœ… Monitor initialized successfully'); await monitor.start(); console.log('āœ… Screenshot monitoring started'); console.log('Press Ctrl+C to stop monitoring'); // Handle graceful shutdown process.on('SIGINT', async () => { console.log('\nšŸ›‘ Stopping monitor...'); monitor.stop(); await monitor.close(); console.log('āœ… Monitor stopped'); process.exit(0); }); } catch (error) { console.error('āŒ Failed to start monitor:', error.message); process.exit(1); } } // Capture single screenshot async function captureScreenshot(options) { console.log('šŸ“ø Capturing screenshot...'); const monitor = new ScreenshotMonitor(options); try { await monitor.init(); if (options.monitorId !== undefined) { // Capture from specific monitor const monitors = monitor.getMonitors(); const targetMonitor = monitors.find(m => m.id === options.monitorId); if (!targetMonitor) { console.error(`āŒ Monitor ${options.monitorId} not found`); process.exit(1); } const result = await monitor.captureScreenshot(targetMonitor); console.log('āœ… Screenshot captured successfully'); console.log(`šŸ–¼ļø Monitor: ${targetMonitor.name} (ID: ${targetMonitor.id})`); console.log(`šŸ“ File: ${result.filename}`); console.log(`šŸ–¼ļø Thumbnail: ${result.thumbnailFilename}`); console.log(`šŸ“‚ Path: ${result.filePath}`); } else { // Capture from all monitors const results = await monitor.captureAllScreenshots(); console.log(`āœ… Captured ${results.length} screenshot(s) successfully`); results.forEach(result => { console.log(`šŸ–¼ļø Monitor ${result.monitorId}: ${result.filename}`); }); } await monitor.close(); } catch (error) { console.error('āŒ Failed to capture screenshot:', error.message); process.exit(1); } } // List screenshots async function listScreenshots(options) { console.log('šŸ“‹ Listing recent screenshots...'); const monitor = new ScreenshotMonitor(options); try { await monitor.init(); const screenshots = await monitor.getScreenshots(options.limit || 10, 0, options.monitorId); if (screenshots.length === 0) { const monitorText = options.monitorId !== undefined ? ` for monitor ${options.monitorId}` : ''; console.log(`šŸ“­ No screenshots found${monitorText}`); } else { const monitorText = options.monitorId !== undefined ? ` for monitor ${options.monitorId}` : ''; console.log(`šŸ“ø Found ${screenshots.length} screenshots${monitorText}:\n`); screenshots.forEach((screenshot, index) => { const date = new Date(screenshot.timestamp).toLocaleString(); const size = Math.round(screenshot.file_size / 1024); console.log(`${index + 1}. ${screenshot.filename}`); console.log(` šŸ–¼ļø Monitor: ${screenshot.monitor_name} (ID: ${screenshot.monitor_id})`); console.log(` šŸ“… ${date}`); console.log(` šŸ“ ${screenshot.screen_width}x${screenshot.screen_height}`); console.log(` šŸ’¾ ${size} KB`); console.log(` šŸ“‚ ${screenshot.file_path}`); console.log(''); }); } await monitor.close(); } catch (error) { console.error('āŒ Failed to list screenshots:', error.message); process.exit(1); } } // Show statistics async function showStats(options) { console.log('šŸ“Š Getting statistics...'); const monitor = new ScreenshotMonitor(options); try { await monitor.init(); if (options.monitorId !== undefined) { // Show stats for specific monitor const stats = await monitor.getStats(options.monitorId); const monitors = monitor.getMonitors(); const targetMonitor = monitors.find(m => m.id === options.monitorId); console.log(`šŸ“ˆ Screenshot Statistics for Monitor ${options.monitorId}:\n`); console.log(`šŸ–¼ļø Monitor: ${targetMonitor ? targetMonitor.name : 'Unknown'}`); console.log(`šŸ“ø Total screenshots: ${stats.total_screenshots || 0}`); console.log(`šŸ’¾ Total size: ${Math.round((stats.total_size || 0) / 1024)} KB`); if (stats.first_screenshot) { console.log(`šŸ“… First screenshot: ${new Date(stats.first_screenshot).toLocaleString()}`); } if (stats.last_screenshot) { console.log(`šŸ“… Last screenshot: ${new Date(stats.last_screenshot).toLocaleString()}`); } } else { // Show overall stats const stats = await monitor.getStats(); const monitorStats = await monitor.getMonitorStats(); console.log('šŸ“ˆ Overall Screenshot Statistics:\n'); console.log(`šŸ“ø Total screenshots: ${stats.total_screenshots || 0}`); console.log(`šŸ’¾ Total size: ${Math.round((stats.total_size || 0) / 1024)} KB`); if (stats.first_screenshot) { console.log(`šŸ“… First screenshot: ${new Date(stats.first_screenshot).toLocaleString()}`); } if (stats.last_screenshot) { console.log(`šŸ“… Last screenshot: ${new Date(stats.last_screenshot).toLocaleString()}`); } if (monitorStats.length > 0) { console.log('\nšŸ“Š Per-Monitor Statistics:'); monitorStats.forEach(monitorStat => { console.log(` Monitor ${monitorStat.monitor_id} (${monitorStat.monitor_name}):`); console.log(` šŸ“ø Screenshots: ${monitorStat.screenshot_count}`); console.log(` šŸ’¾ Size: ${Math.round(monitorStat.total_size / 1024)} KB`); }); } } await monitor.close(); } catch (error) { console.error('āŒ Failed to get statistics:', error.message); process.exit(1); } } // Show configuration function showConfig(options) { console.log('āš™ļø Current Configuration:\n'); console.log(`ā±ļø Frequency: ${options.frequency}ms`); console.log(`šŸ–¼ļø Quality: ${options.screenshotQuality}%`); console.log(`šŸ“ Thumbnail width: ${options.thumbnailWidth}px`); console.log(`šŸ“ Thumbnail height: ${options.thumbnailHeight}px`); console.log(`šŸ“ Storage path: ${options.userDataPath}`); } // Show available monitors async function showMonitors(options) { console.log('šŸ–„ļø Detecting monitors...'); const monitor = new ScreenshotMonitor(options); try { await monitor.init(); const monitors = monitor.getMonitors(); if (monitors.length === 0) { console.log('āŒ No monitors detected'); } else { console.log(`āœ… Found ${monitors.length} monitor(s):\n`); monitors.forEach((monitor, index) => { console.log(`${index + 1}. Monitor ${monitor.id}`); console.log(` šŸ“ Name: ${monitor.name}`); console.log(` šŸ“ Resolution: ${monitor.width}x${monitor.height}`); console.log(` šŸ“ Position: (${monitor.x}, ${monitor.y})`); console.log(` šŸ”¢ Index: ${monitor.index}`); console.log(''); }); } await monitor.close(); } catch (error) { console.error('āŒ Failed to detect monitors:', error.message); process.exit(1); } } // Main CLI logic async function main() { if (!command || command === '--help' || command === '-h') { showHelp(); return; } const options = parseOptions(args.slice(1)); switch (command) { case 'start': await startMonitoring(options); break; case 'capture': await captureScreenshot(options); break; case 'list': await listScreenshots(options); break; case 'stats': await showStats(options); break; case 'monitors': await showMonitors(options); break; case 'config': showConfig(options); break; default: console.error(`āŒ Unknown command: ${command}`); showHelp(); process.exit(1); } } // Run CLI if (require.main === module) { main().catch(error => { console.error('āŒ CLI Error:', error.message); process.exit(1); }); } module.exports = { startMonitoring, captureScreenshot, listScreenshots, showStats, showConfig, showMonitors };