-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquotes.js
More file actions
104 lines (95 loc) · 2.74 KB
/
quotes.js
File metadata and controls
104 lines (95 loc) · 2.74 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var request = require('request');
var _s = require("underscore.string");
var _ = require('underscore');
var quoters = {
qod: function () {
return new Promise(function(resolve, reject) {
request(
'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1',
function (error, response, body) {
if (error !== null) {
reject('Error fetching from http://quotesondesign.com');
return;
}
var json = null;
try {
json = JSON.parse(body);
} catch(e) {
// do nothing
}
if (json == null) {
reject('Error parsing quotesdesign');
} else {
resolve({
text: _s.stripTags(json[0].content).replace(/\n$/, ''),
author: json[0].title,
link: null
});
}
}
);
});
},
forismatic: function() {
return new Promise(function(resolve, reject) {
request(
'http://api.forismatic.com/api/1.0/?method=getQuote&format=json&jsonp=parseQuote&lang=en',
function (error, response, body) {
if (error !== null) {
reject('Error fetching from http://quotesondesign.com');
return;
}
var json = null;
try {
json = JSON.parse(body);
} catch(e) {
// do nothing
}
if (json == null) {
reject('Error parsing forismatic');
} else {
resolve({
text: json.quoteText,
author: json.quoteAuthor,
link: null
});
}
}
);
});
}
};
module.exports = function(RED) {
function Quotes(config) {
RED.nodes.createNode(this, config);
var node = this;
this.source = config.source;
this.markdown = config.markdown;
this.formatQuote = function(text, author, link) {
if (node.markdown) {
return text + '\n' + '*' + author + '*\n' + (link != null ? link : '');
} else {
return text + '\n' + author + '\n' + (link != null ? link : '');
}
};
this.on('input', function(msg) {
var id = null;
if (node.source != null && quoters[node.source] != null && node.source !== 'random') {
id = node.source;
} else {
id = _(quoters).chain().keys().sample().value();
}
quoters[id]()
.then(
function(quote) {
console.log('quote', quote);
msg.payload = node.formatQuote(quote.text, quote.author, quote.link);
node.send(msg);
},
function(error) {
node.error(error);
});
});
}
RED.nodes.registerType('quotes', Quotes);
};