-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataPrepare.js
107 lines (94 loc) · 3.56 KB
/
dataPrepare.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
const csvparsed = require('./csvparser');
const axios = require('axios')
const UserAgent = require('user-agents'); // DOCS: https://www.npmjs.com/package/user-agents
const masterRecord = {};
const center_names = [];
const apiFetch = async() => {
try {
let config = {
method: 'get',
headers: {
'accept': 'application/json',
'Accept-Language': 'hi_IN',
"cache-control": "max-age=0",
}
}
// UPDATE: The new CoWin API requires a User-Agent parameter to be passed everytime.
// Generating random user agents
const obj = new Date();
let MyDateString;
obj.setDate(obj.getDate());
MyDateString = ('0' + obj.getDate()).slice(-2) + '-' + ('0' + (obj.getMonth() + 1)).slice(-2) + '-' + obj.getFullYear();
console.log('[API HIT]')
console.log(`[ DATE API ] : ${MyDateString}`)
for (const pin of csvparsed) {
const userAgent = new UserAgent(); // Generating user agents for mobile devices only to prevent blocking
// const userAgent = new UserAgent({deviceCategory:'mobile'}); // Generating user agents for mobile devices only to prevent blocking
config.headers['User-Agent'] = userAgent.data.userAgent;
const apiEndpoint = `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${pin}&date=${MyDateString}`
const {
data
} = await axios.get(apiEndpoint, config);
// console.log(data)
const dataHolder = [];
for (const [index, {
name,
center_id,
fee_type,
sessions
}] of data.centers.entries()) {
const info = [];
// console.log(sessions)
for (const {
available_capacity,
available_capacity_dose1,
available_capacity_dose2,
date,
vaccine,
min_age_limit
}
of sessions) {
if (available_capacity > 0) {
info.push({
name,
date,
min_age: min_age_limit,
vaccine,
available_capacity,
available_capacity_dose1,
available_capacity_dose2
});
// masterRecord.push({name, info: {date, available_capacity}});
}
// console.log(`AVAILABLE `, available_capacity, ' ON ', date)
}
if (info.length > 0) {
dataHolder.push({
name,
fee_type,
info
})
center_names.push(name.split(' ').slice(0, 2).join(' '))
}
}
if (dataHolder.length > 0)
masterRecord[pin] = dataHolder
}
// console.log(masterRecord)
return masterRecord;
} catch (e) {
console.error(e)
return {};
}
}
const getCenterNames = () => {
const items = new Set(); // Set to avoid duplicate entries
for (const area of center_names) {
items.add(area);
}
return [...items];
};
module.exports = {
apiFetch,
getCenterNames
};