Skip to content

Commit

Permalink
한진, 대신택배 API 오류 수정 (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
falsy authored Oct 11, 2021
1 parent 17c5c99 commit 8545ff2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
28 changes: 23 additions & 5 deletions packages/apiserver/carriers/kr.daesin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { JSDOM } = require('jsdom');
const qs = require('querystring');

const iconv = new Iconv('EUC-KR', 'UTF-8//TRANSLIT//IGNORE');
const outForDeliveryLocationTypePattern = //;

function getTrack(trackId) {
const trimString = s => {
Expand Down Expand Up @@ -58,23 +59,40 @@ function getTrack(trackId) {
progresses.forEach(element => {
const tds = element.querySelectorAll('td');

const location = trimString(tds[1].textContent);
const contact = trimString(tds[2].textContent);

if (!tds[3].textContent) return;

shippingInformation.progresses.push({
time: `${tds[3].textContent.replace(' ', 'T')}:00+09:00`,
location: {
name: trimString(tds[1].textContent),
name: location,
},
description: `연락처: ${trimString(tds[2].textContent)}`,
description: `연락처: ${contact}`,
status: { id: 'in_transit', text: '배송중' },
});

const locationType = trimString(tds[0].textContent);

if (
locationType.match(outForDeliveryLocationTypePattern) &&
contact
) {
shippingInformation.courier = {
name: `${location} ${locationType}`,
contact,
};
}

if (!tds[4].textContent) return;

shippingInformation.progresses.push({
time: `${tds[4].textContent.replace(' ', 'T')}:00+09:00`,
location: {
name: trimString(tds[1].textContent),
name: location,
},
description: `연락처: ${trimString(tds[2].textContent)}`,
description: `연락처: ${contact}`,
status:
tds[5].textContent.indexOf('배송완료') !== -1
? { id: 'delivered', text: '배송완료' }
Expand Down Expand Up @@ -104,4 +122,4 @@ module.exports = {
tel: '+82314620100',
},
getTrack,
};
};
75 changes: 30 additions & 45 deletions packages/apiserver/carriers/kr.hanjin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const axios = require('axios');
const { JSDOM } = require('jsdom');
const qs = require('querystring');
const { Iconv } = require('iconv');
const iconv = new Iconv('euc-kr', 'utf-8');

function parseStatus(s) {
if (s.includes('집하')) return { id: 'at_pickup', text: '상품인수' };
Expand Down Expand Up @@ -31,29 +32,27 @@ function getTrack(trackId) {
}

axios
.post(
'https://m.hanex.hanjin.co.kr/inquiry/incoming/resultWaybill',
qs.stringify({
div: 'B',
show: 'true',
wblNum: trackId,
})
)
.get('http://www.hanjinexpress.hanjin.net/customer/hddcw18.tracking', {
params: {
w_num: trackId,
},
responseType: 'arraybuffer'
})
.then(res => {
const dom = new JSDOM(res.data);
const dom = new JSDOM(iconv.convert(res.data).toString());
const tables = dom.window.document.querySelectorAll('table');

if (tables.length === 0) {
return reject({
code: 404,
message: dom.window.document.querySelector('.noData').textContent,
});
}

return { informationTable: tables[0], progressTable: tables[1] };
})
.then(({ informationTable, progressTable }) => {
const td = informationTable.querySelectorAll('td');

const td = informationTable.querySelectorAll('td');
const shippingInformation = {
from: {
name: td[1].textContent,
Expand All @@ -70,45 +69,31 @@ function getTrack(trackId) {
progresses: [],
};

progressTable.querySelectorAll('tr').forEach(element => {
const insideTd = element.querySelectorAll('th, td');
// TODO : time 년도 처리 나중에 수정 해야 함 (현재 시간하고 마지막 시간하고 비교해서 마지막 시간이 미래면 작년 껄로 처리)
const curTime = new Date();
let time = `${curTime.getFullYear()}-${insideTd[0].innerHTML
.replace('<br>', 'T')
.replace(/<[^>]*>/gi, '')
.replace(/\./gi, '-')}:00+09:00`;

if (new Date(time) > curTime) {
time = `${curTime.getFullYear() - 1}${time.substring(4)}`;
}
const { progresses } = shippingInformation;

shippingInformation.progresses.unshift({
time,
progressTable.querySelector('tbody').querySelectorAll('tr').forEach(element => {
const insideTd = element.querySelectorAll('td');
const date = insideTd[0].textContent; // insideTd[0] - 날짜 (ex. 2021-04-13)
const time = insideTd[1].textContent; // insideTd[1] - 시간 (ex. 10:37)
const address = insideTd[2].textContent; // insideTd[2] - 위치
const description = insideTd[3].textContent.trim();// insideTd[3] - 설명
const timeSet = `${date}T${time}:00+09:00`;

progresses.unshift({
time: timeSet,
location: {
name: insideTd[1].textContent,
name: address,
},
status: parseStatus(insideTd[2].textContent),
description: insideTd[2].textContent,
status: parseStatus(description),
description: description,
});
});

if (shippingInformation.progresses.length > 0) {
shippingInformation.state =
shippingInformation.progresses[
shippingInformation.progresses.length - 1
].status;
shippingInformation.from.time =
shippingInformation.progresses[0].time;
if (
shippingInformation.progresses[
shippingInformation.progresses.length - 1
].status.id === 'delivered'
)
shippingInformation.to.time =
shippingInformation.progresses[
shippingInformation.progresses.length - 1
].time;
if (progresses.length > 0) {
shippingInformation.state = progresses[0].status;
shippingInformation.from.time = progresses[progresses.length - 1].time;
if (progresses[0].status.id === 'delivered')
shippingInformation.to.time = progresses[0].time;
} else {
shippingInformation.state = {
id: 'information_received',
Expand Down

0 comments on commit 8545ff2

Please sign in to comment.