-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
410 lines (336 loc) · 11.8 KB
/
bot.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import * as core from '@actions/core';
import * as dotenv from 'dotenv'; dotenv.config();
import { createRestAPIClient } from 'masto';
import Keyv from '@keyvhq/core';
import { KeyvFile } from 'keyv-file';
import wordfilter from 'wordfilter';
import extraWords from './wordfilter-additions.js';
// Filter a lot of strings to improve comment selection.
wordfilter.addWords(extraWords);
// Set a high number as the ceiling for picking a random track ID. There are
// more than a billion tracks on SoundCloud; stick with a slightly smaller pool.
const maxTrackID = 784514266;
// Initiate key-value store.
let accessTokenIsSet = false;
const keyv = new Keyv({
store: new KeyvFile({
filename: 'keys.json'
})
});
// The main process. Get a comment and post it.
async function doPost() {
console.log('\n💫 🔍 🔊 💬');
const comment = await getCommentToPost();
if (comment) {
console.log('\n🔊 💬 🤖 🚀\n\nFound a usable comment, after ' + attempts + ' attempts, on this track:\n' + trackURL);
console.log('\nTrying to post "' + comment + '" to Mastodon…');
return await postToMastodon(comment);
}
else {
core.setFailed('Could not post.');
}
}
// Keep track of how many attempts were made before a usable comment was found
// and which track URL provided the comment.
let shouldTryToRequest = true;
let attempts = 0;
let trackURL;
// Post!
doPost();
/* --- */
// Get SoundCloud OAuth access token, which is necessary for API calls.
async function getSoundCloudAccessToken() {
if (accessTokenIsSet) {
return await keyv.get('access_token');
}
else if (await keyv.has('access_token')) {
// console.log('Access token already exists.');
return await refreshSoundCloudAccessToken();
}
else {
// console.log('Access token does not exist.');
return await getNewSoundCloudAccessToken();
}
}
async function getNewSoundCloudAccessToken() {
const response = await fetch(
'https://secure.soundcloud.com/oauth/token',
{
method: 'POST',
headers: {
'accept': 'application/json; charset=utf-8',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic Base64(' + process.env.SOUNDCLOUD_CLIENT_ID + ':' + process.env.SOUNDCLOUD_SECRET + ')',
},
body: new URLSearchParams({
'grant_type': 'client_credentials',
})
}
);
if ( response.status !== 200 ) {
console.error('⚠️ REQUEST FOR NEW ACCESS TOKEN FAILED:');
console.log(response);
return false;
}
const responseData = await response.json();
// console.log('Full request response:', responseData);
// console.log('Newly created tokens expire in ' + responseData.expires_in / 60 + ' minutes.');
const expirationTimeInMilliseconds = responseData.expires_in * 1000;
await keyv.set('access_token', responseData.access_token, expirationTimeInMilliseconds);
await keyv.set('refresh_token', responseData.refresh_token, expirationTimeInMilliseconds);
const accessToken = await keyv.get('access_token');
accessTokenIsSet = true;
// console.log('Getting newly created access token:', accessToken);
return accessToken;
}
async function refreshSoundCloudAccessToken() {
const response = await fetch(
'https://secure.soundcloud.com/oauth/token',
{
method: 'POST',
headers: {
'accept': 'application/json; charset=utf-8',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
'client_id': process.env.SOUNDCLOUD_CLIENT_ID,
'client_secret': process.env.SOUNDCLOUD_SECRET,
'grant_type': 'refresh_token',
'refresh_token': await keyv.get('refresh_token'),
})
}
);
const responseData = await response.json();
if ( response.status !== 200 ) {
console.error('⚠️ REQUEST TO REFRESH ACCESS TOKEN FAILED:');
console.log(response);
return false;
}
// console.log('Full request response:', responseData);
// console.log('Storing refreshed access token and new refresh token which expire in ' + responseData.expires_in/60 + ' minutes.') ;
const expirationTimeInMilliseconds = responseData.expires_in * 1000;
await keyv.set('access_token', responseData.access_token, expirationTimeInMilliseconds);
await keyv.set('refresh_token', responseData.refresh_token, expirationTimeInMilliseconds);
const accessToken = await keyv.get('access_token');
accessTokenIsSet = true;
// console.log('Getting refreshed access token:', accessToken);
return accessToken;
}
async function doSoundCloudRequest(endpoint) {
const accessToken = await getSoundCloudAccessToken();
// console.log('Doing request with token ' + accessToken)
// console.log(`Querying https://api.soundcloud.com/${endpoint}`)
if ( ! accessToken ) {
shouldTryToRequest = false;
return false;
}
const response = await fetch(
`https://api.soundcloud.com/${endpoint}`,
{
method: 'GET',
headers: {
'accept': 'application/json; charset=utf-8',
'Authorization': 'OAuth ' + accessToken,
}
}
);
if (response) {
const json = await response.json();
// Make sure the response is a JSON object and not an error message.
if (typeof json === 'object') {
return json;
}
}
}
async function getTrackThatHasComments() {
// DEBUG: Test a track we know has usable comments in English:
// return 400777524; // Has 20 comments, including ones with bad words!
// return 249409061; // Has 2 comments, both are usable.
// DEBUG: Test a track we know has a non-English comment:
// return 170359332;
// DEBUG: Test a track we know doesn't have any comments.
// return 153976760;
console.log('\n---\n');
// Click the counter!
attempts++;
// Pick a random track. The API doesn't provide for this, but SoundCloud track
// IDs are sequential! There are a lot of missing tracks (deleted, private,
// etc.), but this will, uh, *eventually* find a track with comments.
const randomTrackID = String(Math.floor(Math.random() * (maxTrackID - 1) + 1));
console.log('Attempt #' + attempts + ': Looking for a track at ID #' + randomTrackID + '…');
const response = await doSoundCloudRequest(`tracks/${randomTrackID}`);
// console.log('FULL API RESPONSE:\n', response);
if (!response) {
console.error('Bad response.');
return false;
}
// Save the track URL for later reference.
trackURL = response.permalink_url;
if (response.code === 401) {
console.error('Could not authorize.');
}
else if (response.code === 404) {
console.error('No track exists at this ID.');
}
else if (typeof response.code !== 'undefined') {
console.error('Error trying to retrieve track.');
}
else {
if (response.comment_count > 0) {
console.log('\tOK! Track has comments! Comment count: ' + response.comment_count);
return response.id;
}
else {
console.error('Track has no comments!');
}
}
};
// Get a random comment and see if it's usable.
async function getCommentFromTrack(trackID) {
// Get all of the comments for this track.
const comments = await doSoundCloudRequest('tracks/' + trackID + '/comments');
console.log('CHECKING COMMENTS FOR TRACK ID ' + trackID + '…');
// console.log('Full response of comments: ', comments);
// Exit
if (!comments) {
console.error('FAILED: Request failed or the returned comments object was empty.')
return false;
}
// Check all comments and only keep the usable ones.
const usableComments = await getUsableComments(comments);
if (usableComments) {
// Choose one of the comments at random.
const chosenCommentNumber = Math.floor(Math.random() * usableComments.length);
const chosenComment = usableComments[chosenCommentNumber];
console.log('\nCHOSE COMMENT ' + (chosenCommentNumber+1) + ' of ' + usableComments.length + ':\n\t', chosenComment)
return chosenComment;
}
else {
console.error('FAILED: None of the comments were usable.');
return false;
}
}
// Go through each comment until we find a usable one.
async function getUsableComments(comments) {
let usableComments = [];
for (let i = 0; i < comments.length; i++) {
var comment = comments[i].body.trim();
console.log('\nCHECKING COMMENT ' + ( i + 1 ) + ' OF ' + comments.length + ':', comment);
// ---
console.log('ANALYZING: Checking if too short…');
if (comment.length < 1) {
console.error('\tNOPE! Comment is too short.');
continue;
}
console.log('\tOK! Comment is not too short.');
// ---
console.log('ANALYZING: Checking if too long…');
if (comment.length > 360) {
console.error('\tNOPE! Comment is too long');
continue;
}
console.log('\tOK! Comment is not too long.');
// ---
console.log('ANALYZING: Checking for bad words…');
if (wordfilter.blacklisted(comment)) {
console.error('\tNOPE! Comment is a reply, contains a bad word, or looks like spam.');
continue;
}
console.log('\tOK! Comment does not contain any bad words');
// ---
console.log('ANALYZING: Checking if written in English…');
if (await checkIfEnglish(comment)) {
console.log('\tOK! Comment appears to be written in English.');
console.log('SUCCESS: All checks passed! Comment is usable: "' + comment + '"');
usableComments.push(comment);
}
else {
console.error('\tNOPE! Comment appears not to be written in English.')
}
}
if (usableComments.length > 0) {
return usableComments;
}
}
async function checkIfEnglish(comment) {
const response = await fetch(
'https://translation.googleapis.com/language/translate/v2/detect?key=' + process.env.GOOGLE_TRANSLATE_API_KEY + '&q=' + comment,
{
method: 'POST',
headers: {
'accept': 'application/json; charset=utf-8',
}
}
);
const responseData = await response.json();
const detectedLanguage = responseData.data.detections[0][0].language;
// console.log('Detections:', responseData.data.detections);
// console.log('Detected language: ' + detectedLanguage);
if (detectedLanguage === 'en') {
return comment;
}
}
async function getCommentToPost() {
if (!shouldTryToRequest) {
console.error('Not trying anymore.');
return false;
}
const trackID = await getTrackThatHasComments();
if (trackID) {
const comment = await getCommentFromTrack(trackID);
if (comment) {
return comment;
}
else {
// Try again if we failed to find a usable comment.
console.error('FAILED.');
return getCommentToPost();
}
}
else {
// Try again if we failed to find a track with comments.
console.error('FAILED.');
return getCommentToPost();
}
}
// Post the comment.
let attemptsToPost = 0;
async function postToMastodon(thePostToPost) {
attemptsToPost++;
if (thePostToPost) {
// console.log('NOW ATTEMPTING TO POST:', thePostToPost);
// Access Mastodon.
const masto = createRestAPIClient({
url: 'https://botsin.space',
accessToken: process.env.MASTODON_ACCESS_TOKEN,
});
// console.log('LOGGING IN TO MASTODON:', masto);
if (masto) {
const status = await masto.v1.statuses.create({
status: thePostToPost,
visibility: 'public'
});
// console.log('RESULT OF ATTEMPT TO POST:', status);
if (status.id) {
console.error('\n✅ SUCCESSFULLY POSTED TO MASTODON:', status.url);
}
else {
console.error('ERROR POSTING:', status);
}
}
else {
// If we haven't already tried ten times, wait a bit and try again.
if (attemptsToPost < 10) {
setTimeout(() => {
postToMastodon(thePostToPost);
}, 5000);
}
else {
console.error('ERROR: Could not post to Mastodon. Try again later.');
}
}
}
else {
console.error('ERROR: No comment retrieved; cannot post.');
}
}