UNPKG

@heuristical/trackit

Version:

This module allows you to connect to many shipping carriers like UPS and FedEx and download tracking data for your packages in a common schema

177 lines 7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AmazonClient = void 0; /* eslint-disable camelcase, constructor-super, no-constant-condition, no-eval, no-this-before-super, no-unused-vars, */ // TODO: This file was created by bulk-decaffeinate. // Fix any style issues and re-enable lint. /* * decaffeinate suggestions: * DS001: Remove Babel/TypeScript constructor workaround * DS101: Remove unnecessary use of Array.from * DS102: Remove unnecessary code created because of implicit returns * DS103: Rewrite code to no longer use __guard__ * DS206: Consider reworking classes to avoid initClass * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ const cheerio_1 = require("cheerio"); const date_fns_1 = require("date-fns"); const shipper_1 = require("./shipper"); function __guard__(value, transform) { return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; } const MONTHS = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']; const DAYS_OF_WEEK = { SUNDAY: 0, MONDAY: 1, TUESDAY: 2, WEDNESDAY: 3, THURSDAY: 4, FRIDAY: 5, SATURDAY: 6 }; class AmazonClient extends shipper_1.ShipperClient { constructor(options) { super(); this.STATUS_MAP = new Map([ ['ORDERED', shipper_1.STATUS_TYPES.SHIPPING], ['SHIPPED', shipper_1.STATUS_TYPES.EN_ROUTE], ['IN_TRANSIT', shipper_1.STATUS_TYPES.EN_ROUTE], ['OUT_FOR_DELIVERY', shipper_1.STATUS_TYPES.OUT_FOR_DELIVERY], ['DELIVERED', shipper_1.STATUS_TYPES.DELIVERED], ]); this.options = options; } validateResponse(response, cb) { const $ = cheerio_1.load(response, { normalizeWhitespace: true }); return cb(null, { $, response }); } getService() { } getWeight() { } getDestination(data) { if (data == null) { return; } const { $ } = data; const dest = $('.delivery-address').text(); if (dest != null ? dest.length : undefined) { return this.presentLocationString(dest); } } getEta(data) { if (data == null) { return; } let eta = null; const baseDate = date_fns_1.set(new Date(), { hours: 20, minutes: 0, seconds: 0, milliseconds: 0 }); const { response } = data; let matchResult = response.toString().match('"promiseMessage":"Arriving (.*?)"'); if (matchResult == null) { matchResult = response.toString().match('"promiseMessage":"Now expected (.*?)"'); } let arrival = matchResult != null ? matchResult[1] : undefined; if (arrival != null ? new RegExp('today').exec(arrival) : undefined) { eta = baseDate; } else if (arrival != null ? new RegExp('tomorrow').exec(arrival) : undefined) { eta = date_fns_1.addDays(baseDate, 1); } else { if (arrival != null ? new RegExp('-').exec(arrival) : undefined) { arrival = arrival.split('-')[1]; // Get latest possible ETA } let foundMonth = false; for (const month of Array.from(MONTHS)) { if (arrival === null || arrival === void 0 ? void 0 : arrival.toUpperCase().match(month)) { foundMonth = true; } } if (foundMonth) { eta = date_fns_1.set(new Date(arrival), { year: new Date().getUTCFullYear(), hours: 20, minutes: 0, seconds: 0, milliseconds: 0 }); } else { for (const dayOfWeek in DAYS_OF_WEEK) { const dayNum = DAYS_OF_WEEK[dayOfWeek]; if (arrival === null || arrival === void 0 ? void 0 : arrival.toUpperCase().match(dayOfWeek)) { eta = date_fns_1.setDay(baseDate, dayNum); } } } } if (!(eta ? date_fns_1.isValid(eta) : undefined)) { return; } return (eta != null ? eta : undefined); } presentStatus(data) { const { response } = data; return this.STATUS_MAP.get(__guard__(response.toString().match('"shortStatus":"(.*?)"'), x => x[1])); } getActivitiesAndStatus(data) { const activities = []; const status = this.presentStatus(data); if (data == null) { return { activities, status }; } const { $ } = data; for (const row of Array.from($('#tracking-events-container').children('.a-container').children('.a-row'))) { if (!$(row).children('.tracking-event-date-header').length) { continue; } let dateText = ''; const rows = Array.from($(row).children('.a-row')); for (let subrow of rows) { subrow = $(subrow); const cols = subrow.children('.a-column'); if (subrow.hasClass('tracking-event-date-header')) { dateText = subrow.children('.tracking-event-date').text(); if (dateText.split(',').length === 2) { dateText += `, ${new Date().getUTCFullYear()}`; } } else if (cols.length === 2) { let timestamp; const details = $(cols[1]).find('.tracking-event-message').text(); const location = $(cols[1]).find('.tracking-event-location').text(); const timeText = $(cols[0]).find('.tracking-event-time').text(); if (dateText ? dateText.length : undefined) { if ((timeText != null ? timeText.length : undefined)) { timestamp = new Date(`${dateText} ${timeText} +0000`); } else { timestamp = new Date(`${dateText} 00:00:00 +0000`); } } activities.push({ timestamp, location, details }); } } } return { activities, status }; } requestOptions({ orderID, orderingShipmentId }) { return { method: 'GET', gzip: true, headers: { accept: 'text/html', 'accept-encoding': 'gzip' }, uri: 'https://www.amazon.com/gp/css/shiptrack/view.html' + '/ref=pe_385040_121528360_TE_SIMP_typ?ie=UTF8' + `&orderID=${orderID}` + `&orderingShipmentId=${orderingShipmentId}` + '&packageId=1' }; } } exports.AmazonClient = AmazonClient; ; //# sourceMappingURL=amazon.js.map