From 8336e335225ec898b77607d007ce063ee8bca7b1 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 3 Mar 2020 11:19:33 -0500 Subject: [PATCH] Get Expected Delivery Date in USPS Tracking --- commands/search/usps-tracking.js | 25 ++++++++++++++++++++----- package.json | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/commands/search/usps-tracking.js b/commands/search/usps-tracking.js index b3598a46..31c18dcd 100644 --- a/commands/search/usps-tracking.js +++ b/commands/search/usps-tracking.js @@ -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: `); } 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: `` + XML: stripIndents` + + 1 + 127.0.0.1 + ${homepage} + + + + ` }); if (text.includes('-2147219283')) return null; if (text.includes('')) throw new Error(text.match(/(.+)<\/Description>/i)[1].trim()); - const summary = text.match(/(.+)<\/TrackSummary>/i)[1].trim(); - return summary; + const summary = text.match(/(.+)<\/StatusSummary>/i); + const expected = text.match(/(.+)<\/ExpectedDeliveryDate>/i); + return { + summary: summary ? summary[1].trim() : null, + expected: expected ? expected[1].trim() : null + }; } }; diff --git a/package.json b/package.json index 7363a55a..8541316e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "111.1.6", + "version": "111.1.7", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {