forked from ChakshuGautam/KALIA-Wrapper-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (153 loc) · 5.39 KB
/
index.js
File metadata and controls
181 lines (153 loc) · 5.39 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const browserManager = require("./browserManager");
const cheerio = require("cheerio");
const app = express();
const port = 3000;
let browser = null;
app.use(bodyParser.json());
app.get("/:id", async (req, res) => {
const userInput = req.params.id;
try {
if(!browser) {
browser = await browserManager.launchBrowser();
}
const page = await browser.newPage();
await page.goto("https://kaliaportal.odisha.gov.in/TrackToken.aspx");
await page.type("#txttokenNo", userInput);
await page.click("#Btn_Show");
await Promise.race([
page.waitForSelector("#viewpart"),
// if aadhaar Number is invalid
page.waitForFunction(() => {
const scripts = document.querySelectorAll('script');
for(const script of scripts) {
if(script.innerText.includes("jAlert")) {
return true;
}
}
return false;
})
]);
const viewpart = await page.$('#viewpart');
if(viewpart) {
const result = await page.evaluate(
() => document.querySelector("#viewpart").innerHTML
);
const htmlContent = result;
const $ = cheerio.load(htmlContent);
let extractedData = {
personalDetails: {},
onlineGrievanceApplicationStatus: "",
eligibilityStatus: {},
paymentAccountDetails: [],
reasonsOfIneligibility: [],
};
$("table")
.eq(0)
.find("tr")
.each((i, el) => {
if (i > 0) {
const key = $(el).find("td").eq(0).text().trim();
const value = $(el).find("td").eq(1).find("span").text().trim();
if (key && value) {
extractedData.personalDetails[key] = value;
}
const key2 = $(el).find("td").eq(2).text().trim();
const value2 = $(el).find("td").eq(3).find("span").text().trim();
if (key2 && value2) {
extractedData.personalDetails[key2] = value2;
}
}
});
const grievanceStatus = {}
const grievanceStatusTableRows = $("table").eq(1).find("tr").slice(1);
if(grievanceStatusTableRows.length > 1) {
grievanceStatusTableRows.each((idx, row) => {
const grievanceStatusKey1 = $(row).find("td").eq(0).text().trim();
const grievanceStatusValue1 = $(row).find("td").eq(1).find("span").text().trim();
grievanceStatus[grievanceStatusKey1] = grievanceStatusValue1
if(idx < 3) {
const grievanceStatusKey2 = $(row).find("td").eq(2).text().trim();
const grievanceStatusValue2 = $(row).find("td").eq(3).find("span").text().trim();
grievanceStatus[grievanceStatusKey2] = grievanceStatusValue2
}
})
} else {
grievanceStatus["message"] = "No data found"
}
extractedData.onlineGrievanceApplicationStatus = grievanceStatus;
$("table")
.eq(2)
.find("tr")
.each((i, el) => {
if (i === 1) {
const key = $(el).find("td").eq(0).text().trim();
const value = $(el).find("td").eq(1).find("span").text().trim();
if (key && value) {
extractedData.eligibilityStatus[key] = value;
}
}
});
$("#grdineligible tr").each((i, el) => {
if (i > 0) {
const reason = $(el).find("td").eq(1).text().trim();
if (reason) {
extractedData.reasonsOfIneligibility.push(reason);
}
}
});
const paymentDetailsTables = $("table").slice(4);
const paymentDetails = []
paymentDetailsTables.each((idx, table) => {
const tableData = {}
$(table).find("tr").each((rowIdx, tableRow) => {
// skip header row of every paymentInstallment table
if(rowIdx == 0) {
return;
}
// Each paymentInstallment row has two key-value pairs describing the current installment data
const paymentInstallmentKey1 = $(tableRow).find("td").eq(0).text().trim();
const paymentInstallmentValue1 = $(tableRow).find("td").eq(1).find("span").text().trim()
const paymentInstallmentKey2 = $(tableRow).find("td").eq(2).text().trim();
const paymentInstallmentValue2 = $(tableRow).find("td").eq(3).find("span").text().trim()
if(paymentInstallmentKey1 && paymentInstallmentValue1) {
tableData[paymentInstallmentKey1] = paymentInstallmentValue1;
}
if(paymentInstallmentKey2 && paymentInstallmentValue2) {
tableData[paymentInstallmentKey2] = paymentInstallmentValue2;
}
})
paymentDetails.push(tableData);
})
extractedData.paymentAccountDetails = paymentDetails
console.log(JSON.stringify(extractedData, null, 2));
res.json(extractedData);
} else {
res.json({
message: "Invalid aadhaar number"
})
}
await page.close();
} catch (error) {
console.log(error);
}
});
const closeBrowser = async () => {
if(browser) {
await browser.close();
browser = null;
}
}
app.on('close', closeBrowser)
process.on('SIGINT', async () => {
await closeBrowser();
process.exit();
})
process.on('SIGTERM', async () => {
await closeBrowser();
process.exit();
})
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});