-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
69 lines (61 loc) · 2.24 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-Gas
*
* By Cowboysdude
*
*/
const NodeHelper = require('node_helper');
const cheerio = require('cheerio');
const request = require('request');
module.exports = NodeHelper.create({
start: function() {
console.log("Starting module: " + this.name);
},
getUrl: function() {
var url = null;
if (this.config.typeGas === "regular") {
url = "https://www.autoblog.com/" + this.config.zip + "-gas-prices/"
} else {
url = "https://www.autoblog.com/" + this.config.zip + "-gas-prices/" + this.config.typeGas
}
return url;
},
getGAS: function(url) {
request({
url: this.getUrl(),
//url: "https://www.autoblog.com/14904-gas-prices/",
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 Safari/537.36'
}
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(body);
var gasset = [];
$('.shop').each(function(i, elem) {
const gaslist = {
name: $(elem).find('.name h4').text(),
dist: $(elem).find('ul .dist').text().replace(/[\n\t\r]/g, ""),
address: $(elem).find('.name address').text(),
ppg: $(elem).find('li .slab.price .price').text(),
//updated: $(elem).find('li .time').text(),
};
gasset.push(gaslist);
});
console.log(gasset);
if (this.config.sortBy == 'price') {
gasset.sort((a, b) => eval(a.ppg.slice(1)) - eval(b.ppg.slice(1)));
}
//console.log(gasset);
this.sendSocketNotification("GAS_RESULT", gasset);
}
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'CONFIG') {
this.config = payload;
} else if (notification === 'GET_GAS') {
this.getGAS(payload);
}
}
});