/
what.js
112 lines (72 loc) · 3 KB
/
what.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
// What.js
// "What is your name?",
// "What is the capital of Spain?"
// -------------------------------------------------- //
var lang = require("../brain/language")
, fileEx = Nodebot.lexicon.file["regular expression"]
, request = require("request")
;
module.exports = function what (a) {
if (a.owner === a.subject) a.subject = "definition";
var nodebot = this
, owner = a.owner
, subject = a.subject || "definition"
, base = (owner) ? nodebot.lexicon[owner] : nodebot.lexicon
, term = (subject && base) ? base[subject] : undefined
;
// Just some more bullet proofing for the subject
if (owner === subject) subject === "definition";
// If the term is a function, call it to determin the value
if (typeof term === "function") term = term().toString();
// If it is a file, simply return that it is a file
if (fileEx.test(subject)) {
return nodebot.say(owner.cyan + " is a file.").request();
}
// Do we have a definition for ths subject?
// -------------------------------------------------- //
if (term) {
switch (subject) {
case "definition":
nodebot.say(lang.capitalize(owner) + " is " + term.green.bold);
break;
default:
nodebot.say(lang.possessify(lang.capitalize(owner)) + " " + subject + " is " + term.toString().green.bold);
}
return nodebot.request();
}
// No, search WolframAlpha
// -------------------------------------------------- //
var app_id = "GVH84U-9YQ66P7PU3";
var qs = require('querystring')
, sax = require("../sax-js")
, strict = true
, parser = sax.parser(strict)
, request = require("request")
, data = qs.stringify({ input: a.tokens.join(" ") })
;
nodebot.say("Hmm, I don't know off the top of my head. Give me a minute...");
request.get("http://api.wolframalpha.com/v2/query?" + data + "&appid=" + app_id, function(err, data) {
var result = []
parser.ontext = function(t) {
var proc = t.trim();
if (proc !== "" && proc !== "\n") result.push(proc)
};
parser.onend = function () {
nodebot.say("Here's what %s has to say:\n", "WolframAlpha".red.bold);
var tidbit = [];
var width = 80;
var space = Array(~~((width/2) - (result[0].length / 2))).join(" ");
console.log(Array(width).join("-"));
console.log(space + result[0].bold.red);
console.log(Array(width).join("-"));
result = result.filter(function(i) { return i.trim() !== ""; });
result.slice(1, 3).forEach(function(i) {
tidbit = i.split("|");
var term = tidbit[0];
console.log(term.bold.trim() + "\n" + tidbit.slice(1).join(":"));
});
};
parser.write(data.body).close();
});
return;
};