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