-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairasia.js
54 lines (47 loc) · 3.19 KB
/
airasia.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
const cheerio = require('cheerio');
const mail = require('./mail');
const parse = (html, mailgun, input) => {
const $ = cheerio.load(html);
const leaveFair = $('#availabilityForm > div:nth-child(1) > table > tbody > tr.fare-light-row > td.avail-table-top-border-black.avail-fare.depart.LF > div > div > div:nth-child(2) > div > div > div.avail-fare-price-wrapper > div').text();
const leaveDepartedTime = $('#availabilityForm > div:nth-child(1) > table > tbody > tr.fare-light-row > td.avail-table-vert.avail-fare-td.avail-table-top-border-black > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(2) > div > div.avail-table-bold').text();
const leaveArrivalTime = $('#availabilityForm > div:nth-child(1) > table > tbody > tr.fare-light-row > td.avail-table-vert.avail-fare-td.avail-table-top-border-black > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(4) > div > div.avail-table-bold').text();
const returnFair = $('#availabilityForm > div:nth-child(2) > table > tbody > tr.fare-light-row > td.avail-table-top-border-black.avail-fare.return.LF > div > div > div:nth-child(2) > div > div > div.avail-fare-price-wrapper > div').text();
const returnDepartedTime = $('#availabilityForm > div:nth-child(2) > table > tbody > tr.fare-light-row > td.avail-table-vert.avail-fare-td.avail-table-top-border-black > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(2) > div > div.avail-table-bold').text();
const returnArrivalTime = $('#availabilityForm > div:nth-child(2) > table > tbody > tr.fare-light-row > td.avail-table-vert.avail-fare-td.avail-table-top-border-black > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(4) > div > div.avail-table-bold').text();
input.sender = 'Airport Scraper';
input.target = 'AirAsia';
input.fromDate = `${input.fromDate} ${leaveDepartedTime}-${leaveArrivalTime}`;
input.toDate = `${input.toDate} ${returnDepartedTime}-${returnArrivalTime}`;
mail.sendReport(mailgun, leaveFair, returnFair, input);
}
const scrap = (nightmare, mailgun, input) => {
const url = 'http://www.airasia.com/id/en/home.page';
nightmare.goto(url)
.wait('#fromInput')
.type('#fromInput', input.fromLocation).wait(1000)
.type('#fromInput', '\t').wait(1000)
.type('#toInput', input.toLocation).wait(1000)
.type('#toInput', '\t').wait(1000)
.type('#search_from_date', input.fromDate).wait(1000)
.type('#search_from_date', '\t').wait(1000)
.type('#search_to_date', input.toDate).wait(1000)
.type('#search_to_date', '\t').wait(1000)
.type('#adtPaxCount', input.passenger).wait(1000)
.type('#adtPaxCount', '\t').wait(1000)
.click('#calFair2').wait(1000)
.click('#searchButton')
.wait('#undefined-sticky-wrapper > div > div.price-display-header > h4')
.evaluate(() => {
return document.documentElement.innerHTML;
})
.then((html) => {
parse(html, mailgun, input);
})
.catch(function (error) {
console.error('ERROR: ', error);
});
nightmare.end();
}
module.exports = {
scrap: scrap
};