Created
August 29, 2022 13:39
-
-
Save tuhuynh27/7730dfa983c8fee10762eac669a203c7 to your computer and use it in GitHub Desktop.
Traveloka Flights Crawl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extractPrice(priceStr) { | |
const priceOne = priceStr.slice(0, -2) | |
const priceTwo = priceStr.slice(-2) | |
return parseFloat(priceOne + '.' + priceTwo) | |
} | |
function extractDayMonthYearFromString(str) { | |
const [day, month, year] = str.split('-') | |
return { day, month, year } | |
} | |
async function getFlights(time = '17-9-2022', from = 'SIN', to = 'SGN') { | |
const { day, month, year } = extractDayMonthYearFromString(time) | |
const response = await fetch("https://www.traveloka.com/api/v2/flight/search/oneway", { | |
"headers": { | |
"accept": "application/json", | |
"accept-language": "en-US,en;q=0.9,vi;q=0.8", | |
"content-type": "application/json", | |
"sec-ch-ua": "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"", | |
"sec-ch-ua-mobile": "?0", | |
"sec-ch-ua-platform": "\"macOS\"", | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-origin", | |
"x-domain": "flight", | |
"x-nonce": "ba08a6f9-5cac-4454-be90-fc41f48a385b", | |
"x-route-prefix": "en-sg", | |
"cookie": "G_ENABLED_IDPS=google; tripCheckInDate=26-05-2022; tripNumOfGuests=1; tripNumOfNights=9; tripNumOfRooms=1; tripSelectedLocation=%7B%22id%22:null,%22name%22:%22Th%C3%A0nh%20ph%E1%BB%91%20%C4%91%E1%BA%BFn%20c%E1%BB%A7a%20b%E1%BA%A1n%22,%22type%22:%22HOTEL_GEO%22%7D; g_state={\"i_l\":4,\"i_p\":1662299701759}; state-auth=auth-90010a29-5d90-4b43-924b-1a1332afbeba; tv-repeat-visit=true; tvs=qgdHX7GvehrD9XH5a3S4PWL3Nd74xArIuT+JzcRMbKddQHovERAJ9HWRLrAaZ0jPhWj5HSxm0ZKiRbldET1ham2PeYg1sQr2h/wIBjIyPQ1JQfOnq9PrXiJXCb7pG+GuPPDRiK6O8K2+QmwYhi/LAi0mJ6tOazJZ/UnOF0YdKS2rqamdl+R0wLdgBCXA7PtF3jW7f6f85zK7XA1xLrLbn3wpMY91AYFzJ6h8za/vSrng40uUoDT+qJIv0oQGNB1A; flightFlightType=ONE_WAY; flightDepartureDate=18-9-2022; flightSourceAirport=SIN; flightSourceLocation=Singapore; flightDestinationAirport=SGN; flightDestinationLocation=Ho%2520Chi%2520Minh%2520City; flightNumOfAdults=1; flightNumChildren=0; flightNumInfants=0; flightSeatClassType=ECONOMY; flexibility=0; tvl=qgdHX7GvehrD9XH5a3S4PdE8AYpuF3hYPaT5bxhY7ZbergIdT5nCaXx5UaC+vEvXKFT70jxwVsH4bSYD5kAh9DIi8FLAfEtCEwoLzeiRulFUJ1yeSV74kYGlduXG0lE7T58GCU/rC+QGBAj39YgtOFh+8QqhoHh7qJKXZBn1FjDvlyHfFnPptZUxAgMVwRNSCMYWUJplNNMY2P4/83O9X+8GNrPf8Ng75ZieUaJama8=", | |
"Referer": "https://www.traveloka.com/en-sg/flight/fullsearch?ap=SIN.SGN&dt=18-09-2022.NA&ps=1.0.0&sc=ECONOMY", | |
"Referrer-Policy": "strict-origin-when-cross-origin" | |
}, | |
"body": `{"clientInterface":"desktop","data":{"currency":"SGD","isReschedule":false,"locale":"en_SG","numSeats":{"numAdults":1,"numChildren":0,"numInfants":0},"seatPublishedClass":"ECONOMY","destinationAirportOrArea":"${to}","flexibleTicket":false,"flightDate":{"year":${year},"month":${month},"day":"${day}"},"sourceAirportOrArea":"${from}","newResult":true,"seqNo":null,"searchId":"b32eedaf-1ee7-422b-a93a-3ff73434df09","visitId":"32d87bae-a1f7-41ca-b501-8edb276e15be","utmId":null,"utmSource":null,"funnelId":"null","funnelSource":"null","searchSpecRoutesTotal":1},"fields":[]}`, | |
"method": "POST" | |
}); | |
const rObj = await response.json() | |
const obj = rObj.data | |
const r = obj.searchResults | |
const f = r | |
const d = f.map(e => { | |
return { | |
airlineId: e.flightMetadata[0].airlineId, | |
start: e.connectingFlightRoutes[0].segments[0].departureTime.hour + 'h' + e.connectingFlightRoutes[0].segments[0].departureTime.minute, | |
end: e.connectingFlightRoutes[0].segments[0].arrivalTime.hour + 'h' + e.connectingFlightRoutes[0].segments[0].arrivalTime.minute, | |
price: extractPrice(e.mAppsPrice.amount) | |
} | |
}) | |
d.sort((a, b) => a.price - b.price) | |
return d.filter((v,i,a)=>a.findIndex(v2=>['airlineId','start'].every(k=>v2[k] ===v[k]))===i).slice(0,10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment