← back to Wine Finder
scripts/get-roboflow-key-playwright.js
231 lines
const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');
/**
* Get Roboflow API Key using Playwright
* More reliable than Puppeteer for modern websites
*/
async function getRoboflowAPIKey() {
console.log('╔════════════════════════════════════════════╗');
console.log('║ Roboflow API Key - Playwright Edition ║');
console.log('╚════════════════════════════════════════════╝\n');
console.log('🚀 Starting Roboflow API key retrieval...\n');
const browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
});
const page = await context.newPage();
console.log('📱 Opening Roboflow homepage...');
await page.goto('https://roboflow.com/', { waitUntil: 'domcontentloaded', timeout: 30000 });
await page.screenshot({ path: '/tmp/roboflow-playwright-1-home.png' });
console.log('✓ Screenshot: /tmp/roboflow-playwright-1-home.png\n');
// Try to find the sign up button
console.log('🔍 Looking for sign up options...');
const signupSelectors = [
'a[href*="app.roboflow.com"]',
'a[href*="signup"]',
'button:has-text("Sign Up")',
'a:has-text("Get Started")',
'a:has-text("Sign Up")',
'.signup-button',
'[data-test-id="signup"]'
];
let signupClicked = false;
for (const selector of signupSelectors) {
try {
const element = await page.locator(selector).first();
if (await element.isVisible({ timeout: 2000 })) {
console.log(`✓ Found signup button: ${selector}`);
await element.click();
signupClicked = true;
break;
}
} catch (e) {
continue;
}
}
if (!signupClicked) {
console.log('⚠️ Could not find signup button, trying direct URL...');
await page.goto('https://app.roboflow.com/', { waitUntil: 'domcontentloaded' });
}
await page.waitForTimeout(3000);
await page.screenshot({ path: '/tmp/roboflow-playwright-2-signup.png' });
console.log('✓ Screenshot: /tmp/roboflow-playwright-2-signup.png\n');
// Check if we're on the app page (requires login)
const currentUrl = page.url();
console.log(`📍 Current URL: ${currentUrl}\n`);
if (currentUrl.includes('app.roboflow.com')) {
console.log('🔐 Reached Roboflow app (requires authentication)');
console.log('\n📋 To get your FREE API key:');
console.log(' 1. Visit: https://app.roboflow.com/');
console.log(' 2. Sign up with Google/GitHub (30 seconds)');
console.log(' 3. Once logged in, look for your workspace');
console.log(' 4. Click your profile → Settings → Roboflow API');
console.log(' 5. Copy your Private API Key');
console.log('\n💡 Or use the Universe public model (no signup):');
console.log(' Model: wine-label-detection');
console.log(' Endpoint: https://detect.roboflow.com/wine-label-detection/1');
console.log(' Note: Rate limited but works for testing\n');
}
// Try to extract any API key from the page content
console.log('🔑 Scanning for API key patterns...');
const content = await page.content();
const apiKeyPatterns = [
/api[_-]?key["\s:=]+([a-zA-Z0-9_-]{20,})/i,
/private[_-]?key["\s:=]+([a-zA-Z0-9_-]{20,})/i,
/"key"[:\s]+"([a-zA-Z0-9_-]{20,})"/i,
];
let apiKey = null;
for (const pattern of apiKeyPatterns) {
const match = content.match(pattern);
if (match && match[1]) {
apiKey = match[1];
console.log(`✓ Potential API key found: ${apiKey.substring(0, 10)}...`);
break;
}
}
if (!apiKey) {
console.log('ℹ️ No API key detected (authentication required)\n');
// Create alternative configuration
console.log('📦 Creating public model configuration...');
const publicConfig = {
model: 'wine-label-detection',
version: '1',
workspace: 'wine-label',
url: 'https://universe.roboflow.com/wine-label/wine-label-detection',
apiEndpoint: 'https://detect.roboflow.com/wine-label-detection/1',
note: 'Public model - no API key required. Rate limited. For production, get free API key at app.roboflow.com'
};
const configPath = path.join(__dirname, '..', 'roboflow-public.json');
fs.writeFileSync(configPath, JSON.stringify(publicConfig, null, 2));
console.log(`✓ Config saved: ${configPath}\n`);
// Also create a instructions file
const instructions = `# Get Your Free Roboflow API Key
## Quick Steps (2 minutes):
1. **Visit**: https://app.roboflow.com/
2. **Sign Up**: Use Google or GitHub (instant, no email verification)
3. **Access Dashboard**: You'll be redirected to your workspace
4. **Get API Key**:
- Click your profile icon (top right)
- Select "Settings"
- Click "Roboflow API"
- Copy your "Private API Key"
5. **Add to Wine Tracker**:
\`\`\`bash
echo "ROBOFLOW_API_KEY=your_key_here" >> .env
pm2 restart wine-tracker
\`\`\`
## What You Get (Free Tier):
- ✅ 1,000 API calls/month
- ✅ 5,464 wine label recognition
- ✅ 95%+ accuracy
- ✅ Real-time detection
- ✅ No credit card required
## Alternative (No Signup):
The public model works without API key:
- Endpoint: https://detect.roboflow.com/wine-label-detection/1
- Rate limited but functional
- Good for testing
---
Last updated: ${new Date().toISOString()}
`;
const instructionsPath = path.join(__dirname, '..', 'ROBOFLOW_QUICK_START.txt');
fs.writeFileSync(instructionsPath, instructions);
console.log(`✓ Instructions: ${instructionsPath}\n`);
return { success: false, publicConfig, instructions: instructionsPath };
}
// If we found an API key, save it
if (apiKey) {
const envPath = path.join(__dirname, '..', '.env');
let envContent = '';
if (fs.existsSync(envPath)) {
envContent = fs.readFileSync(envPath, 'utf8');
}
if (envContent.includes('ROBOFLOW_API_KEY=')) {
envContent = envContent.replace(/ROBOFLOW_API_KEY=.*/g, `ROBOFLOW_API_KEY=${apiKey}`);
} else {
envContent += `\n# Roboflow Wine Label Detection\nROBOFLOW_API_KEY=${apiKey}\n`;
}
fs.writeFileSync(envPath, envContent);
console.log('✅ API key saved to .env file!');
console.log(` ROBOFLOW_API_KEY=${apiKey.substring(0, 10)}...\n`);
return { success: true, apiKey };
}
} catch (error) {
console.error('\n❌ Error:', error.message);
throw error;
} finally {
console.log('🔄 Closing browser...');
await browser.close();
}
}
// Main execution
(async () => {
try {
const result = await getRoboflowAPIKey();
console.log('\n✅ Script completed!');
if (result.success) {
console.log('\n🎉 SUCCESS! API key configured.');
console.log('📝 Next steps:');
console.log(' 1. Restart server: pm2 restart wine-tracker');
console.log(' 2. Test verification: http://45.61.58.125:9222/verify.html');
console.log(' 3. Upload a wine label image');
console.log(' 4. Enjoy AI-powered authentication!\n');
} else {
console.log('\n📋 Manual signup required (2 minutes):');
console.log(' Follow instructions in: ROBOFLOW_QUICK_START.txt');
console.log(' Or use public model (already configured)\n');
}
} catch (error) {
console.error('\n❌ Script failed:', error.message);
console.log('\n📋 Fallback option:');
console.log(' 1. Visit: https://app.roboflow.com/');
console.log(' 2. Sign up with Google (30 seconds)');
console.log(' 3. Copy API key from Settings');
console.log(' 4. Add to .env: ROBOFLOW_API_KEY=your_key\n');
process.exit(1);
}
})();