-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·147 lines (122 loc) · 4.59 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/node
const schedule = require('node-schedule')
const request = require('request')
const querystring = require('querystring')
const nodemailer = require('nodemailer')
const moment = require('moment-timezone')
const NAME = process.env.AN_NAME
const FROM_EMAIL = process.env.FROM_EMAIL
const TO_EMAIL = process.env.TO_EMAIL || process.env.FROM_EMAIL
const PASSWORD = process.env.AN_PASSWORD
const POST_CODE = process.env.POST_CODE
const URL = 'http://www.apple.com/ca/shop/retail/pickup-message'
const DEFAULT_MODELS = [
'MNNY2CL/A', // Sport band
'MNP02CL/A' // Nylon strap
]
const MODELS = (process.env.AN_MODELS && process.env.AN_MODELS.split(' ')) || DEFAULT_MODELS
const constructUrl = (model) => {
return `${URL}?${querystring.stringify({
'parts.0': model,
'location': POST_CODE
})}`
}
const getTheTime = (isFullDate) => {
const format = isFullDate ? 'MMM Do, h:mm a' : 'h:mm:ss a'
return moment(new Date().toISOString()).tz('America/Vancouver').format(format)
}
const getDistance = (str) => {
return parseInt(str.replace(/\s+km/, ''), 10)
}
const craftBody = (stores, model) => {
var body = `${model} available at the following stores; ${getTheTime(true)}:\n` + (
model === MODELS[0] ?
'http://www.apple.com/ca/shop/buy-watch/apple-watch/rose-gold-aluminium-pink-sand-sport-band?product=MNNY2CL/A&step=detail\n\n'
:
'http://www.apple.com/ca/shop/buy-watch/apple-watch/rose-gold-aluminium-light-pink-midnight-blue-woven-nylon?product=MNP02CL/A&step=detail\n\n'
)
stores.forEach((store) => {
body += `${store.storeName}\n${store.hours}\n`
})
return body
}
const sendMail = (availableStores, model) => {
return new Promise((resolve, reject) => {
const transporter = nodemailer.createTransport(`smtps://${encodeURIComponent(FROM_EMAIL)}:${PASSWORD}`)
const body = craftBody(availableStores, model)
const mailOptions = {
from: `${NAME} <${FROM_EMAIL}>`,
to: TO_EMAIL,
subject: 'Apple Watch is available for pickup',
text: body
}
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error)
reject(error)
} else {
console.log('Message sent: ' + info.response)
resolve()
}
})
})
}
const outputHours = (hours) => {
var str = ''
hours.forEach((hour) => {
str += `${hour.storeDays} ${hour.storeTimings}\n`
})
return str
}
const isAvailable = (detail) => {
return detail !== 'unavailable' &&
detail !== 'ships-to-store'
}
var JOB
const makeRequest = () => {
MODELS.forEach((model) => {
request(constructUrl(model), (error, response, body) => {
if (error) {
console.error(error)
JOB.cancel()
} else {
var availableStores = []
try {
const resp = JSON.parse(body)
const stores = resp.body.stores
stores.forEach((store) => {
const modelAvailability = store.partsAvailability[model]
const storeName = store.storeName
const dist = getDistance(store.storeDistanceWithUnit)
const storeHours = store.storeHours.hours
if (dist < 22 && isAvailable(modelAvailability.pickupDisplay)) {
console.log(`Apple Watch model ${model} found at ${storeName}`)
availableStores.push({
modelName: modelAvailability.storePickupProductTitle,
storeName: storeName,
hours: outputHours(storeHours)
})
}
})
} catch(e) {
console.log(e.message)
}
if (availableStores.length > 0) {
console.log('Dispatching e-mail.')
sendMail(availableStores, model).then(() => {
JOB.cancel()
})
} else {
console.log(`[${getTheTime(false)}] Model ${model} not found.`)
}
}
})
})
}
const scheduleJob = () => {
console.log('Searching for available Apple Watches every three minutes...')
// Once per 3 minutes
const rule = '*/5 * * * *'
JOB = schedule.scheduleJob(rule, makeRequest);
}
scheduleJob()