-
Notifications
You must be signed in to change notification settings - Fork 1
/
googlecloudfunction.js
63 lines (51 loc) · 1.53 KB
/
googlecloudfunction.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
const https = require('https');
const cheerio = require('cheerio');
exports.helloWorld = function helloWorld(request, response) {
response.set('Access-Control-Allow-Origin', "*");
response.set('Access-Control-Allow-Methods', 'GET, POST');
https.get('https://coinmarketcap.com/tokens/', (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^text\/html/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected text/html but received ${contentType}`);
}
if (error) {
response.status(500).send(error.message);
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
const tokens = scrapeERCTokens(rawData);
response.status(200).send(tokens);
} catch (e) {
response.status(500).send(e.message);
}
});
}).on('error', (e) => {
response.status(500).send(e.message);
});
};
function scrapeERCTokens(html) {
let result = [];
let $ = cheerio.load(html);
const tableRows = $('tbody').find('tr').each(function (i, elem) {
const name = $(this).find('.currency-name-container')[0];
const symbol = $(this).find('.currency-symbol')[0];
const platform = $(this).find('.platform-name')[0];
result.push({
name: $(name).text(),
symbol: $(symbol).text(),
platform: $(platform).text()
});
});
return result;
}