-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (66 loc) · 2.34 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
'use strict';
const cheerio = require('cheerio');
const fetch = require('node-fetch');
const currency = require('currency.js');
const USD = value => currency(value);
const USD_REGEX = /^\$/;
const NTD = value => currency(value, { symbol: 'NT$', precision: 0 });
const NTD_REGEX = /^NT\$/;
let getSum = (getCount) => {
if (!(getCount)) {
throw new Error("Argument missing");
}
let url = 'https://playboard.co/en/youtube-ranking/most-superchatted-v-tuber-channels-in-worldwide-daily';
console.log(`Fetch URL: ${url}`);
console.log();
fetch(url)
.then(function (response) {
return response.text();
})
.then(function (body) {
let $ = cheerio.load(body);
let title = $('title').text();
console.log(title);
console.log();
let name = [];
let data = [];
$('.name__label h3').each(function () {
name.push($(this).text());
});
$('.fluc-label.fluc-label--en.fluc-label--symbol-math.up').each(function () {
data.push($(this).text());
});
let sum = 0;
let n = data.length;
let counter = 1;
let currencyType;
if (NTD_REGEX.test(data[0])) {
currencyType = "NTD";
} else if (USD_REGEX.test(data[0])) {
currencyType = "USD";
} else {
currencyType = null;
throw new Error("Unable to determine Currency Type");
}
for (let i = 0; i < n && counter <= getCount; i += 2, counter++) {
console.log(counter);
console.log(name[counter - 1]);
console.log(data[i]);
// console.log(currency(data[i]).value);
sum += currency(data[i]).value;
console.log();
}
counter--;
console.log(`Data Count: ${counter}`);
console.log(`Date in local time: ${Date().toLocaleString()}`);
switch (currencyType) {
case "NTD":
console.log(`Sum: ${NTD(sum).format()}`);
break;
case "USD":
console.log(`Sum: ${USD(sum).format()}`);
break;
}
});
}
getSum(10);