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
JavaScript
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
};