diff --git a/docs/other.md b/docs/other.md index efba9a15fe..54d50b8786 100644 --- a/docs/other.md +++ b/docs/other.md @@ -74,6 +74,12 @@ pageClass: routes +## DHL + +### DHL 国际快递包裹追踪 + + + ## Dilbert Comic Strip diff --git a/lib/router.js b/lib/router.js index 1377c35aa6..a03a49e770 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1597,6 +1597,9 @@ router.get('/aliyun/database_month', require('./routes/aliyun/database_month')); // 礼物说 router.get('/liwushuo/index', require('./routes/liwushuo/index.js')); +// DHL +router.get('/dhl/:id', require('./routes/dhl/shipment-tracking')); + // 中华人民共和国商务部 router.get('/mofcom/article/:suffix', require('./routes/mofcom/article')); diff --git a/lib/routes/dhl/shipment-tracking.js b/lib/routes/dhl/shipment-tracking.js new file mode 100644 index 0000000000..128a7945f0 --- /dev/null +++ b/lib/routes/dhl/shipment-tracking.js @@ -0,0 +1,55 @@ +const got = require('@/utils/got'); + +const monthMap = { + 一月: 0, + 二月: 1, + 三月: 2, + 四月: 3, + 五月: 4, + 六月: 5, + 七月: 6, + 八月: 7, + 九月: 8, + 十月: 9, + 十一月: 10, + 十二月: 11, +}; + +module.exports = async (ctx) => { + const id = ctx.params && ctx.params.id; + + const response = await got({ + method: 'get', + url: `https://www.logistics.dhl/shipmentTracking?AWB=${id}&countryCode=CN&languageCode=zh`, + headers: { + Accept: 'application/json', + }, + }); + const data = response.data; + const result = data.results[0]; + + const link = `https://www.logistics.dhl/cn-zh/home/tracking/tracking-express.html?tracking-id=${id}`; + + ctx.state.data = { + title: `Shipment tracking for DHL (${id})`, + link, + item: result.checkpoints.map((checkpoint) => { + const [, month, day, year] = checkpoint.date.match(/^[^,]+, ([^ ]+) (\d+), (\d+)/i); + const [, hour, minute] = checkpoint.time.match(/(\d+):(\d+)/); + const date = new Date(); + + date.setFullYear(year); + date.setMonth(monthMap[month]); + date.setDate(day); + date.setHours(hour); + date.setMinutes(minute); + date.setSeconds(0); + date.setMilliseconds(0); + + return { + title: checkpoint.description, + pubDate: date.toUTCString(), + }; + }), + }; +};