-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
87 lines (77 loc) · 3.32 KB
/
node_helper.js
File metadata and controls
87 lines (77 loc) · 3.32 KB
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
/*
* Magic Mirror MMM-Kanji-Random node_helper
* Nick Williams
* MIT Licensed
*/
const NodeHelper = require("node_helper");
const request = require("request");
const sync = require("csv-parse/sync");
const name = "MMM-Kanji-Random";
const fs = require("fs");
module.exports = NodeHelper.create({
start: function() {
console.log(name + ": node_helper started");
this.kanji = {};
},
socketNotificationReceived: function(notification, payload) {
console.log(name + ": node_helper received " + notification + " update");
if (!('N5' in this.kanji) && notification === 'START') {
// Load the kanji
console.log(name + ": reading kanji from " + payload.file);
fs.readFile(payload.file, "utf-8", (err, data) => {
if (!err) {
var kanji = sync.parse(data, { columns: ['glyph','furigana','english','level'], skipLines: 1 })
for (var i = 0; i < kanji.length; i++) {
level = kanji[i].level;
if (!(level in this.kanji)) {
this.kanji[level] = [];
console.log(name + ": found level " + level)
}
this.kanji[level].push(kanji[i])
}
console.log(name + ": loaded " + kanji.length + " kanji")
this.kanjiRequest(payload);
} else {
console.log(name + ": load error " + err)
}
});
} else {
if (Object.keys(this.kanji).length == 0) {
console.log(name + ": skipping further processing, since kanji records aren't loaded");
return;
}
this.kanjiRequest(payload);
}
},
kanjiRequest: function(payload) {
var self = this;
var info = [];
console.log(name + ": generating random kanji of level " + payload.level)
// Pick a random kani and let the mirror know about it.
k = Math.floor(Math.random() * self.kanji[payload.level].length);
var glyph = self.kanji[payload.level][k].glyph;
console.log(name + ": kanji random " + glyph + " (" + k + ")");
var reply = {
'kanji': glyph,
'description': self.kanji[payload.level][k].english,
'furigana': self.kanji[payload.level][k].furigana
};
self.sendSocketNotification('KANJI_RESULT', reply)
// and asynchronously, go lookup the definition of the kanji
url = new URL("http://jisho.org/api/v1/search/words?keyword=" + glyph);
console.log(name + ": kanji random is requesting " + url);
request({url:url, method:'GET'}, function(err, resp, content) {
if (!err && resp.statusCode == 200) {
json = JSON.parse(content);
self.sendSocketNotification('DICT_RESULT', json)
console.log(name + ": successfully looked up dictionary entry")
} else {
if (err) {
console.log(name + ": dictionary error " + err);
} else {
console.log(name + ": dictionary bad response " + resp.statusCode);
}
};
});
},
});