-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-urls-gb.js
109 lines (92 loc) · 3.41 KB
/
generate-urls-gb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
import { google } from 'googleapis';
import { GoogleAuth } from 'google-auth-library';
import dotenv from 'dotenv';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const blogPostsDir = path.join(__dirname, 'src/pages/blog/posts');
const siteUrl = 'https://www.leadgen-gpt.com';
function getBlogPostUrls() {
console.log('Scanning blog posts directory...');
const files = fs.readdirSync(blogPostsDir);
console.log(`Found ${files.length} blog posts.`);
return files.map(file => `${siteUrl}/blog/posts/${path.parse(file).name}`);
}
async function submitToIndexNow(urls) {
const apiKey = '909d02eacbeb4db79775a48c41642530';
const keyLocation = `${siteUrl}/${apiKey}.txt`;
const payload = {
host: siteUrl.replace('https://', ''),
key: apiKey,
keyLocation: keyLocation,
urlList: urls
};
const response = await fetch('https://api.indexnow.org/IndexNow', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify(payload),
});
if (response.ok) {
console.log('URLs submitted to IndexNow successfully');
} else {
console.error('Error submitting URLs to IndexNow:', response.statusText);
}
}
async function submitToGoogle(urls) {
console.log('GOOGLE_APPLICATION_CREDENTIALS:', process.env.GOOGLE_APPLICATION_CREDENTIALS_BASE64);
const serviceAccountBase64 = process.env.GOOGLE_APPLICATION_CREDENTIALS_BASE64;
const serviceAccountJson = Buffer.from(serviceAccountBase64, 'base64').toString('utf-8');
const serviceAccount = JSON.parse(serviceAccountJson);
const auth = new GoogleAuth({
credentials: serviceAccount,
scopes: ['https://www.googleapis.com/auth/indexing'],
});
const authClient = await auth.getClient();
const indexing = google.indexing({ version: 'v3', auth: authClient });
const requests = urls.map(url => ({
url,
type: 'URL_UPDATED'
}));
try {
for (const request of requests) {
let retryCount = 0;
const maxRetries = 3;
let success = false;
while (retryCount < maxRetries && !success) {
try {
const response = await indexing.urlNotifications.publish({
requestBody: request
});
console.log('URL submitted to Google successfully:', response.data);
success = true;
} catch (error) {
retryCount++;
if (retryCount >= maxRetries) {
throw error;
}
console.error(`Retrying (${retryCount}/${maxRetries})...`, error);
}
}
}
} catch (error) {
console.error('Error submitting URLs to Google:', error);
}
}
async function main() {
const newBlogPostUrls = getBlogPostUrls();
console.log('New blog post URLs:', newBlogPostUrls);
if (newBlogPostUrls.length > 0) {
// await submitToIndexNow(newBlogPostUrls);
await submitToGoogle(newBlogPostUrls);
} else {
console.log('No new blog posts found. Nothing to submit.');
}
}
main();