-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
268 lines (227 loc) · 10.4 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const zlib = require('zlib');
const parseString = require('xml2js').parseString;
const axios = require('axios');
const util = require('util')
const pLimit = require('p-limit');
/**
* XML Sitemap URL Scraper
* @param {array} sitemapArray is an array of xml sitemap urls, ex: "https://www.example.com/sitemap.xml"
* @param {number} compressedConcurrent is the number of compressed XML sitemaps to process at once (lower numbers save on CPU and Memory resources.)
* @returns {array} of urls from all sitemaps provided
*/
const sitemapUrlScraper = (sitemapArray, compressedConcurrent = 1) => {
return new Promise((resolve, reject) => {
// Array to hold all URLs parsed out of all sitemaps
let allUrls = [];
// Array to hold sitemaps we're about to process
let childSitemaps = [];
// First, get parent level XML sitemaps as an array of JSON objects
let parentSiteMaps = getSitemapsAsJson(sitemapArray);
parentSiteMaps
.then((result) => {
// Add any parsed urlset(s) to the allUrls array
handleUrlsets(result.filter(item => {
if(typeof item["urlset"] !== "undefined") {
return true;
}
}));
// Get all child sitemap urls
childSitemaps = getSitemapUrlsFromIndexes(result.filter(item => {
if (typeof item["sitemapindex"] !== "undefined") {
return true;
}
}));
// Parse child sitemaps that are NOT compressed, as JSON
return getSitemapsAsJson(childSitemaps.filter(currentSitemap => {
if (/\.gz$/i.test(currentSitemap)) {
return false;
} else {
return true;
}
}))
})
.then((result) => {
// Add any parsed urlset(s) to the allUrls array
handleUrlsets(result.filter(item => {
if(typeof item["urlset"] !== "undefined") {
return true;
}
}));
// console.log("Completed parsing non-compressed child site maps: ", util.inspect(allUrls, { maxArrayLength: null }))
// Parse child sitemaps that ARE compressed, as JSON
return handleCompressedXml(childSitemaps.filter(currentSitemap => {
if (/\.gz$/i.test(currentSitemap)) {
return true;
}
}))
})
.then((result) => {
// console.log("Completed getting all urlset(s) from the compressed XML sitemaps.");
// Add any parsed urlset(s) to the allUrls array
handleUrlsets(result.filter(item => {
if(typeof item["urlset"] !== "undefined") {
return true;
}
}));
// console.log("Completed parsing all sitemaps.\nAll URLS: ", util.inspect(allUrls, { maxArrayLength: 100 }))
// console.log("Total Urls: ", allUrls.length);
// Final return for function
return resolve(allUrls);
})
.catch(err => {
console.log("Error: ", err);
reject(err);
})
/**
* Handle urlsets
* @param {array} urlsetArray is an array of urlset objects parsed from xml
* @returns {*} nothing, just pushes urls into the allUrls array
*/
function handleUrlsets(urlsetArray) {
// Push urls to allUrls array
urlsetArray.forEach(urlSet => {
urlSet["urlset"]["url"].forEach(url => {
allUrls.push(url["loc"][0])
})
})
// console.log("URLSET URLs: ", allUrls);
}
/**
* Get Sitemap Urls From Indexes
* Gets the URL's of all sitemaps listed in the all of the provided sitemapindex(s)
* @param {array} sitemapindexArray is an array of sitemapindex objects parsed from xml
* @returns {array} of child sitemaps
*/
function getSitemapUrlsFromIndexes(sitemapindexArray) {
// Create an array of all sitemap urls
let allChildSitemaps = [];
// For each sitemapindex
sitemapindexArray.forEach(sitemapindex => {
// for each sitemap object
sitemapindex["sitemapindex"]["sitemap"].forEach(sitemapObject => {
// Add each sitemap url to our allChildSitemaps object (trim any trailing "/" to make consistent)
allChildSitemaps.push(sitemapObject["loc"][0].replace(/\/$/, ""));
})
})
return allChildSitemaps;
}
/**
* Get Sitemaps As JSON
* Inputs an array of XML sitemap URLS
* Outputs an array of JSON objects, converted from the XML
* @param {array} sitemapArray is an array of URL's to xml sitemaps
* @returns {array} a promise resolving with array of parsed xml sitemaps as JSON
*/
function getSitemapsAsJson(sitemapArray) {
return new Promise ((resolve, reject) => {
// Create an array of promises for each sitemap request we send
const promises = sitemapArray.reduce((accumulator, currentSitemap) => {
accumulator.push(new Promise((resolve, reject) => {
console.log("Processing XML Sitemap: ", currentSitemap);
// Else - if sitemap is a real URL
axios.get(currentSitemap)
.then((response) => {
// Parse XML into JSON
parseString(response.data.toString(), (err, result) => {
if(err) {
console.log("Sitemap error: ", err);
reject(err);
} else {
// console.log("RESULT: ", result);
resolve(result);
}
});
})
.catch(err => {
reject(err);
});
}));
return accumulator;
}, []);
// When all promises are resolved (all parent sitemaps retrieved)
Promise.all(promises)
.then(result => {
// console.log("Done getting batch of " + sitemapArray.length + " XML sitemaps.");
resolve(result);
})
.catch(err => {
console.log("XML sitemap batch error: ", err);
reject(err);
});
});
}
/**
* Handle Compressed XML
* Upzips and parses as json XML sitemaps. This is done with limited concurrency to avoid memory and CPU resource issues.
* @param {array} compressedSitemapArray is an array of compressed XML sitemap URLS
* @returns {*} promise resolving with an array of sitemaps parsed as JSON
*/
function handleCompressedXml(compressedSitemapArray) {
return new Promise((resolve, reject) => {
// Array to store parsed XML JSON
let parsedXmlArray = [];
// Define our promise limiter with desired concurrency (taken from main function params)
const promiseLimit = pLimit(compressedConcurrent);
// Create an array of our promise returning functions
let promises = compressedSitemapArray.map(sitemapUrl => {
return promiseLimit(() => processCompressedXmlFile(sitemapUrl));
});
(async () => {
// Only one promise is run at once
const result = await Promise.all(promises);
// console.log(result);
return resolve(result);
})();
});
}
// Retrieve a stream of the zip file, pipe it to gunzip, then parse the XML file as json - push the JSON to the parsedXmlArray - then resolve the promise to move to the next item
function processCompressedXmlFile(sitemapUrl) {
return new Promise((resolve, reject) => {
console.log("Processing Compressed XML Sitemap: ", sitemapUrl);
// Configure axios to receive a response type of stream
axios({
method:'get',
url: sitemapUrl,
responseType:'stream'
})
.then((response) => {
// Buffer to hold file download stream chunks
let buffer = [];
// Instantiate Gunzip
let gunzip = zlib.createGunzip();
// Pipe response stream data to gunzip instance
response.data.pipe(gunzip);
// Handle Data / End / Error events
gunzip
.on('data', function(data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString())
})
.on("end", function() {
// response and decompression complete, join the buffer
let fullResponse = buffer.join("");
// Parse the xml string into JSON
parseString(fullResponse, (err, result) => {
if(err) {
console.log("Compressed sitemap error: ", err);
reject(err);
} else {
// Resolve with the JSON parsed
resolve(result);
}
});
})
.on("error", function(e) {
reject(console.log("Gunzip Error: ", e));
})
})
.catch(err => {
reject("Axios gzip stream get error. ", err);
});
});
}
});
}
module.exports = {
sitemapUrlScraper
}