forked from jef/streetmerchant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
406 lines (377 loc) · 11.7 KB
/
config.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import {banner} from './banner';
import {config as config_} from 'dotenv';
import path from 'path';
import {readFileSync} from 'fs';
config_({path: path.resolve(__dirname, '../.env')});
console.info(
banner.render(
envOrBoolean(process.env.ASCII_BANNER, false),
envOrString(process.env.BANNER_COLOR, '#808080')
)
);
/**
* Returns environment variable, given array, or default array.
*
* @param environment Interested environment variable.
* @param array Default array. If not set, is `[]`.
*/
function envOrArray(
environment: string | undefined,
array?: string[]
): string[] {
return (environment
? environment.includes('\n')
? environment.split('\n')
: environment.split(',')
: array ?? []
).map((s) => s.trim());
}
/**
* Returns environment variable, given boolean, or default boolean.
*
* @param environment Interested environment variable.
* @param boolean Default boolean. If not set, is `true`.
*/
function envOrBoolean(
environment: string | undefined,
boolean?: boolean
): boolean {
return environment ? environment === 'true' : boolean ?? true;
}
/**
* Returns environment variable, given string, or default string.
*
* @param environment Interested environment variable.
* @param string Default string. If not set, is `''`.
*/
function envOrString(environment: string | undefined, string?: string): string {
return environment ? environment : string ?? '';
}
/**
* Returns environment variable, given number, or default number.
*
* @param environment Interested environment variable.
* @param number Default number. If not set, is `0`.
*/
function envOrNumber(environment: string | undefined, number?: number): number {
return environment ? Number(environment) : number ?? 0;
}
/**
* Returns environment variable, given number, or default number,
* while handling .env input errors for a Min/Max pair.
* .env errors handled:
* - Min/Max swapped (Min larger than Max, Max smaller than Min)
* - Min larger than default Max when no Max defined
* - Max smaller than default Min when no Min defined
*
* @param environmentMin Min environment variable of Min/Max pair.
* @param environmentMax Max environment variable of Min/Max pair.
* @param number Default number. If not set, is `0`.
*/
function envOrNumberMin(
environmentMin: string | undefined,
environmentMax: string | undefined,
number?: number
) {
if (environmentMin || environmentMax) {
if (environmentMin && environmentMax) {
return Number(
Number(environmentMin) < Number(environmentMax)
? environmentMin
: environmentMax
);
}
if (environmentMax) {
return Number(environmentMax) < (number ?? 0)
? Number(environmentMax)
: number ?? 0;
}
if (environmentMin) {
return Number(environmentMin);
}
}
return number ?? 0;
}
/**
* Returns environment variable, given number, or default number,
* while handling .env input errors for a Min/Max pair.
* .env errors handled:
* - Min/Max swapped (Min larger than Max, Max smaller than Min)
* - Min larger than default Max when no Max defined
* - Max smaller than default Min when no Min defined
*
* @param environmentMin Min environment variable of Min/Max pair.
* @param environmentMax Max environment variable of Min/Max pair.
* @param number Default number. If not set, is `0`.
*/
function envOrNumberMax(
environmentMin: string | undefined,
environmentMax: string | undefined,
number?: number
) {
if (environmentMin || environmentMax) {
if (environmentMin && environmentMax) {
return Number(
Number(environmentMin) < Number(environmentMax)
? environmentMax
: environmentMax
);
}
if (environmentMin) {
return Number(environmentMin) > (number ?? 0)
? Number(environmentMin)
: number ?? 0;
}
if (environmentMax) {
return Number(environmentMax);
}
}
return number ?? 0;
}
const browser = {
isHeadless: envOrBoolean(process.env.HEADLESS),
isIncognito: envOrBoolean(process.env.INCOGNITO, false),
isTrusted: envOrBoolean(process.env.BROWSER_TRUSTED, false),
lowBandwidth: envOrBoolean(process.env.LOW_BANDWIDTH, false),
maxBackoff: envOrNumberMax(
process.env.PAGE_BACKOFF_MIN,
process.env.PAGE_BACKOFF_MAX,
3600000
),
maxSleep: envOrNumberMax(
process.env.PAGE_SLEEP_MIN,
process.env.PAGE_SLEEP_MAX,
10000
),
minBackoff: envOrNumberMin(
process.env.PAGE_BACKOFF_MIN,
process.env.PAGE_BACKOFF_MAX,
10000
),
minSleep: envOrNumberMin(
process.env.PAGE_SLEEP_MIN,
process.env.PAGE_SLEEP_MAX,
5000
),
open: envOrBoolean(process.env.OPEN_BROWSER)
};
const docker = envOrBoolean(process.env.DOCKER);
const logLevel = envOrString(process.env.LOG_LEVEL, 'info');
const notifications = {
desktop: process.env.DESKTOP_NOTIFICATIONS === 'true',
discord: {
notifyGroup: envOrArray(process.env.DISCORD_NOTIFY_GROUP),
webHookUrl: envOrArray(process.env.DISCORD_WEB_HOOK)
},
email: {
password: envOrString(process.env.EMAIL_PASSWORD),
smtpAddress: envOrString(process.env.SMTP_ADDRESS),
smtpPort: envOrNumber(process.env.SMTP_PORT, 25),
to: envOrString(
process.env.EMAIL_TO,
envOrString(process.env.EMAIL_USERNAME)
),
username: envOrString(process.env.EMAIL_USERNAME)
},
mqtt: {
broker: envOrString(process.env.MQTT_BROKER_ADDRESS),
clientId: envOrString(process.env.MQTT_CLIENT_ID),
password: envOrString(process.env.MQTT_PASSWORD),
port: envOrNumber(process.env.MQTT_BROKER_PORT, 1883),
qos: envOrNumber(process.env.MQTT_QOS, 0),
topic: envOrString(process.env.MQTT_TOPIC, 'streetmerchant/alert'),
username: envOrString(process.env.MQTT_USERNAME)
},
pagerduty: {
integrationKey: envOrString(process.env.PAGERDUTY_INTEGRATION_KEY),
severity: envOrString(process.env.PAGERDUTY_SEVERITY, 'info')
},
philips_hue: {
accessToken: envOrString(process.env.PHILIPS_HUE_CLOUD_ACCESS_TOKEN),
apiKey: envOrString(process.env.PHILIPS_HUE_API_KEY),
bridgeIp: envOrString(process.env.PHILIPS_HUE_LAN_BRIDGE_IP),
clientId: envOrString(process.env.PHILIPS_HUE_CLOUD_CLIENT_ID),
clientSecret: envOrString(process.env.PHILIPS_HUE_CLOUD_CLIENT_SECRET),
lightColor: envOrString(process.env.PHILIPS_HUE_LIGHT_COLOR),
lightIds: envOrString(process.env.PHILIPS_HUE_LIGHT_IDS),
lightPattern: envOrString(process.env.PHILIPS_HUE_LIGHT_PATTERN),
refreshToken: envOrString(process.env.PHILIPS_HUE_CLOUD_REFRESH_TOKEN),
remoteApiUsername: envOrString(process.env.PHILIPS_HUE_API_KEY)
},
phone: {
availableCarriers: new Map([
['att', 'txt.att.net'],
['attgo', 'mms.att.net'],
['bell', 'txt.bell.ca'],
['fido', 'fido.ca'],
['google', 'msg.fi.google.com'],
['koodo', 'msg.koodomobile.com'],
['mint', 'mailmymobile.net'],
['rogers', 'pcs.rogers.com'],
['sprint', 'messaging.sprintpcs.com'],
['telus', 'msg.telus.com'],
['tmobile', 'tmomail.net'],
['verizon', 'vtext.com'],
['virgin', 'vmobl.com'],
['virgin-ca', 'vmobile.ca']
]),
carrier: envOrArray(process.env.PHONE_CARRIER),
number: envOrArray(process.env.PHONE_NUMBER)
},
playSound: envOrString(process.env.PLAY_SOUND),
pushbullet: envOrString(process.env.PUSHBULLET),
pushover: {
priority: envOrNumber(process.env.PUSHOVER_PRIORITY),
token: envOrString(process.env.PUSHOVER_TOKEN),
username: envOrString(process.env.PUSHOVER_USER)
},
slack: {
channel: envOrString(process.env.SLACK_CHANNEL),
token: envOrString(process.env.SLACK_TOKEN)
},
telegram: {
accessToken: envOrString(process.env.TELEGRAM_ACCESS_TOKEN),
chatId: envOrArray(process.env.TELEGRAM_CHAT_ID)
},
twilio: {
accountSid: envOrString(process.env.TWILIO_ACCOUNT_SID),
authToken: envOrString(process.env.TWILIO_AUTH_TOKEN),
from: envOrString(process.env.TWILIO_FROM_NUMBER),
to: envOrString(process.env.TWILIO_TO_NUMBER)
},
twitch: {
accessToken: envOrString(process.env.TWITCH_ACCESS_TOKEN),
channel: envOrString(process.env.TWITCH_CHANNEL),
clientId: envOrString(process.env.TWITCH_CLIENT_ID),
clientSecret: envOrString(process.env.TWITCH_CLIENT_SECRET),
refreshToken: envOrString(process.env.TWITCH_REFRESH_TOKEN)
},
twitter: {
accessTokenKey: envOrString(process.env.TWITTER_ACCESS_TOKEN_KEY),
accessTokenSecret: envOrString(process.env.TWITTER_ACCESS_TOKEN_SECRET),
consumerKey: envOrString(process.env.TWITTER_CONSUMER_KEY),
consumerSecret: envOrString(process.env.TWITTER_CONSUMER_SECRET),
tweetTags: envOrString(process.env.TWITTER_TWEET_TAGS)
}
};
const nvidia = {
addToCardAttempts: envOrNumber(process.env.NVIDIA_ADD_TO_CART_ATTEMPTS, 10),
sessionTtl: envOrNumber(process.env.NVIDIA_SESSION_TTL, 60000)
};
const page = {
height: 1080,
inStockWaitTime: envOrNumber(process.env.IN_STOCK_WAIT_TIME),
screenshot: envOrBoolean(process.env.SCREENSHOT),
timeout: envOrNumber(process.env.PAGE_TIMEOUT, 30000),
userAgents: envOrArray(process.env.USER_AGENT, [
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
]),
width: 1920
};
const proxy = {
address: envOrString(process.env.PROXY_ADDRESS),
port: envOrNumber(process.env.PROXY_PORT, 80),
protocol: envOrString(process.env.PROXY_PROTOCOL, 'http')
};
// Check for deprecated configuration values
if (process.env.MAX_PRICE) {
console.warn(
'ℹ MAX_PRICE is deprecated, please use MAX_PRICE_SERIES_{{series}}'
);
}
const store = {
autoAddToCart: envOrBoolean(process.env.AUTO_ADD_TO_CART, true),
country: envOrString(process.env.COUNTRY, 'usa'),
maxPrice: {
series: {
'3060ti': envOrNumber(process.env.MAX_PRICE_SERIES_3060TI),
3070: envOrNumber(process.env.MAX_PRICE_SERIES_3070),
3080: envOrNumber(process.env.MAX_PRICE_SERIES_3080),
3090: envOrNumber(process.env.MAX_PRICE_SERIES_3090),
rx6800: envOrNumber(process.env.MAX_PRICE_SERIES_RX6800),
rx6800xt: envOrNumber(process.env.MAX_PRICE_SERIES_RX6800XT),
rx6900xt: envOrNumber(process.env.MAX_PRICE_SERIES_RX6900XT),
ryzen5600: envOrNumber(process.env.MAX_PRICE_SERIES_RYZEN5600),
ryzen5800: envOrNumber(process.env.MAX_PRICE_SERIES_RYZEN5800),
ryzen5900: envOrNumber(process.env.MAX_PRICE_SERIES_RYZEN5900),
ryzen5950: envOrNumber(process.env.MAX_PRICE_SERIES_RYZEN5950),
sf: envOrNumber(process.env.MAX_PRICE_SERIES_CORSAIR_SF),
sonyps5c: envOrNumber(process.env.MAX_PRICE_SERIES_SONYPS5C),
sonyps5de: envOrNumber(process.env.MAX_PRICE_SERIES_SONYPS5DE),
'test:series': envOrNumber(process.env.MAX_PRICE_SERIES_TEST),
xboxss: -1,
xboxsx: -1
}
},
microCenterLocation: envOrArray(process.env.MICROCENTER_LOCATION, ['web']),
showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS).map((entry) => {
const [name, series] = entry.match(/[^:]+/g) ?? [];
return {
name: envOrString(name),
series: envOrString(series)
};
}),
showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, [
'3060ti',
'3070',
'3080',
'3090',
'rx6800',
'rx6800xt',
'rx6900xt',
'ryzen5600',
'ryzen5800',
'ryzen5900',
'ryzen5950',
'sonyps5c',
'sonyps5de',
'xboxss',
'xboxsx'
]),
stores: envOrArray(process.env.STORES, ['nvidia']).map((entry) => {
const [name, minPageSleep, maxPageSleep] = entry.match(/[^:]+/g) ?? [];
let proxyList;
try {
proxyList = readFileSync(`${name}.proxies`)
.toString()
.trim()
.split('\n')
.map((x) => x.trim());
} catch {}
return {
maxPageSleep: envOrNumberMax(
minPageSleep,
maxPageSleep,
browser.maxSleep
),
minPageSleep: envOrNumberMin(
minPageSleep,
maxPageSleep,
browser.minSleep
),
name: envOrString(name),
proxyList
};
})
};
export const defaultStoreData = {
maxPageSleep: browser.maxSleep,
minPageSleep: browser.minSleep
};
export const config = {
browser,
docker,
logLevel,
notifications,
nvidia,
page,
proxy,
store
};
export function setConfig(newConfig: any) {
const writeConfig = config as any;
for (const key of Object.keys(newConfig)) {
writeConfig[key] = newConfig[key];
}
}