-
Notifications
You must be signed in to change notification settings - Fork 1
/
getContentAndTweet.js
179 lines (167 loc) · 6.35 KB
/
getContentAndTweet.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
const { sendTweet } = require("./twit/twitter");
const database = require("./twit/database");
const grammar = require("./grammar");
const hashtagSanitize = /['\- ]/g;
const hashtagDeEscape = /\\#/g;
const chanceOfAReply = 0.99;
const microDelay = () => {
return Math.round(1000 + Math.random() * 30000);
};
const handleReply = async tweet => {
console.log("\nBEGIN REPLY...");
const userHandle = tweet.user.screen_name;
const originalTweetId = tweet.in_reply_to_status_id_str;
const replyTweetId = tweet.id_str;
if (originalTweetId && Math.random() < chanceOfAReply) {
// check if we have a deity associated with the original tweet
try {
const data = await database.get(originalTweetId);
if (data) {
// TODO: sometimes deities are sent to people other than their original tweeter
// sometimes deities domains and other details appear to be swapped with other deities
// Investigate why
let [
deityName,
deityType,
deityDomain,
spiritAnimal,
deityThey,
deityThem,
deityTheir,
deityTheirs,
previousTemplate,
replyCount,
deityPlace,
spiritBird,
deityPunishment
] = data.split(":");
// we have a deity!
console.log(`This is the ${replyCount} tweet in a thread
The last template was: ${previousTemplate}`);
sendTweetAndLogDeity(
`[userHandle:@${userHandle}][deityName:${deityName}][deityType:${deityType}][deityDomain:${deityDomain}][spiritAnimal:${spiritAnimal}][deityThey:${deityThey}][deityThem:${deityThem}][deityTheir:${deityTheir}][deityTheirs:${deityTheirs}][deityPlace:${deityPlace}][spiritBird:${spiritBird}]#replyWithDeity#`,
replyTweetId,
replyCount
);
} else {
// we don't have a deity stored for the original tweet
sendTweetAndLogDeity(
`#[userHandle:@${userHandle}]replyWithLostDeity#`,
replyTweetId
);
}
} catch (error) {
console.log(`Error retrieving previous details from database ${error}`);
}
} else {
// this is a mention, not a reply, so there's no deity
sendTweetAndLogDeity(
`#[userHandle:@${userHandle}]replyOrigin#`,
replyTweetId
);
}
console.log("...END REPLY\n");
};
// HORRIBLE horrible horrible hack to find symbols used in the tweet - because flattening gets an earlier version !?!
const getSymbolLastUsed = symbol => {
try {
return grammar.symbols[symbol].uses.slice(-1)[0].node.childRule;
} catch (error) {
return grammar.symbols[symbol].rawRules[0];
}
};
const addHashTagIfTheresRoom = (tweet, hashtag) => {
const sanitizedHashtag = hashtag.replace(hashtagSanitize, "");
if (tweet.length < 278 - sanitizedHashtag.length)
return tweet + `\n#${sanitizedHashtag}`;
return tweet;
};
const makeMultiWordHashtag = phrase =>
phrase
.replace(/ and .*$/, "")
.replace(/[ -](\w)/g, match => match.toUpperCase())
.replace(/&/g, "and");
const addHashTags = (tweet, deityName, deityType, deityDomain) => {
let amendedTweet = tweet;
amendedTweet = addHashTagIfTheresRoom(amendedTweet, deityName);
amendedTweet = addHashTagIfTheresRoom(
amendedTweet,
makeMultiWordHashtag(deityDomain)
);
// amendedTweet = addHashTagIfTheresRoom(
// amendedTweet,
// makeMultiWordHashtag(deityType)
// );
if (new Date().getDay() === 4 && Math.random() < 0.02)
amendedTweet = addHashTagIfTheresRoom(amendedTweet, "folkloreThursday");
// const randomHashtag = grammar.flatten("#hashtag#");
// amendedTweet = addHashTagIfTheresRoom(amendedTweet, randomHashtag);
return amendedTweet;
};
const sendTweetAndLogDeity = (template, in_reply_to_status_id, replyCount) => {
const tweetCount = 1 + (Number(replyCount) || 0);
const root = grammar.createRoot(template);
root.expand();
// HORRIBLE hack to find symbols used in the tweet - because flattening gets an earlier version !?!
const deityName = getSymbolLastUsed("deityName");
const deityType = getSymbolLastUsed("deityType");
const deityDomain = getSymbolLastUsed("deityDomain");
const spiritAnimal = getSymbolLastUsed("spiritAnimal");
const spiritBird = getSymbolLastUsed("spiritBird");
const deityPlace = getSymbolLastUsed("deityPlace");
const deityThey = getSymbolLastUsed("deityThey");
const deityThem = getSymbolLastUsed("deityThem");
const deityTheir = getSymbolLastUsed("deityTheir");
const deityTheirs = getSymbolLastUsed("deityTheirs");
const deityPunishment = getSymbolLastUsed("deityPunishment");
const auto_populate_reply_metadata = !!in_reply_to_status_id;
// This is needed because of a bug where the escape character isn't being correctly stripped
const tweetContent = root.finishedText.replace(hashtagDeEscape, "#");
console.log(
`TWEET CONTENT & SYMBOLS: ${tweetContent}, ${deityName}, ${deityType}, ${deityDomain}, ${deityPunishment}`.replace(
"/\n/g",
"\\n"
)
);
const status = addHashTags(tweetContent, deityName, deityType, deityDomain);
console.log(
`CREATE FROM: '${template}\n\tTWEET: ${status}\n\tDEITY: ${deityName}`.replace(
"/\n/g",
"\\n"
)
);
setTimeout(() => {
sendTweet({
status,
in_reply_to_status_id,
auto_populate_reply_metadata
})
.then(data => {
database.set(
data.id_str,
`${deityName}:${deityType}:${deityDomain}:${spiritAnimal}:${deityThey}:${deityThem}:${deityTheir}:${deityTheirs}:${template.replace(
/:/g,
"~"
)}:${tweetCount}:${deityPlace}:${spiritBird}:${deityPunishment}`
);
})
.catch(error => {
console.log(`there was an error sending the tweet: ${error}`);
});
}, microDelay());
};
const sendTweetAfterDelay = delayFunction => {
setTimeout(sendRandomTweet, delayFunction(), delayFunction);
};
const sendRandomTweet = delayFunction => {
sendTweetAndLogDeity("#origin#");
sendTweetAfterDelay(delayFunction);
};
const handleSearchTerm = tweet => {
const userHandle = tweet.user.screen_name;
const status = grammar.flatten(`#[userHandle:@${userHandle}]searchOrigin#`);
const in_reply_to_status_id = tweet.id_str;
const auto_populate_reply_metadata = true;
sendTweet({ status, in_reply_to_status_id, auto_populate_reply_metadata });
};
module.exports = { sendRandomTweet, handleReply, handleSearchTerm };