-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid.js
85 lines (81 loc) · 2.74 KB
/
covid.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
const axios = require("axios");
const jsdom = require("jsdom");
var fs = require("fs");
async function getCovid19Info(lag) {
try {
const url = getUrl();
const items = await getItems(url);
const russiaFirstIndex = items.findIndex(
(o) => o.name === "Алтайский край"
);
const russiaFirstHistogram = items[russiaFirstIndex].histogram;
const [lastItemRussiaFirstHistogram] = russiaFirstHistogram.slice(lag ? lag : -1);
printDate(lastItemRussiaFirstHistogram.ts);
const russiaItems = items.slice(russiaFirstIndex);
const cases = russiaItems.map((o) => o.histogram.slice(lag ? lag : -1)[0].value);
const date = getDateString(lastItemRussiaFirstHistogram.ts);
cases.unshift(date);
console.log(date);
const res = cases.join("\n");
fs.writeFile("covid.csv", res, function (err) {
if (err) return console.log(err);
console.log("Wrote in covid.csv");
});
} catch (error) {
console.error(error);
}
function getUrl() {
const url = "https://yandex.ru/maps/covid19";
return url;
}
async function getItems(url) {
const response = await axios.get(url);
const { JSDOM } = jsdom;
const { document } = (new JSDOM(response.data)).window;
const html = document.getElementsByClassName("config-view")[0].innerHTML;
const json = JSON.parse(html);
return json.covidData.items;
}
async function getUrlAndCookieOld() {
const url = "https://yandex.ru/maps/api/covid19";
const response1 = await axios.get(url);
const {
data: { csrfToken },
} = response1;
const cookie = response1.headers["set-cookie"][0];
let urlWithCsrf = `https://yandex.ru/maps/api/covid?ajax=1&csrfToken=${csrfToken}&lang=ru&s=3701076815&sessionId=1591195411184_644096`;
console.log(urlWithCsrf)
return { url: urlWithCsrf, cookie };
}
async function getItemsOld(url, cookie) {
const responseWithCsrf = await axios({
method: "get",
url,
headers: { Cookie: cookie },
});
console.log(responseWithCsrf)
const {
data: {
data: { items },
},
} = responseWithCsrf;
return items;
}
function printDate(timestamp) {
const today = new Date();
today.setHours(5,0,0,0)
const dateInfo = new Date();
dateInfo.setTime(timestamp * 1000);
if(dateInfo.getTime()-today.getTime() === -86400000)
console.log("Yesterday");
else
console.log(dateInfo.getTime() === today.getTime() ? "Today" : "Not Today!!");
}
function getDateString(timestamp) {
const dateInfo = new Date();
dateInfo.setTime(timestamp * 1000);
const options = { day: "numeric", month: "2-digit", year: "numeric" };
return dateInfo.toLocaleDateString("ru", options);
}
}
getCovid19Info(process.argv[2]);