-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.js
141 lines (128 loc) · 4.38 KB
/
query.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
"use strict";
let api = require("./api.js");
let wolfram = api.wolfram;
let cloudinary = api.cloudinary;
let units = require("./units.js");
class Query {
constructor(query) {
this.query = query;
}
get query() {
return this._query;
}
set query(query) {
this._query = query;
}
/**
* Solves the query and calls the given function.
* @param callback A function with the signature "message, attachments, error".
**/
solve(callback) {}
}
/**
* Represents a simple conversion
**/
class SimpleConvert extends Query {
constructor(input) {
super(input);
this.solution = units.getConversions(input);
}
/**
* Converts the given input to the given target units.
* @param callback A function with the signature "message, attachments, error".
* This query type will only generate a message.
**/
solve(callback) {
callback(this.query + " = " + this.solution.join(" = "), null, null);
}
}
/**
* Represents a query to Wolfram|Alpha
**/
class WolframQuery extends Query {
/**
* Constructs the query to Wolfram|Alpha.
* @param query The query to send.
* @param full Whether to display all resulting pods. If false, will only display the primary pod and all its subpods.
*/
constructor(query, full) {
super(query);
this.full = full;
}
get full() {
return this._full;
}
set full(full) {
this._full = full;
}
/**
* Executes the given query on Wolfram|Alpha.
* @param callback A function with the signature "message, attachments, error".
* This query type will only generate attachments.
*/
solve(callback) {
var self = this;
wolfram.query(this.query, function (err, res) {
function addAttachment(pod) {
var attachment = {};
if (!self.full) {
attachment.color = "#F58120";
}
attachment.title = pod.$.title;
attachment.title_link = "http://www.wolframalpha.com/input/?i=" + encodeURIComponent(self.query);
pod.subpod.forEach(function (subpod) {
attachment.image_url = subpod.img[0].$.src;
attachment.fallback = subpod.plaintext[0];
if (pod.$.primary) {
attachment.color = "#F58120";
}
attachments.push(attachment);
attachment = {}
});
}
function checkDone() {
var containsWolfram = false;
attachments.forEach(function(attachment) {
if (attachment.image_url && attachment.image_url.indexOf("wolframalpha.com") >= 0) {
containsWolfram = true;
}
});
if (!containsWolfram) {
callback(null, attachments, false);
}
}
if (!err && res && res.pod) {
var attachments = [];
res.pod.forEach(function(pod) {
if (!self.full && !pod.$.primary) {
return;
}
addAttachment(pod);
});
if (attachments.length == 0 && res.pod.length >= 2) {
addAttachment(res.pod[1]);
} else if (res.pod.length == 0) {
callback(null, null, true);
}
attachments.forEach(function (attachment) {
if (attachment.image_url) {
cloudinary.uploader.upload(attachment.image_url, function(result) {
var i = attachments.indexOf(attachment);
attachments[i].image_url = result.secure_url;
attachments[i].image_width = result.width;
attachments[i].image_height = result.height;
attachments[i].from_url = result.secure_url;
checkDone();
}, {format: "png"});
}
});
} else {
callback(null, null, true);
}
});
}
}
module.exports = {
SimpleConvert: SimpleConvert,
WolframQuery: WolframQuery
};