-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
177 lines (151 loc) · 5.83 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
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
/* Magic Mirror
* Node Helper: MMM-GoogleDocs-Notes
*
* By No3x
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const cheerio = require('cheerio');
const fs = require('fs');
const { google } = require('googleapis');
const TOKEN_DIR = `${process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE}/.credentials`;
const TOKEN_PATH = `${TOKEN_DIR}/MMM-GoogleDocs-Notes.json`;
let moduleInstance = null;
let config = null;
module.exports = NodeHelper.create({
start() {
moduleInstance = this;
},
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
authorize(credentials, callback) {
const { client_secret, client_id } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret
);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return this.getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
return false;
});
},
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {callback} callback The callback for the authorized client.
*/
getNewToken(oAuth2Client, callback) {
console.log(
'[MMM-GoogleDocs-Notes] Creating a token is an interactive process that requires user input. For that please run \\"node authorize.mjs\\" in the MMM-GoogleDocs-Notes directory'
);
},
async getTask(drive, noteDocumentId) {
try {
const { modifiedTime } = (await drive.files.get({
fileId: noteDocumentId,
fields: ['modifiedTime']
})).data;
console.log(`[MMM-GoogleDocs-Notes][${noteDocumentId}] last modified time of the note: ${modifiedTime}`);
try {
const content = (await drive.files.export({
fileId: noteDocumentId,
mimeType: 'text/html'
})).data;
// console.log(`[MMM-GoogleDocs-Notes][${noteDocumentId}] doc content of your note: ${content}`);
const $ = cheerio.load(content);
$('*').css('width', '');
$('*').css('height', '');
const htmlContent = $('body').html();
// console.log(`[MMM-GoogleDocs-Notes][${noteDocumentId}] html content of your note: ${htmlContent}`);
return {
noteText: htmlContent,
dateStamp: modifiedTime
};
} catch (err) {
console.log(
`[MMM-GoogleDocs-Notes] Failed to get the content of your note. The docs API returned an error: ${err}`
);
throw err;
}
} catch (err) {
console.log(
`[MMM-GoogleDocs-Notes] Failed to get the last modified time of your note. The docs API returned an error: ${err}`
);
throw err;
}
},
/**
* Retrieves note from Google Drive and sends information to the browser.
* @param {google.auth.OAuth2} auth The authenticated Google OAuth 2.0 client.
*/
async getNoteData(auth) {
console.log('[MMM-GoogleDocs-Notes] getNoteData');
const drive = google.drive({ version: 'v3', auth });
const options = {
orderBy: 'modifiedTime',
q: `mimeType = 'application/vnd.google-apps.document' and name starts with '${config.notesPrefix}' and trashed = false`
};
try {
const { files } = (await drive.files.list(options)).data;
// console.log(JSON.stringify(files, null, 4));
console.log(`[MMM-GoogleDocs-Notes] Found ${files.length} documents in drive matching the search ${JSON.stringify(options)}.`);
if (!files.length > 0) {
console.log('[MMM-GoogleDocs-Notes] Did not find your note in drive.');
await drive.files.create({
requestBody: {
mimeType: 'application/vnd.google-apps.document',
name: `${config.notesPrefix} Google Docs Note`,
body: 'The first note'
}
});
}
const tasks = [];
for (const note of files) {
const noteDocumentId = note.id;
console.log(`[MMM-GoogleDocs-Notes] noteDocumentId: ${noteDocumentId}`);
const task = moduleInstance.getTask(drive, noteDocumentId);
tasks.push(task);
}
const results = await Promise.all(tasks.map((p) => p.catch((e) => e)));
const notes = results.filter((result) => !(result instanceof Error));
moduleInstance.sendSocketNotification(
'MMM-GOOGLEDOCS-NOTES-RESPONSE',
{ data: notes }
);
} catch (err) {
console.log(
`[MMM-GoogleDocs-Notes] Failed to list the documents in your drive. The docs API returned an error: ${err}`
);
}
},
/** socketNotificationReceived(notification, payload)
* This method is called when a socket notification arrives.
*
* argument notification string - The identifier of the noitication.
* argument payload mixed - The payload of the notification.
*/
socketNotificationReceived(notification, payload) {
if (notification === 'MMM-GOOGLEDOCS-NOTES-GET') {
if (config == null) {
config = payload;
}
// console.log(`[MMM-GoogleDocs-Notes] TOKEN_PATH:${TOKEN_PATH}`);
// Load client secrets from a local file.
fs.readFile(`${this.path}/client_secret.json`, (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Docs API.
console.log('[MMM-GoogleDocs-Notes] authorizing...');
this.authorize(JSON.parse(content), this.getNoteData);
return false;
});
}
}
});