import { google } from 'googleapis'; import fs from 'fs'; import path from 'path'; import { getAllIndexableUrls } from '../src/lib/indexnow'; // ========================================== // CONFIGURATION // ========================================== // Path to your Service Account Key (JSON file) const KEY_FILE = path.join(__dirname, '../service_account.json'); // Urls are now fetched dynamically from src/lib/indexnow.ts // ========================================== async function runUsingServiceAccount() { console.log('šŸš€ Starting Google Indexing Script (All Pages)...'); if (!fs.existsSync(KEY_FILE)) { console.error('\nāŒ ERROR: Service Account Key not found!'); console.error(` Expected path: ${KEY_FILE}`); console.error(' Please follow the instructions in INDEXING_GUIDE.md to create and save the key.'); return; } console.log(`šŸ”‘ Authenticating with key file: ${path.basename(KEY_FILE)}...`); const auth = new google.auth.GoogleAuth({ keyFile: KEY_FILE, scopes: ['https://www.googleapis.com/auth/indexing'], }); try { const client = await auth.getClient(); console.log('āœ… Authentication successful.'); console.log(' Gathering URLs to index...'); const allUrls = getAllIndexableUrls(); console.log(` Found ${allUrls.length} URLs to index.`); for (const url of allUrls) { console.log(`\nšŸ“„ Processing: ${url}`); try { const result = await google.indexing('v3').urlNotifications.publish({ auth: client, requestBody: { url: url, type: 'URL_UPDATED' } }); console.log(` šŸ‘‰ Status: ${result.status} ${result.statusText}`); // Optional: Log more details from result.data if needed } catch (innerError: any) { console.error(` āŒ Failed to index ${url}`); if (innerError.response) { console.error(` Reason: ${innerError.response.status} - ${JSON.stringify(innerError.response.data)}`); // 429 = Quota exceeded // 403 = Permission denied (check service account owner status) } else { console.error(` Reason: ${innerError.message}`); } } // Optional: Add a small delay to avoid hitting rate limits too fast if you have hundreds of URLs // await new Promise(resolve => setTimeout(resolve, 500)); } console.log('\n✨ Done! All requests processed.'); console.log(' Note: Check Google Search Console for actual indexing status over time.'); } catch (error: any) { console.error('\nāŒ Fatal error occurred:'); console.error(error.message); } } runUsingServiceAccount();