Add Hermes UK tracking (#906)

This commit is contained in:
Henry Wang 2018-10-17 04:07:13 +01:00 committed by DIYgod
parent 142889d144
commit bbb14b9509
3 changed files with 60 additions and 1 deletions

View File

@ -312,6 +312,12 @@ GitHub provides some official RSS feeds:
</routeEn>
## Parcel Tracking
### Hermes
<routeEn name="Hermes UK" author="HenryQW" example="/parcel/hermesuk/[tracking number]" path="/parcel/hermesuk/:tracking" :paramsDesc="['Tracking number']"/>
## Uncategorized
### EZTV
@ -366,4 +372,4 @@ Provides a better reading experience (full text articles) over the official one.
### 99% Invisible
<route name="Transcript" author="Ji4n1ng" example="/99percentinvisible/transcript" path="/99percentinvisible/transcript"/>
<routeEn name="Transcript" author="Ji4n1ng" example="/99percentinvisible/transcript" path="/99percentinvisible/transcript"/>

View File

@ -654,4 +654,7 @@ router.get('/laosiji/feed', require('./routes/laosiji/feed'));
// 99% Invisible
router.get('/99percentinvisible/transcript', require('./routes/99percentinvisible/transcript'));
// Hermes UK
router.get('/parcel/hermesuk/:tracking', require('./routes/parcel/hermesuk'));
module.exports = router;

50
routes/parcel/hermesuk.js Normal file
View File

@ -0,0 +1,50 @@
const axios = require('../../utils/axios');
const dayjs = require('dayjs');
module.exports = async (ctx) => {
const tracking = ctx.params.tracking;
let single;
const cache = await ctx.cache.get(tracking);
if (cache) {
single = JSON.parse(cache);
} else {
const response = await axios({
method: 'get',
url: `https://www.myhermes.co.uk/ajax/ref/trackParcelByBarcode?barcode=${tracking}`,
});
let parcel = response.data;
let message = '';
if (parcel.length > 0) {
parcel = parcel[0];
if (parcel.point) {
message += `${parcel.point.description}`;
}
if (parcel.location) {
message += ` at ${parcel.location.description}`;
}
if (parcel.estimatedDeliveryDate) {
message += ` ETA: ${dayjs(parcel.estimatedDeliveryDate).format('MMM DD')}`;
}
single = {
pubDate: new Date(parcel.dateTime).toISOString(),
title: message,
};
}
await ctx.cache.set(tracking, JSON.stringify(single), 1 * 60 * 60);
}
ctx.state.data = {
title: `Hermes Tracking ${tracking}`,
link: `https://www.myhermes.co.uk/tracking-results.html?trackingNumber=${tracking}`,
item: [single],
};
};