-
Notifications
You must be signed in to change notification settings - Fork 3
/
thanks.js
186 lines (156 loc) · 4.44 KB
/
thanks.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
/* global process */
var Bot = require('./node_modules/twit/examples/bot');
var config = require('./config');
var createWordnok = require('wordnok').createWordnok;
var tributeDemander = require('./tributedemander');
var figurepicker = require('./figurepicker');
var prepphrasepicker = require('./prepphrasepicker');
var logger = require('./logger');
var behavior = require('./behaviorsettings');
var probable = require('probable');
var callNextTick = require('call-next-tick');
var _ = require('lodash');
var canonicalizer = require('canonicalizer');
var createNounfinder = require('nounfinder');
var pickThanksTopic = require('./pick-thanks-topic');
var relevantRelatedWordTypes = require('./relevant-related-word-types');
var bot = new Bot(config.twitter);
var simulationMode = process.argv[2] === '--simulate';
logger.info('Thanks maker is running.');
var wordnok = createWordnok({
apiKey: config.wordnikAPIKey,
logger: {
log: logger.info
}
});
var maxCommonnessForSecondary =
behavior.maxCommonnessForReplyTopic[0] +
probable.roll(
behavior.maxCommonnessForSecondaryTopic[1] -
behavior.maxCommonnessForSecondaryTopic[0]
);
(function go() {
behavior.visionDonors.forEach(postThanksTribute);
postThanksTribute('https://www.patreon.com/deathmtn', {
'same-context': [
'vision',
'aesthesis',
'sight',
'eyesight',
'perception',
'eyes'
]
});
})();
function postThanksTribute(prefix, secondaryTopicPool) {
var primaryTopic;
var primaryDemand;
postOnTopic(null, pickThanksTopic());
function postOnTopic(error, topic) {
if (error) {
logger.error(error);
process.exit();
}
var forms = canonicalizer.getSingularAndPluralForms(topic);
primaryTopic = forms[0];
primaryDemand = getPrimaryDemand(primaryTopic, false);
if (secondaryTopicPool) {
makeDemands(null, secondaryTopicPool);
} else {
wordnok.getRelatedWords(
{
word: primaryTopic
},
makeDemands
);
}
}
function makeDemands(relatedWordsError, relatedWords) {
var tweetText = prefix + ' ' + primaryDemand;
if (relatedWordsError) {
logger.error(relatedWordsError);
process.exit();
} else {
getSecondaryDemand(relatedWords, appendDemandToTweet);
}
function appendDemandToTweet(error, secondaryDemand) {
if (error) {
logger.error(error);
// An error is OK here. We can keep going.
}
if (secondaryDemand) {
tweetText += '! ' + secondaryDemand;
}
callNextTick(tweet, tweetText);
}
}
}
function getSecondaryDemand(relatedWords, done) {
var nounfinder;
if (relatedWords) {
var relevantLists = _.values(
_.pick(relatedWords, relevantRelatedWordTypes)
);
if (relevantLists.length > 0) {
var topics = _.flatten(relevantLists);
nounfinder = createNounfinder({
wordnikAPIKey: config.wordnikAPIKey
});
nounfinder.getNounsFromText(topics.join(' '), filterForInterestingness);
return;
}
}
// Fell through? Call back with nothing.
callNextTick(done);
function filterForInterestingness(error, nouns) {
if (error) {
done(error);
} else {
nounfinder.filterNounsForInterestingness(
nouns,
maxCommonnessForSecondary,
assembleSecondaryDemand
);
}
}
function assembleSecondaryDemand(error, nouns) {
if (error) {
done(error);
} else {
var demand;
if (nouns.length > 0) {
demand = tributeDemander.makeDemandForTopic({
topic: probable.pickFromArray(nouns),
prepositionalPhrase: prepphrasepicker.getPrepPhrase(),
tributeFigure: figurepicker.getSecondaryTributeFigure()
});
}
done(error, demand);
}
}
}
function getPrimaryDemand(topic, isEmoji) {
var opts = {
topic: topic,
prepositionalPhrase: prepphrasepicker.getPrepPhrase(),
tributeFigure: figurepicker.getMainTributeFigure(),
isEmoji: isEmoji
};
if (isEmoji) {
opts.repeatNTimesToPluralize = probable.roll(4) + probable.roll(4) + 2;
}
return tributeDemander.makeDemandForTopic(opts);
}
function tweet(tweetText) {
if (simulationMode) {
console.log('Would have tweeted', tweetText);
} else {
bot.tweet(tweetText, function reportTweetResult(error, reply) {
if (error) {
console.log(error);
} else {
logger.info(new Date().toString(), 'Tweet posted', reply);
}
});
}
}