Skip to content

Commit

Permalink
kr.chunilps - Add Carrier (#32)
Browse files Browse the repository at this point in the history
Refs #22
  • Loading branch information
shlee322 committed Jul 19, 2019
1 parent 18e4b85 commit 07bfb09
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions apiserver/carriers/kr.chunilps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const axios = require('axios')
const { JSDOM } = require('jsdom')

const STATUS_ID_MAP = {
'μ ‘μˆ˜': 'information_received',
'λ°œμ†‘': 'at_pickup',
'λ°°μ†‘μ™„λ£Œ': 'delivered'
}

function getTrack(trackId) {
const trimString = (s) => {
return s.replace(/([\n\t]{1,}|\s{2,})/g, ' ').trim()
}

return new Promise((resolve, reject) => {
axios.get('http://www.chunil.co.kr/HTrace/HTrace.jsp', {
params: {
transNo: trackId
}
}).then(res => {
const dom = new JSDOM(res.data)
const document = dom.window.document

const tables = document.querySelectorAll('table[cellspacing="1"]')

if ( tables.length === 0 ) {
return reject({
code: 404,
message: 'μš΄μ†‘μž₯이 λ“±λ‘λ˜μ§€ μ•Šμ•˜κ±°λ‚˜ μ—…μ²΄μ—μ„œ μƒν’ˆμ„ μ€€λΉ„μ€‘μ΄λ‹ˆ μ—…μ²΄λ‘œ λ¬Έμ˜ν•΄μ£Όμ‹œκΈ° λ°”λžλ‹ˆλ‹€.'
})
}

return {
from: tables[0].querySelectorAll('td:nth-child(2n)'),
to: tables[1].querySelectorAll('td:nth-child(2n)'),
item: tables[2].querySelectorAll('td:nth-child(2n)'),
progresses: tables[4].querySelectorAll('tr:not(:first-child)')
}
}).then(({ from, to, item, progresses }) => {

let shippingInformation = {
from: {
name: from[0].textContent,
address: trimString(from[1].textContent)
},
to: {
name: to[0].textContent,
address: trimString(to[1].textContent)
},
state: { id: 'information_received', text:'μ ‘μˆ˜' },
item: item[0].textContent,
progresses: [],
}

progresses.forEach(element => {
const tds = element.querySelectorAll('td')

shippingInformation.progresses.push({
time: `${tds[0].textContent}T00:00:00+09:00`,
location: { name: tds[1].textContent },
description: `μ—°λ½μ²˜: ${tds[2].textContent}`,
status: { id: STATUS_ID_MAP[tds[3].textContent] || 'in_transit', text: tds[3].textContent }
})
})

const lastProgress = shippingInformation.progresses[shippingInformation.progresses.length - 1]
if(lastProgress) {
shippingInformation.state = lastProgress.status
if(lastProgress.status.id === 'delivered') shippingInformation.to.time = lastProgress.time
}

resolve(shippingInformation)
}).catch(err => reject(err))
})
}

module.exports = {
info: {
name: 'μ²œμΌνƒλ°°',
tel: '+8218776606'
},
getTrack: getTrack
}

0 comments on commit 07bfb09

Please sign in to comment.