-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitoobot.js
210 lines (181 loc) · 5.9 KB
/
itoobot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var _ = require('lodash');
var Client = require('node-rest-client').Client;
var Twit = require('twit');
var async = require('async');
var wordFilter = require('wordfilter');
var env = require('dotenv').config();
var t = new Twit({
consumer_key: env.CONSUMER_KEY,
consumer_secret: env.CONSUMER_SECRET,
access_token: env.ACESSS_TOKEN,
access_token_secret: env.ACCESS_TOKEN_SECRET
});
var wordnikKey = env.WORDNIK_API_KEY
run = function() {
async.waterfall([
getPublicTweet,
extractWordsFromTweet,
getAllWordData,
findAdjNouns,
// getSynonyms,
// getAdjSynonyms,
// getNounSynonyms,
formatTweet,
postTweet
],
function(err, botData) {
if (err) {
console.log('There was an error posting to Twitter: ', err);
} else {
console.log('Tweet successful!');
console.log('Tweet: ', botData.tweetBlock);
}
console.log('Base tweet: ', botData.baseTweet);
});
}
getPublicTweet = function(cb) {
t.get('search/tweets', {q: '\"I love\" -birthday -happy', count: 1, result_type: 'recent', lang: 'en'}, function(err, data, response) {
if (!err) {
var botData = {
baseTweet : data.statuses[0].text.toLowerCase(),
tweetID : data.statuses[0].id_str,
tweetUsername : data.statuses[0].user.screen_name
};
console.log(botData);
cb(null, botData);
} else {
console.log("There was an error getting a public Tweet. Abandoning EVERYTHING :(");
cb(err, botData);
}
});
};
extractWordsFromTweet = function(botData, cb) {
var excludeNonAlpha = /[^a-zA-Z]+/;
var excludeURLs = /https?:\/\/[-a-zA-Z0-9@:%_\+.~#?&\/=]+/g;
var excludeShortAlpha = /\b[a-z][a-z]?\b/g;
var excludeHandles = /@[a-z0-9_-]+/g;
var excludePatterns = [excludeURLs, excludeShortAlpha, excludeHandles];
botData.tweet = botData.baseTweet;
_.each(excludePatterns, function(pat) {
botData.tweet = botData.tweet.replace(pat, ' ');
});
botData.tweetWordList = botData.tweet.split(excludeNonAlpha);
var excludedElements = ['and','the','pick','select','picking','much','love','your', 'just', 'birthday', 'crazy', 'happy'];
botData.tweetWordList = _.reject(botData.tweetWordList, function(w) {
return _.includes(excludedElements, w);
});
cb(null, botData);
};
getAllWordData = function(botData, cb) {
async.map(botData.tweetWordList, getWordData, function(err, results){
botData.wordList = results;
cb(err, botData);
});
}
getWordData = function(word, cb) {
var client = new Client();
var wordnikWordURLPart1 = 'http://api.wordnik.com:80/v4/word.json/';
var wordnikWordURLPart2 = '/definitions?limit=1&includeRelated=false&useCanonical=true&includeTags=false&api_key=';
var args = {headers: {'Accept':'application/json'}};
var wordnikURL = wordnikWordURLPart1 + word.toLowerCase() + wordnikWordURLPart2 + wordnikKey;
client.get(wordnikURL, args, function (data, response) {
if (response.statusCode === 200) {
console.log(data);
var result = data;
if (result.length) {
cb(null, result);
} else {
cb(null, null);
}
} else {
cb(null, null);
}
});
};
findAdjNouns = function(botData, cb) {
botData.adjList = [];
botData.nounList = [];
botData.nounSynonyms = [];
botData.adjSynonyms = [];
botData.wordList = _.compact(botData.wordList);
_.each(botData.wordList, function(wordInfo) {
var word = wordInfo[0].word;
var partOfSpeech = wordInfo[0].partOfSpeech;
if (partOfSpeech === 'noun') {
botData.nounList.push(word);
}
if(partOfSpeech === 'adjective'){
botData.adjList.push(word);
}
});
if (botData.nounList.length > 0 && botData.adjList.length > 0) {
cb(null, botData);
} else {
cb('There is no adjective and/or noun.', botData);
}
}
//continue working
getAdjSynonyms = function(botData, cb) {
async.map(botData.adjList, getSynonyms, function(err, results){
botData.adjSynonyms = results;
console.log("Adj Synonyms: ", botData.adjSynonyms);
cb(err, botData);
});
}
getNounSynonyms = function(botData, cb) {
async.map(botData.nounList, getSynonyms, function(err, results){
botData.nounSynonyms = results;
console.log("Noun Synonyms: ", botData.nounSynonyms);
cb(err, botData);
});
}
getSynonyms = function(word, cb){
var client = new Client();
var wordnikWordURLPart1 = 'http://api.wordnik.com:80/v4/word.json/';
var wordnikWordURLPart2 = '/relatedWords?limit=1&includeRelated=false&useCanonical=true&includeTags=false&api_key=';
var args = {headers: {'Accept':'application/json'}};
var wordnikURL = wordnikWordURLPart1 + word + wordnikWordURLPart2 + wordnikKey;
client.get(wordnikURL, args, function (data, response) {
if (response.statusCode === 200) {
console.log(data);
var result = data;
if (result.length) {
cb(null, result);
} else {
cb(null, null);
}
} else {
cb(null, null);
}
});
};
formatTweet = function(botData, cb) {
botData.adjNoun = [];
_.each(botData.adjList.slice(0,1), function(word) {
botData.adjNoun.push(word);
});
_.each(botData.nounList.slice(0,1), function(word) {
botData.adjNoun.push(word);
});
var tweetLine1 = botData.adjNoun.join(' ');
var username = botData.tweetUsername;
if(tweetLine1[tweetLine1.length - 1] === 's'){
botData.tweetBlock = 'I, too, love ' + tweetLine1 + ', ' + username + '. ❤️'
console.log(botData);
} else{
botData.tweetBlock = 'I, too, love ' + tweetLine1 + 's, ' + username + '. ❤️';
console.log(botData);
}
cb(null, botData);
}
postTweet = function(botData, cb) {
console.log(botData);
if (!wordFilter.blacklisted(botData.tweetBlock)) {
t.post('statuses/update', {status: botData.tweetBlock}, function(err, data, response) {
cb(err, botData);
});
}
}
run();
run();
run();