-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.62 KB
/
index.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
const fs = require('fs');
const https = require('https');
const xml2js = require('xml2js');
const outputFile = 'urls.txt';
const sitemapUrls = fs.readFileSync('sitemaps.txt', 'utf-8').split('\n');
// const sitemapUrls = [
// 'https://example.com/sitemap.xml',
// 'https://example.com/fr/sitemap.xml',
// 'https://example.com/de/sitemap.xml'
// ];
function processSitemap(sitemapUrl) {
https.get(sitemapUrl, (res) => {
if (res.statusCode !== 200) {
console.error(`Failed to fetch sitemap from ${sitemapUrl} - (${res.statusCode}): ${res.statusMessage}`);
return;
}
// Concatenate the response data
let data = '';
res.on('data', chunk => {
data += chunk;
});
// Parse the XML data when the response is complete
res.on('end', () => {
xml2js.parseString(data, (err, result) => {
if (err) {
console.error(err);
return;
}
// Extract all the URLs from the sitemap
const urls = result.urlset.url.map(url => url.loc[0]);
// Append the URLs to a file
fs.appendFile(outputFile, urls.join('\n') + '\n', (err) => {
if (err) {
console.error(err);
return;
}
console.log(`Appended ${urls.length} URLs from ${sitemapUrl} to ${outputFile} `);
});
});
});
}).on('error', (err) => {
console.error(err);
});
}
sitemapUrls.forEach(processSitemap);