-
Notifications
You must be signed in to change notification settings - Fork 10
/
scrapeTencent.js
172 lines (146 loc) · 4.47 KB
/
scrapeTencent.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
const https = require('https');
const db = require('../system/database');
const tencentUrl = 'https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_area_counts';
const countryMap = {
"中国": "China",
"加拿大": "Canada",
"尼泊尔": "Nepal",
"巴基斯坦": "Pakistan",
"新加坡": "Singapore",
"日本": "Japan",
"法国": "France",
"泰国": "Thailand",
"澳大利亚": "Australia",
"美国": "United States",
"越南": "Vietnam",
"韩国": "South Korea",
"马来西亚": "Malaysia",
"德国": "Germany",
"斯里兰卡": "Sri Lanka",
"芬兰": "Finland",
"阿联酋": "United Arab Emirates",
"菲律宾": "Philippines",
"印度": "India",
};
const areaMap = {
"湖北": "Hubei",
"广东": "Guangdong",
"浙江": "Zhejiang",
"重庆": "Chongqing",
"湖南": "Hunan",
"安徽": "Anhui",
"北京": "Beijing",
"上海": "Shanghai",
"河南": "Henan",
"四川": "Sichuan",
"山东": "Shandong",
"广西": "Guangxi",
"江西": "Jiangxi",
"福建": "Fujian",
"江苏": "Jiangsu",
"海南": "Hainan",
"辽宁": "Liaoning",
"陕西": "Shaanxi",
"云南": "Yunnan",
"天津": "Tianjin",
"黑龙江": "Heilongjiang",
"河北": "Hebei",
"山西": "Shanxi",
"香港": "Hong Kong",
"贵州": "Guizhou",
"吉林": "Jilin",
"甘肃": "Gansu",
"宁夏": "Ningxia",
"台湾": "Taiwan",
"新疆": "Xinjiang",
"澳门": "Macao",
"内蒙古": "Inner Mongolia",
"青海": "Qinghai",
"西藏": "Tibet"
};
async function processData(data) {
const translatedData = data
.map(d => {
if (countryMap[d.country]) {
d.country = countryMap[d.country];
}
if (areaMap[d.area]) {
d.area = areaMap[d.area];
}
return d;
});
const dataByArea = translatedData.reduce((accumulate, current) => {
const { country, area, confirm, suspect, dead, heal } = current;
const key = country + area;
if (!accumulate[key]) {
accumulate[key] = { country, area, confirm, suspect, dead, heal };
return accumulate;
}
accumulate[key].confirm += confirm;
accumulate[key].suspect += suspect;
accumulate[key].dead += dead;
accumulate[key].heal += heal;
return accumulate;
}, {});
// console.log("dataByArea:", Object.values(dataByArea));
const conn = db.conn.promise();
await importDataGroupByAreaToDb(conn, Object.values(dataByArea));
const dataByCountry = translatedData.reduce((accumulate, current) => {
const { country, confirm, suspect, dead, heal } = current;
if (!accumulate[country]) {
accumulate[country] = { country, confirm, suspect, dead, heal };
return accumulate;
}
accumulate[country].confirm += confirm;
accumulate[country].suspect += suspect;
accumulate[country].dead += dead;
accumulate[country].heal += heal;
return accumulate;
}, {});
// console.log("dataByCountry:", Object.values(dataByCountry));
await importDataGroupByCountryToDb(conn, Object.values(dataByCountry));
conn.end();
}
async function importDataGroupByAreaToDb(conn, data) {
const currentTimestamp = Math.ceil(new Date().getTime() / 1000);
for (let i = 0; i < data.length; i++) {
const d = data[i];
const query = `INSERT INTO tencent_data_by_area (country, area, num_confirm, num_suspect, num_dead, num_heal, created) VALUES (?, ?, ?, ?, ?, ?, ?)`;
const args = [d.country, d.area, d.confirm, d.suspect, d.dead, d.heal, currentTimestamp];
try {
await conn.execute(query, args);
}
catch (ex) {
console.log(ex);
}
}
}
async function importDataGroupByCountryToDb(conn, data) {
const currentTimestamp = Math.ceil(new Date().getTime() / 1000);
for (let i = 0; i < data.length; i++) {
const d = data[i];
const query = `INSERT INTO tencent_data_by_country (country, num_confirm, num_suspect, num_dead, num_heal, created) VALUES (?, ?, ?, ?, ?, ?)`;
const args = [d.country, d.confirm, d.suspect, d.dead, d.heal, currentTimestamp];
try {
await conn.execute(query, args);
}
catch (ex) {
console.log(ex);
}
}
}
https.get(tencentUrl, (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', async () => {
const json = JSON.parse(data);
const statsData = JSON.parse(json.data);
await processData(statsData);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});