-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmehCache.js
54 lines (47 loc) · 1.27 KB
/
mehCache.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
const fetch = require('node-fetch');
var conf = require('./lib/botconfig');
var mehItem;
function parseMehItem(mehItem) {
var mehOutput = '';
if (!mehItem) {
return mehOutput;
}
try {
mehOutput = mehItem.items[0].condition + ' ';
mehOutput = mehItem.title + ', ';
if (mehItem.soldOutAt == true) {
mehOutput += 'SOLD OUT';
} else {
mehOutput += '$' + mehItem.items[0].price;
}
// if (mehItem.WootOff == true) {
// mehOutput += ', ';
// mehOutput += mehItem.SoldOutPercentage * 100 + '% Sold';
// }
mehOutput += ', ';
mehOutput += mehItem.url;
} catch (e) {
console.error('Error building meh item output', e);
}
return mehOutput;
}
function refreshCache() {
try {
var mehApiKey = conf.MEH_API_KEY;
fetch(`https://api.meh.com/1/current.json?apikey=${mehApiKey}`)
.then(res => res.json())
.then(json => mehItem = json.deal)
.catch((err) => {
console.error("Error loading meh item", err);
});
} catch (e) {
console.error('Bigger error handling meh cache refresh', e);
}
}
exports.getMehInfo = function() {
return parseMehItem(mehItem);
}
refreshCache();
setInterval(function () {
refreshCache()
}, 60000);