forked from dixoncheng/covid19map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
81 lines (69 loc) · 1.92 KB
/
scraper.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
import cheerio from "cheerio";
import locations from "./data/regions";
const fetch = require("@zeit/fetch-retry")(require("node-fetch"));
// import mohHtml from "./moh-html";
import { staticData } from "./data/static";
const URL = `https://www.health.govt.nz/our-work/diseases-and-conditions/covid-19-novel-coronavirus/covid-19-current-cases?${new Date()}`;
const scraper = async () => {
const response = await fetch(URL);
const html = await response.text();
// const html = mohHtml;
const $ = await cheerio.load(html);
// const lastUpdated = $(".page_updated .date").text();
const lastUpdated = $(".field-name-body .georgia-italic")
.first()
.text();
let rows = [];
$(".table-style-two")
.eq(1)
.find("tbody tr")
.each((i, elem) => {
const location = $(elem)
.find("td:nth-child(1)")
.text()
.trim();
const totalCases = parseInt(
$(elem)
.find("td:nth-child(2)")
.text()
.trim()
);
if (location && location !== "Total" && totalCases > 0) {
const latlngItem = locations.find(x => location === x.name);
if (!latlngItem) {
throw new Error(`No location "${location}" exist`);
}
const confirmedCases = parseInt(
$(elem)
.find("td:nth-child(2)")
.text()
.trim()
);
const probableCases = parseInt(
$(elem)
.find("td:nth-child(3)")
.text()
.trim()
);
rows.push({
location,
confirmedCases,
probableCases,
totalCases,
...latlngItem
});
}
});
rows.sort((a, b) => {
if (a.totalCases === b.totalCases) {
return a.location > b.location ? 1 : -1;
}
return a.totalCases > b.totalCases ? -1 : 1;
});
return {
staticData,
locations: rows,
lastUpdated
};
};
export default scraper;