This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·68 lines (55 loc) · 2.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const request = require('request')
const { spawn } = require('child_process')
const notifier = require('node-notifier')
const processArgs = require('command-line-args')
const args = processArgs([
{ name: 'apiKey', alias: 'k', type: String },
{ name: 'shipmentId', alias: 's', type: String },
{ name: 'lang', alias: 'l', type: String, defaultValue: 'en' }
])
const checkInterval = 300000
let previousEvent = null
if (!args.shipmentId) {
console.error('Invalid shipment ID. Please pass one using `--shipmentId=<shipmentId>` or `-s <shipmentId>`')
process.exit(1)
}
function notify(event) {
notifier.notify({
title: `New shipment update! (${args.shipmentId})`,
subtitle: event.location.displayName,
message: event.eventDescription,
timeout: checkInterval
})
spawn('say', ['New shipment update!'])
}
function checkForUpdates() {
console.log('Checking for updates...')
const url = `https://api2.postnord.com/rest/shipment/v1/trackandtrace/findByIdentifier.json?id=${args.shipmentId}&locale=${args.lang}&apikey=${args.apiKey}`
request.get(url, {json: true}, (err, response, body) => {
if (err) {
throw err
}
if (
!(body && typeof(body) === 'object') ||
!(body.TrackingInformationResponse && typeof(body.TrackingInformationResponse) === 'object') ||
!(body.TrackingInformationResponse.shipments && body.TrackingInformationResponse.shipments instanceof Array) ||
!(body.TrackingInformationResponse.shipments[0] && typeof(body.TrackingInformationResponse.shipments[0]) === 'object') ||
!(body.TrackingInformationResponse.shipments[0].items && body.TrackingInformationResponse.shipments[0].items instanceof Array) ||
!(body.TrackingInformationResponse.shipments[0].items[0] && typeof(body.TrackingInformationResponse.shipments[0].items[0]) === 'object') ||
!(body.TrackingInformationResponse.shipments[0].items[0].events && body.TrackingInformationResponse.shipments[0].items[0].events instanceof Array)
) {
console.error('Unexpected API response:\n${body}\n\n')
return
}
const events = body.TrackingInformationResponse.shipments[0].items[0].events
const latestEvent = events[events.length-1]
if (!previousEvent || previousEvent.eventTime !== latestEvent.eventTime) {
console.log(`Got new event!\n - [${latestEvent.location.displayName}] ${latestEvent.eventDescription}`)
previousEvent = latestEvent
notify(latestEvent)
}
})
}
checkForUpdates()
setInterval(checkForUpdates, checkInterval)