forked from timdows/MMM-JsonTable
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnode_helper.js
61 lines (51 loc) · 1.66 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
const Log = require("logger");
const NodeHelper = require("node_helper");
const xml2js = require("xml2js");
module.exports = NodeHelper.create({
start () {
Log.log("MMM-GmailFeed helper started...");
},
async getFeed (config) {
try {
const self = this;
const feedUrl = "https://mail.google.com/mail/feed/atom";
const response = await fetch(
feedUrl,
{
headers: {
Authorization: `Basic ${btoa(`${config.username}:${config.password}`)}`
}
}
);
if (!response.ok) {
throw new Error(`Error fetching feed: ${response.status}`);
}
const parser = new xml2js.Parser({trim: true, explicitArray: false});
const body = await response.text();
parser.parseString(body, (error_, result) => {
if (result.feed.entry) {
if (!Array.isArray(result.feed.entry)) {
result.feed.entry = [result.feed.entry];
}
result.feed.entry = result.feed.entry.slice(0, config.maxEmails);
}
// Log.log("----");
// Log.log(JSON.stringify(result.feed, null, 2));
// Log.log("----");
// Send the json data back with the URL to distinguish it on the receiving port
self.sendSocketNotification("MMM-GmailFeed_JSON_RESULT", {
username: config.username,
data: result.feed
});
});
} catch (error) {
Log.error(`Error fetching feed: ${error.message}`);
}
},
// Subclass socketNotificationReceived received.
socketNotificationReceived (notification, config) {
if (notification === "MMM-GmailFeed_GET_JSON") {
this.getFeed(config);
}
}
});