-
Notifications
You must be signed in to change notification settings - Fork 0
/
genius.ts
486 lines (366 loc) · 19.5 KB
/
genius.ts
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import encodeUrl = require('encodeurl');
// Load the full build.
var _ = require('lodash');
import axios = require('axios');
import cio = require('cheerio-without-node-native');
import Discord = require('discord.js');
import { splitMessage } from './splitMessage';
import {Util,ReactionCollector,CommandInteraction,Message} from 'discord.js'
import {decode} from 'html-entities';
import { initial } from 'lodash';
import { logger,tracer,span } from './logger';
import { asyncForEach,hexCodeToColorNumber } from './util';
import forEach = require("for-each");
import { config } from './../../config.json';
// Set config defaults when creating the instance
const geniusinstance = axios.create({
baseURL: 'https://api.genius.com',
headers: {
'Authorization': "Bearer " + config.geniusapitoken
}
});
export async function geniusSongUrlHTMLExtract(geniusSongUrl) {
try {
//stores resulting data into a variable
let { data } = await axios.get(geniusSongUrl);
//Cherio inerpretation of HTML contents
const $ = cio.load(data);
//find lyrics inside div element, trim off whitespace
//let lyrics = $('div[class="lyrics"]').text().trim();
var lyrics = ''
$('[class^=Lyrics__Container]').each((i, elem) => {
if($(elem).text().length !== 0) {
let snippet = decode($(elem).html()
.replace(/<br><br>/g, 'AdorabotTwoLine00x00')
.replace(/<br>/g, 'AdorabotOneLine00x00')
.replace(/\*/g, '\\*')
.replace(/<\/? *i[^>]*>/g, '*')
.replace(/<\/b><b>/g,"")
.replace(/<\/?b*>/g, '**')
.replace(/<(?!\s*br\s*\/?)[^>]+>/gi, '')
.replace(/AdorabotTwoLine00x00/g, '\n\n')
.replace(/AdorabotOneLine00x00/g, '\n')
// .replace(/\n\n\n/g, '\n')
.replace(/\n\n\n/g, '\n'));
// console.log($(elem).html() + " => " + snippet)
// lyrics = lyrics + $('[data-scrolltrigger-pin]').html(snippet).trim();
lyrics += snippet + "\n\n";
}
})
if (!lyrics) {return null};
return lyrics.trim();
} catch (bruhsdflj) {
console.error(bruhsdflj)
}
}
export async function geniusShowOtherSongs(response,requesterid,isInteractionOrMessage,initialRequestObject) {
try {
logger.discordInfoLogger.info("type of response.data.response.hits is " + typeof response.data.response.hits)
var embedsArrayUngroomed = response.data.response.hits.map((hit) => {
//const colorForSong = hexCodeToColorNumber(hit.result.song_art_primary_color)
return {
"title": hit.result.title,
"url": hit.result.url,
"author": {
"name" : hit.result.primary_artist.name.substring(0, 255),
"icon_url": hit.result.primary_artist.image_url
},
"thumbnail": {
"url": hit.result.song_art_image_url
}
}
})
var groupedEmbeds = _.chunk(embedsArrayUngroomed, 10);
var messageOfOtherSongs
var otherSongsResponseObject = {"embeds": groupedEmbeds[0], "content": "run `a!lyrics <song name>` to fetch the correct one! If that fails, try `a!lyrics <artist name> <song name>`"}
if(isInteractionOrMessage === 'message') {
messageOfOtherSongs = await initialRequestObject.reply(otherSongsResponseObject)
}
if(isInteractionOrMessage === 'interaction') {
messageOfOtherSongs = await initialRequestObject.followUp(otherSongsResponseObject)
}
// Create a reaction collector
//reaction.emoji.name === '🗑' && user.id === lyricsRequester
const deleteFilter = (reaction, user) => user.id === requesterid && reaction.emoji.name === "🗑" && user.id !== initialRequestObject.client;
const deleteCollector = messageOfOtherSongs.createReactionCollector({filter: deleteFilter});
deleteCollector.on('collect', async (r) => {
console.log("YUH DELETE THIS")
//const reaction = collected.first()
messageOfOtherSongs.delete().catch()
logger.discordInfoLogger.info(r);
logger.discordInfoLogger.info(`Collected ${r.emoji.name}`)});
deleteCollector.on('end', collected => logger.discordInfoLogger.info(`Collected ${collected.size} items`));
messageOfOtherSongs.react("🗑").then((reaction) => {
//lastMessageToListenTo.react('❓')
})
} catch (error) {
console.error(error)
}
}
export async function geniusRetrieveArrayOfMessageObjects(geniusQuery) {
var geniusQueryUrlEncoded = encodeUrl(geniusQuery)
var searchString = "/search" + "?" + "q=" + geniusQueryUrlEncoded
// Make a request for a user with a given ID
var response = await geniusinstance.get(searchString)
// handle success
console.log(response);
console.log(response.data)
console.log(response.data.meta.status)
console.log(response.data.response.hits[0])
if(response.data.meta.status === 200) {
//genius responded successfully
console.log(response.data.response.hits.length)
if(response.data.response.hits.length === 0) {
//messageOrInteraction.reply("I couldn't find anything, try modifying your search")
return ['error','404']
}
else {
//found something
var songLyricsHTML;
songLyricsHTML = await decideWhichGeniusUrlToUse(response.data.response);
console.log(songLyricsHTML)
logger.discordInfoLogger.info({type: 'songlyricshtml', message: songLyricsHTML})
var arrayOfTexts: Array<string> = [];
var arrayOfTextsSingleLined = await splitMessage(songLyricsHTML, {char : `\n`});;
var arrayOfTextsDoubleLined = await splitMessage(songLyricsHTML, {char : `\n\n`});
if (arrayOfTextsDoubleLined === arrayOfTextsSingleLined) {
arrayOfTexts = arrayOfTextsDoubleLined
} else {
arrayOfTexts = arrayOfTextsSingleLined
}
console.log(arrayOfTexts)
const arrayOfEmbeds = await arrayOfTexts.map(text => {
return {
"description": text
};
})
_.set(arrayOfEmbeds, '[0].title', response.data.response.hits[0].result.title_with_featured.substring(0, 255))
_.set(arrayOfEmbeds, '[0].author.name', response.data.response.hits[0].result.primary_artist.name.substring(0, 255))
_.set(arrayOfEmbeds, '[0].thumbnail.url', response.data.response.hits[0].result.song_art_image_url)
_.set(arrayOfEmbeds, '[0].url', response.data.response.hits[0].result.url)
_.set(arrayOfEmbeds, '[0].author.icon_url', response.data.response.hits[0].result.primary_artist.image_url)
const lastItem = arrayOfEmbeds[arrayOfEmbeds.length - 1]
_.set(lastItem, 'footer.text', `Powered by Genius | ${response.data.response.hits[0].result.stats.pageviews} pageviews.\nWrong song? Click ❓ to see other song options.`)
//Get Color as Hex String from Genius API response, remove the hashtag
// const extractHex = response.data.response.hits[0].result.song_art_primary_color.replace(/#/g, '')
//Add 0x and give it to parse int
// const colorNumber = parseInt("0x" + extractHex)
// console.log(colorNumber)
//For every array, set the color
// arrayOfEmbeds.map(eachEmbed => eachEmbed.color = colorNumber)
return ['success',arrayOfEmbeds,response];
}
} else {
//messageOrInteraction.reply("Something went wrong! Try again in a bit? Ooop-")
return ['error','500'];
}
};
export async function geniusLyricsFromInteraction(interaction) {
try {
var arrayOfMessagesSentForLyrics:Array<any> = [];
var lyricsRequester = interaction.user.id;
var geniusQuery = interaction.options.get('search').value
var searchingForQueryMessage;
if (geniusQuery) {
interaction.reply(`Searching for: \`${geniusQuery}\` :mag_right:`).then((searchingQueryAlert) => { searchingForQueryMessage = searchingQueryAlert})
var arrayOfMessageObjectsResponse = await geniusRetrieveArrayOfMessageObjects(geniusQuery)
if (arrayOfMessageObjectsResponse[0] === 'success') {
await asyncForEach(arrayOfMessageObjectsResponse[1], async (embed) => {
var messageObject = await interaction.channel.send({embeds: [embed]})
arrayOfMessagesSentForLyrics.push(messageObject)
})
await reactionsInitialSet(arrayOfMessagesSentForLyrics, lyricsRequester,searchingForQueryMessage,arrayOfMessageObjectsResponse[2],'interaction',interaction)
}
if (arrayOfMessageObjectsResponse[0] === 'error') {
if(arrayOfMessageObjectsResponse[1] === '500') {
interaction.followUp('Something went wrong! Try again in a bit? Ooop-')
}
if(arrayOfMessageObjectsResponse[1] === '404') {
interaction.followUp("I couldn't find anything, try modifying your search")
}
}
}
}
catch (err) {
console.error(err)
}
}
async function reactionsInitialSet(arrayOfMessagesSentForLyrics,lyricsRequester, searchingForQueryMessage, response, isInteractionOrMessage,initialRequestObject) {
console.log(arrayOfMessagesSentForLyrics)
//Basically, if the user clicks on the trash can, it deletes the embeds
//console.log(arrayOfMessagesSentForLyrics[arrayOfEmbeds.length - 1])
var lastMessageToListenTo = await arrayOfMessagesSentForLyrics[arrayOfMessagesSentForLyrics.length - 1]
// Create a reaction collector
//reaction.emoji.name === '🗑' && user.id === lyricsRequester
const deleteFilter = (reaction, user) => {
console.log(reaction)
console.log(user)
if(user.id === lyricsRequester && reaction.emoji.name === "🗑" && user.id != lastMessageToListenTo.author.id) {
console.log("delete this message, trash icon clicked")
return true;
} else {
return false;
}
}
// Create a reaction collector
//reaction.emoji.name === '🗑' && user.id === lyricsRequester
const showOtherResultsFilter = (reaction, user) => user.id === lyricsRequester && reaction.emoji.name === "❓" && user.id != lastMessageToListenTo.author.id
const deleteCollector:ReactionCollector = lastMessageToListenTo.createReactionCollector({filter: deleteFilter});
const otherResultsCollector:ReactionCollector = lastMessageToListenTo.createReactionCollector({filter: showOtherResultsFilter});
deleteCollector.on('collect', async (r) => {
console.log("YUH DELETE THIS")
//const reaction = collected.first()
arrayOfMessagesSentForLyrics.forEach(async (eachMessageSentForLyrics) => {
console.log("delete msg")
console.log(eachMessageSentForLyrics)
eachMessageSentForLyrics.delete()
})
console.log(searchingForQueryMessage)
try {searchingForQueryMessage.delete()} catch{}
logger.discordInfoLogger.info(r);
logger.discordInfoLogger.info(`Collected ${r.emoji.name}`)});
deleteCollector.on('end', collected => logger.discordInfoLogger.info(`Collected ${collected.size} items`));
otherResultsCollector.on('collect', async (r) => {
console.log("show similar results")
//const reaction = collected.first()
geniusShowOtherSongs(response,lyricsRequester,isInteractionOrMessage,initialRequestObject)
logger.discordInfoLogger.info(r);
logger.discordInfoLogger.info(`Collected ${r.emoji.name}`)})
lastMessageToListenTo.react("🗑").then((reaction) => {
lastMessageToListenTo.react('❓')
})
}
function isBadGeniusUrl(url) {
var arrayOfBadUrlsWeDontWant = [
"https://genius.com/Genius-korea-genius-korea-communitys-favorite-songs-of-2021-so-far-annotated"
]
return arrayOfBadUrlsWeDontWant.includes(url)
}
function isBadGeniusArtistUrl(url) {
var arrayOfBadArtistUrlsWeDontWant = [
'https://genius.com/artists/Genius-korea'
]
return arrayOfBadArtistUrlsWeDontWant.includes(url)
}
async function decideWhichGeniusUrlToUse(response) {
console.log(response)
//not the http response.... the genius response
return new Promise(async (resolve, reject) => {
var hitNumberToUse = decideWhichGeniusNumberToUse(response);
geniusSongUrlHTMLExtract(response.hits[hitNumberToUse].result.url).then((geniusSongUrlExtract) => {
resolve(geniusSongUrlExtract)
})
.catch((error) => {
reject(error)
})
});
}
function decideWhichGeniusNumberToUse(response) {
try {
//not the http response.... the genius response
var hitNumberCounter:number = 0;
var hitNumberToUse:number = 0;
console.log('response inside genius number to use', response)
var numberOfMaxHits = response.hits.length
while ((hitNumberCounter < numberOfMaxHits)) {
var workingOnThisResult = response.hits[hitNumberCounter].result;
console.log(workingOnThisResult)
if (isBadGeniusUrl(workingOnThisResult.url) || isBadGeniusArtistUrl(workingOnThisResult.primary_artist.url)) {
//do nothing, skip to the next hit
console.log('skip genius hit', hitNumberCounter)
if (hitNumberCounter = numberOfMaxHits - 1) {
//this entry is the last
return 0;
}
} else {
hitNumberToUse = hitNumberCounter;
console.log('use this genius hit', hitNumberCounter)
break;
}
hitNumberCounter = hitNumberCounter + 1;
}
console.log('hitNumberToUse', hitNumberToUse)
return hitNumberToUse;
} catch (
error
) {
console.error(error)
}
}
export async function geniusLyrics(message:Message,args, client) {
try {
var geniusQuery = message.content.trim().replace("a! genius","").replace("a!genius","").replace("a!lyrics","").replace("a! lyrics","").replace("a! lyric","").replace("a!lyric","").trim()
if(geniusQuery.length === 0) {
message.reply("Command: `a!lyrics <search>`")
} else {
var searchingForQueryMessage;
message.channel.send(`Searching for: \`${geniusQuery}\` :mag_right:`).then((searchingQueryAlert) => { searchingForQueryMessage = searchingQueryAlert})
var geniusQueryUrlEncoded = encodeUrl(geniusQuery)
var searchString = "/search" + "?" + "q=" + geniusQueryUrlEncoded
// Make a request for a user with a given ID
geniusinstance.get(searchString)
.then( async function (response) {
// handle success
console.log(response);
console.log(response.data)
console.log(response.data.meta.status)
// console.log(response.data.response.hits[0])
if(response.data.meta.status === 200) {
//genius responded successfully
console.log(response.data.response.hits.length)
if(response.data.response.hits.length === 0) {
message.reply("I couldn't find anything, try modifying your search")
} else {
//found something
var songLyricsHTML;
songLyricsHTML = await decideWhichGeniusUrlToUse(response.data.response);
// console.log(songLyricsHTML)
logger.discordInfoLogger.info({type: 'songlyricshtml', message: songLyricsHTML})
var arrayOfTexts = await splitMessage(songLyricsHTML);
console.log(arrayOfTexts)
const arrayOfEmbeds = await arrayOfTexts.map(text => {
return {
"description": text
};
})
var whichHitNumberToUse = decideWhichGeniusNumberToUse(response.data.response);
var resultToUse = response.data.response.hits[whichHitNumberToUse].result;
_.set(arrayOfEmbeds, '[0].title', resultToUse.title_with_featured.substring(0, 255))
_.set(arrayOfEmbeds, '[0].author.name', resultToUse.primary_artist.name.substring(0, 255))
_.set(arrayOfEmbeds, '[0].thumbnail.url', resultToUse.song_art_image_url)
_.set(arrayOfEmbeds, '[0].url', resultToUse.url)
_.set(arrayOfEmbeds, '[0].author.icon_url', resultToUse.primary_artist.image_url)
const lastItem = arrayOfEmbeds[arrayOfEmbeds.length - 1]
_.set(lastItem, 'footer.text', `Powered by Genius | ${resultToUse.stats.pageviews} pageviews.\nWrong song? Click ❓ to see other song options.`)
//Get Color as Hex String from Genius API response, remove the hashtag
// const extractHex = response.data.response.hits[0].result.song_art_primary_color.replace(/#/g, '')
//Add 0x and give it to parse int
// const colorNumber = parseInt("0x" + extractHex)
// console.log(colorNumber)
//For every array, set the color
// arrayOfEmbeds.map(eachEmbed => eachEmbed.color = colorNumber)
var lyricsRequester = message.author.id;
var arrayOfMessagesSentForLyrics:Array<any> = [];
//arrayOfMessagesSentForLyrics.push(searchingForQueryMessage)
await asyncForEach(arrayOfEmbeds, async (embed) => {
var messageObject = await message.channel.send({embeds: [embed]})
arrayOfMessagesSentForLyrics.push(messageObject)
})
await reactionsInitialSet(arrayOfMessagesSentForLyrics, lyricsRequester,searchingForQueryMessage,response,'message',message)
}
} else {
message.reply("Something went wrong! Try again in a bit? Ooop-")
}
})
.catch(function (error) {
// handle error
console.error(error);
})
.then(function () {
// always executed
});
}
} catch (errorgenius) {
console.error(errorgenius)
}
}