-
Notifications
You must be signed in to change notification settings - Fork 0
/
automateScript.js
207 lines (180 loc) · 6.98 KB
/
automateScript.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const axios = require("axios").default;
const healthLKAPI = "https://hpb.health.gov.lk/api/get-current-statistical";
var latestUpdate = null;
const bot_id = process.env.bot_id || require("./config/env").bot_id;
const chat_id = process.env.chat_id || require("./config/env").chat_id_group;
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const mongodbAPI = process.env.mongodbAPI || "mongodb://127.0.0.1:27017/covid19";
const Data = require("./models/data");
try {
mongoose.connect(
mongodbAPI,
{ useNewUrlParser: true, useUnifiedTopology: true },
err => {
if (!err) console.log("connected to mongodb sucsessfully" + "👍");
}
);
} catch (error) {
console.log(err);
}
setInterval(() => {
refresh();
}, 30000);
function refresh() {
axios
.get(healthLKAPI)
.then(result => {
if (result.data.message === "Success") {
// console.log(result.data);
console.log("total cases ", result.data.data.local_total_cases);
console.log("updatedAt ", result.data.data.update_date_time);
var datasource = result.data.data;
if (latestUpdate !== result.data.data.update_date_time) {
console.log("new update");
var diffdate = new Date(latestUpdate);
var diff2date = new Date(
new Date(result.data.data.update_date_time).toISOString()
);
var d1 = diff2date.toDateString();
var d2 = diffdate.toDateString();
if (d1 === d2) {
console.log("update");
Data.find({})
.sort({ update_date_time: -1 })
.then(doc1 => {
Data.findOneAndUpdate(
{ _id: doc1[0]._id },
{
$set: {
local_recovered: datasource.local_recovered,
local_deaths: datasource.local_deaths,
local_new_deaths: datasource.local_new_deaths,
local_total_number_of_individuals_in_hospitals:
datasource.local_total_number_of_individuals_in_hospitals,
local_active_cases: datasource.local_active_cases,
local_total_cases: datasource.local_total_cases,
local_new_cases: datasource.local_new_cases,
update_date_time: new Date(
datasource.update_date_time
).toISOString(),
hospitals_info: JSON.stringify(datasource.hospital_data)
}
}
)
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
axios
.post(
`https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendMessage?chat_id=XXXXXXXXXXXXX&text=DB%20update%20error%20${JSON.stringify(
err
)}`
)
.then(result => {})
.catch(err => {
console.log(err);
});
});
})
.catch(err => {
console.log(err);
axios
.post(
`https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendMessage?chat_id=XXXXXXXXXXXXX&text=DB%20find%20error%20${JSON.stringify(
err
)}`
)
.then(result => {})
.catch(err => {
console.log(err);
});
});
} else {
console.log("new day");
var newdata = new Data({
local_recovered: datasource.local_recovered,
local_deaths: datasource.local_deaths,
local_new_deaths: datasource.local_new_deaths,
local_total_number_of_individuals_in_hospitals:
datasource.local_total_number_of_individuals_in_hospitals,
local_active_cases: datasource.local_active_cases,
local_total_cases: datasource.local_total_cases,
local_new_cases: datasource.local_new_cases,
update_date_time: new Date(
datasource.update_date_time
).toISOString(),
hospitals_info: JSON.stringify(datasource.hospital_data)
});
newdata
.save()
.then(doc => {
// console.log(doc);
})
.catch(err => {
console.log(err);
axios
.post(
`https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendMessage?chat_id=XXXXXXXXXXXXX4&text=DB%20newdayadd%20error%20${JSON.stringify(
err
)}`
)
.then(result => {})
.catch(err => {
console.log(err);
});
});
}
latestUpdate = result.data.data.update_date_time;
var time = new Date(datasource.update_date_time);
var date = datasource.update_date_time.slice(0, 10);
var hour = ("0" + time.getHours()).slice(-2);
var minutes = ("0" + time.getMinutes()).slice(-2);
var message = encodeURIComponent(
`\n\nLatest Covid-19 🦠 Situation Report Sri lanka 🇱🇰 \n\nUpdate at 🕡 : ${date +
" " +
hour +
":" +
minutes} \n\nNew cases 👨🦽 : ${
datasource.local_new_cases
} \n\nTotal cases 😷 : ${
datasource.local_total_cases
} \n\nActive cases 🛌 : ${
datasource.local_active_cases
} \n\nTotal number of persons in hospitals 🏣 : ${
datasource.local_total_number_of_individuals_in_hospitals
} \n\nDeaths 🏴 : ${datasource.local_deaths} \n\nRecovered ✅ : ${
datasource.local_recovered
}\n\nSource : Health Promotion Bureau Sri Lanka`
);
axios
.post(
`https://api.telegram.org/bot${bot_id}/sendMessage?chat_id=${chat_id}&text=${message}`
)
.then(result => {
console.log(result.data.ok);
})
.catch(err => {
console.log(err);
});
} else {
console.log("no updates");
}
} else {
axios
.post(
`https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendMessage?chat_id=XXXXXXXXXXXXXXXXXXXX&text=covid%20api%20error%20${result.data.message}`
)
.then(result => {})
.catch(err => {
console.log(err);
});
}
// console.log(result);
})
.catch(err => {
console.log(err);
});
}