-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (47 loc) · 2.08 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
const fetch = require('node-fetch');
//Credit to https://github.com/petasittek/chrome-web-store-stats/ for regex below
const URL_PREFIX = 'https://chrome.google.com/webstore/detail/';
const REGEX_NAME = '<meta itemprop="name" content="([^"]*)"/>';
// const REGEX_INSTALL_COUNT = '<Attribute name="user_count">([0-9]*)</Attribute>';
const REGEX_INSTALL_COUNT = /content="UserDownloads:([\d,+]+)/;
const REGEX_RATING_COUNT = '([0-9]*) users rated this';
const REGEX_RATING_VALUE = 'Average rating ([0-9.]*) out of 5.';
// const REGEX_RATING_COUNT = '<meta itemprop="ratingCount" content="([0-9]*)"/>'; //No longer scrapeable
// const REGEX_RATING_VALUE = '<meta itemprop="ratingValue" content="([0-9.]*)"/>'; //No longer scrapeable
//Credit to https://github.com/petasittek/chrome-web-store-stats/ for regex above
const REGEX_EXTENSIONID = /[a-z]{32}$/;
module.exports = async function(inputIDs) {
var extensionIDs = [].concat(inputIDs);
var responses = {};
for (var i = 0, l = extensionIDs.length; i < l; i++) {
var extensionID = extensionIDs[i];
if (typeof extensionID != "string" || !REGEX_EXTENSIONID.test(extensionID.toLowerCase())) {
responses[extensionID] = createResponse(false, 'Invalid extension ID.');
continue;
}
try {
const response = await fetch(`${URL_PREFIX}${extensionID}`);
const data = await response.text();
const name = data.match(REGEX_NAME)[1];
const installCount = data.match(REGEX_INSTALL_COUNT)[1] + "";
const ratingCount = parseInt(data.match(REGEX_RATING_COUNT)[1]);
const ratingValue = parseFloat(data.match(REGEX_RATING_VALUE)[1]);
responses[extensionID] = createResponse(true, false, name, installCount, ratingCount, ratingValue);
continue;
} catch (e) {
responses[extensionID] = createResponse(false, `Couldn't find extension with ID ${extensionID}`);
continue;
}
}
return extensionIDs.length <= 1 ? responses[extensionIDs[0]] : responses;
}
function createResponse(success, error, name = '', installCount = "0", ratingCount = 0, ratingValue = 0) {
return {
success,
error,
name,
installCount,
ratingCount,
ratingValue
};
}