-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (135 loc) · 4.91 KB
/
app.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
//jshint esversion: 6
let queryState = "";
let queryDate = "";
let queryRes = [];
let distInputVaccine = "";
var vaccineTweakedDay = "";
let vaccineUrl = "";
let vaccineCenterLists = [];
const https = require("https");
const fs = require("fs");
const csv = require("csv-parser");
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.set("view engine", "ejs");
var getJSON = require("get-json");
const fetch = require("node-fetch");
var interval = 43200000;
const report = require(__dirname + "/report.js");
report.getReport();
setInterval(function(){
report.getReport();
console.log("Updated after " + interval + " seconds");
}, 43200000);
let currDate = new Date();
let formatedTday = (currDate.toLocaleDateString("en-GB"));
const yesterday = new Date(currDate);
yesterday.setDate(yesterday.getDate() - 1);
const formatedYday = yesterday.toLocaleDateString("en-GB");
app.get("/", function(req, res){
res.render("index");
});
app.post("/", function(req, res){
queryRes = [];
queryState = req.body.stateInput;
queryDate = req.body.dateInput;
let year = new Date(queryDate);
var tweakedDay = new Date(queryDate);
queryDate = tweakedDay.toLocaleDateString("en-GB");
// console.log(queryState, queryDate, year.getFullYear(), currDate.getFullYear());
fs.createReadStream(__dirname + "/public/data/vaccine_report.csv")
.pipe(csv())
.on("data", function(row){
var currResJSONString = JSON.stringify((row));
var currResJSON = JSON.parse(currResJSONString);
if(currResJSON["State"] == queryState && currResJSON["Updated On"] == queryDate){
let totalAdministered = parseInt(currResJSON["First Dose Administered"]) + parseInt(currResJSON["Second Dose Administered"]);
queryRes.push(currResJSON["State"], currResJSON["Updated On"],
totalAdministered.toString(), currResJSON["Total Individuals Vaccinated"]);
// console.log(queryRes);
}
});
fs.createReadStream(__dirname + "/public/data/state_report.csv")
.pipe(csv())
.on("data", function(row){
//console.log(typeof(row));
var currResJSONString = JSON.stringify((row));
//console.log(typeof(currResJSONString));
var currResJSON = JSON.parse(currResJSONString);
var tweakedDay = new Date(currResJSON["Date"]);
tweakedDay = tweakedDay.toLocaleDateString("en-GB");
if(currResJSON["State"] == queryState && tweakedDay == queryDate){
queryRes.push(currResJSON["Confirmed"],
currResJSON["Recovered"], currResJSON["Deceased"]);
// console.log(queryRes);
res.redirect("/state");
}
});
});
app.get("/state", function(req, res){
res.render("state", {queryRes: queryRes});
});
app.post("/state", function(req, res){
res.redirect("/");
});
app.get("/vaccinations", function(req, res){
res.render("vaccinations");
});
app.post("/vaccinations", function(req, res){
// console.log(req.body);
distInputVaccine = req.body.distInputVaccine;
vaccineTweakedDay = new Date(req.body.dateInput);
let dd = vaccineTweakedDay.getDate();
let mm = vaccineTweakedDay.getMonth() + 1;
const yyyy = vaccineTweakedDay.getFullYear();
if(dd<10)
{
dd=`0${dd}`;
}
if(mm<10)
{
mm=`0${mm}`;
}
vaccineTweakedDay = `${dd}-${mm}-${yyyy}`;
// console.log("__________");
vaccineUrl = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id=" + distInputVaccine + "&date=" + vaccineTweakedDay;
console.log("Retriving data from: " + vaccineUrl);
res.redirect("/vaccinations-result");
});
app.get("/vaccinations-result", function(req, res){
vaccineCenterLists = [];
getJSON(vaccineUrl)
.then(function(responseFromUrl) {
// console.log(responseFromUrl.sessions.length);
// console.log(responseFromUrl);
if(responseFromUrl.sessions.length == 0){
console.log("No vaccine details available");
}
else{
console.log("Getting vaccine details");
for(let i = 0; i < responseFromUrl.sessions.length; i++){
vaccineCenterLists.push(
responseFromUrl.sessions[i].name + ", " +
responseFromUrl.sessions[i].address + ", " +
responseFromUrl.sessions[i].district_name + ", " +
responseFromUrl.sessions[i].state_name + " || " +
responseFromUrl.sessions[i].min_age_limit + " || " +
responseFromUrl.sessions[i].available_capacity + " || " +
responseFromUrl.sessions[i].slots
);
// console.log(response.sessions[i]);
}
}
// console.log(vaccineCenterLists.sort());
res.render("vaccinationsResult", {vaccineCenterLists: vaccineCenterLists});
}).catch(function(error) {
console.log("Error while fetching JSON from API: " + error);
});
});
//start server
app.listen(process.env.PORT || 3000, function(){
console.log("Main server is running...");
});