-
Notifications
You must be signed in to change notification settings - Fork 1
/
wolfram.js
56 lines (47 loc) · 1.71 KB
/
wolfram.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
"use strict";
let xml2js = require("xml2js");
let request = require("request");
let Log = require("log");
let logger = new Log("WolframAlpha");
var wolfram = {
withToken: function(token) {
wolfram.token = token;
return wolfram;
},
query: function(query, callback, opts) {
if(!wolfram.token) {
logger.critical("Application key not set");
return;
}
var uri = 'http://api.wolframalpha.com/v2/query?input=' + encodeURIComponent(query) + '&primary=true&maxwidth=400&width=400&appid=' + wolfram.token;
var retries = opts ? opts.retries : 0;
request(uri, function(error, response, body) {
if(!error && response.statusCode == 200) {
var doc = xml2js.parseString(body, function(err, result) {
//console.log(result);
if (!err) {
callback(result.queryresult.$.error == "true", result.queryresult);
} else {
callback(true, null);
}
});
} else {
logger.critical("Query '" + query + "' resulted in error:");
logger.critical(error);
logger.critical(response);
if (retries < 3) {
retries ++;
logger.critical("Retry " + retries + " / 3");
if (!opts) {
opts = {};
}
opts.retries = retries;
wolfram.query(query, callback, opts);
} else {
callback(true, null);
}
}
})
}
};
module.exports = wolfram;