-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
121 lines (78 loc) · 2.75 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
const port = 8080;
const express = require("express");
const app = express();
const puppeteer = require("puppeteer");
const url = "https://www.tcsexpress.com/tracking";
app.set("view engine","ejs");
app.use(express.static("public"));
app.get("/",(req,res)=>{
res.render("home");
});
app.post('/track',async(req,res)=>{
let {num} = req.query;
console.log(`request recieved for tracking : ${num}`);
try{
let browser = await puppeteer.launch({headless : true});
let page = await browser.newPage();
await page.goto(url , {waitUntil : 'networkidle0' , timeout : 0});
await page.type("#track_no",num);
await page.click("#btnload")
await page.waitForSelector(".shipment-detail");
let shipment = await page.$$eval(".shipment-detail",(elem)=>{
let s_temp = [];
for(let i=0 ; i<elem.length ; i++){
let shipments = elem[i].getElementsByTagName("p");
let temp = [];
for(let d of shipments){
temp.push(d.innerText)
}
s_temp.push([...temp])
}
return s_temp;
})
let summary = await page.$$eval(".shipment-tracking-summary",(elem)=>{
let summary_temp = []
for(let i=0 ; i<elem.length ; i++){
let t_summary = elem[i].getElementsByTagName("p");
let temp = [];
for(let s of t_summary){
temp.push(s.innerText);
}
summary_temp.push([...temp]);
}
return summary_temp;
})
let history = await page.$$eval(".track-history",(elem)=>{
let t_temp = [];
for(let i=0 ; i<elem.length ; i++){
let t_history = elem[i].getElementsByTagName("tr");
let temp = [];
for(let j= 1 ; j<t_history.length ; j++){
let td = t_history[j].getElementsByTagName("td");
let td_temp = []
for(let t of td){
td_temp.push(t.innerText);
}
temp.push(td_temp);
}
t_temp.push([...temp])
}
return t_temp;
})
let details = {
shipment ,
summary ,
history
}
console.log(details);
await browser.close();
res.status(200).json(details);
}
catch(err){
res.status(500).end();
console.log(err);
}
});
app.listen(port,()=>{
console.log(`app is live at port : ${port}`)
});