mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Get Expected Delivery Date in USPS Tracking
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const request = require('node-superfetch');
|
||||
const { stripIndents } = require('common-tags');
|
||||
const { homepage } = require('../../package');
|
||||
const { USPS_USERID } = process.env;
|
||||
|
||||
module.exports = class USPSTrackingCommand extends Command {
|
||||
@@ -33,11 +34,13 @@ module.exports = class USPSTrackingCommand extends Command {
|
||||
|
||||
async run(msg, { id }) {
|
||||
try {
|
||||
const summary = await this.fetchSummary(id);
|
||||
const { expected, summary } = await this.fetchStatus(id);
|
||||
if (!summary) return msg.say('A status update is not yet available on your package. Check back soon.');
|
||||
return msg.say(stripIndents`
|
||||
**Tracking info for ${id}:**
|
||||
${summary}
|
||||
|
||||
Expected Delivery by: ${expected || 'N/A'}
|
||||
More Info: <https://tools.usps.com/go/TrackConfirmAction?tLabels=${id}>
|
||||
`);
|
||||
} catch (err) {
|
||||
@@ -45,16 +48,28 @@ module.exports = class USPSTrackingCommand extends Command {
|
||||
}
|
||||
}
|
||||
|
||||
async fetchSummary(id) {
|
||||
async fetchStatus(id) {
|
||||
const { text } = await request
|
||||
.get('https://secure.shippingapis.com/ShippingApi.dll')
|
||||
.query({
|
||||
API: 'TrackV2',
|
||||
XML: `<TrackRequest USERID="${USPS_USERID}"><TrackID ID="${id}"></TrackID></TrackRequest>`
|
||||
XML: stripIndents`
|
||||
<TrackFieldRequest USERID="${USPS_USERID}">
|
||||
<Revision>1</Revision>
|
||||
<ClientIp>127.0.0.1</ClientIp>
|
||||
<SourceId>${homepage}</SourceId>
|
||||
<TrackID ID="${id}">
|
||||
</TrackID>
|
||||
</TrackFieldRequest>
|
||||
`
|
||||
});
|
||||
if (text.includes('<Number>-2147219283</Number>')) return null;
|
||||
if (text.includes('<Error>')) throw new Error(text.match(/<Description>(.+)<\/Description>/i)[1].trim());
|
||||
const summary = text.match(/<TrackSummary>(.+)<\/TrackSummary>/i)[1].trim();
|
||||
return summary;
|
||||
const summary = text.match(/<StatusSummary>(.+)<\/StatusSummary>/i);
|
||||
const expected = text.match(/<ExpectedDeliveryDate>(.+)<\/ExpectedDeliveryDate>/i);
|
||||
return {
|
||||
summary: summary ? summary[1].trim() : null,
|
||||
expected: expected ? expected[1].trim() : null
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "111.1.6",
|
||||
"version": "111.1.7",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user