-
Notifications
You must be signed in to change notification settings - Fork 1
/
gbsfm.py
781 lines (747 loc) · 41.5 KB
/
gbsfm.py
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#!/usr/bin/env python
import MySQLdb
import config
import urllib.request
import time
import base64
import urllib.parse
import json
import yt_dlp
import os
#Defining database connection
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
#Check if a string can be converted into a float
def check_float(potential_float):
try:
float(potential_float)
return True
except ValueError:
return False
#Function to add a song to the playlist by song id. This only works for authed users
def gbsfm_addsong( userid, apikey, songid ):
addsong_url = config.gbsfm_baseurl + "/api/add?userid=" + str(userid) + "&key=" + apikey + "&songid=" + str(songid)
try:
addsong_request = urllib.request.urlopen(addsong_url)
addsong_result = addsong_request.read()
addsong_request.close()
addsong_result = addsong_result.decode("utf8")
add_success = 1
msg = "Success!"
except urllib.error.HTTPError as error:
errmsg = error.read()
msg = "Sorry, " + errmsg.decode('utf-8')
add_success = 0
return add_success, msg
#Gets the currently playing song
def gbsfm_nowplaying():
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT \
playlist_playlistentry.song_id, \
playlist_song.title AS songtitle, \
playlist_artist.`name` AS artist, \
playlist_album.`name` as album \
FROM \
playlist_playlistentry \
INNER JOIN playlist_song ON playlist_playlistentry.song_id = playlist_song.id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
WHERE \
playlist_playlistentry.playing = 1")
db.commit()
q_nowplaying = query.fetchone()
return q_nowplaying[0], q_nowplaying[1], q_nowplaying[2], q_nowplaying[3]
#Returns song_id, songtitle, artist, album
#Adds the currently playing song to users faves
def gbsfm_addfav( user_gbsfmid, user_longuid ):
user_longuid = "<@" + str(user_longuid) + ">"
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT \
playlist_playlistentry.song_id, \
playlist_song.title AS songtitle, \
playlist_artist.`name` AS artist, \
playlist_album.`name` as album \
FROM \
playlist_playlistentry \
INNER JOIN playlist_song ON playlist_playlistentry.song_id = playlist_song.id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
WHERE \
playlist_playlistentry.playing = 1")
db.commit()
q_nowplaying = query.fetchone()
query = db.cursor()
query.execute ("select id from playlist_userprofile where user_id = %s", (user_gbsfmid,))
db.commit()
q_profileid = query.fetchone()
query = db.cursor()
str_response = "Added _" + q_nowplaying[1] + "_ by _" + q_nowplaying[2] + "_ from _" + q_nowplaying[3] + "_ to the favourite-list of " + user_longuid
try:
query.execute ("insert into playlist_userprofile_favourites (userprofile_id, song_id) values (%s, %s)", (q_profileid[0], q_nowplaying[0]))
db.commit()
except MySQLdb._exceptions.IntegrityError:
str_response = "Dong already in your favourite-list!"
return str_response
#Function to check if a user has authed. Input their long discord id
def gbsfm_isauthed( discordid_long ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT * from discord_auth where discord_id_long = %s limit 1", (discordid_long,))
db.commit()
queryresult = query.fetchone()
if queryresult is None:
authbool = 0
user_gbsfmid = 0
user_apikey = 0
user_shortuid = 0
user_longuid = 0
return authbool, user_gbsfmid, user_apikey, user_shortuid, user_longuid
else:
authbool = 1
user_gbsfmid = str(queryresult[3])
user_apikey = str(queryresult[2])
user_shortuid = str(queryresult[1])
user_longuid = str(queryresult[4])
return authbool, user_gbsfmid, user_apikey, user_shortuid, user_longuid
#Gets user stats
def gbsfm_stoats( user_gbsfmid ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT count(*) cnt FROM playlist_song ps LEFT OUTER JOIN playlist_oldplaylistentry ope ON ps.id = ope.song_id WHERE ps.uploader_id = %s AND ope.id IS NULL;", (user_gbsfmid,))
q_unplayed = query.fetchone()
str_response = "You have " + str(q_unplayed[0]) + " unplayed dongs.\n"
query.execute ("SELECT count(*) cnt FROM playlist_song WHERE banned = 1 AND uploader_id = %s", (user_gbsfmid,))
q_banned = query.fetchone()
str_response = str_response + "You have " + str(q_banned[0]) + " banned dongs.\n"
query.execute ("SELECT ps.id id, concat(pa.name, ' - ', ps.title) dong, COUNT( * ) cnt \
FROM playlist_song ps \
INNER JOIN playlist_artist pa ON ps.artist_id = pa.id \
INNER JOIN playlist_oldplaylistentry ope ON ope.song_id = ps.id \
WHERE ope.adder_id = %s \
GROUP BY id, dong \
ORDER BY cnt DESC \
LIMIT 0, 10;", (user_gbsfmid,))
q_mostadded = query.fetchone()
str_response = str_response + "__Most songs added by you:__\n"
while q_mostadded is not None:
str_response = str_response + str(q_mostadded[1]) + " (" + str(q_mostadded[2]) + ")\n"
q_mostadded = query.fetchone()
query.execute ("SELECT concat(pa.name, ' - ', ps.title) dong, au.username adder \
FROM playlist_song ps \
INNER JOIN playlist_artist pa \
ON ps.artist_id = pa.id \
INNER JOIN playlist_oldplaylistentry ope \
ON ps.id = ope.song_id \
INNER JOIN auth_user au \
ON au.id = ope.adder_id \
WHERE ps.uploader_id = %s AND ope.adder_id <> %s \
ORDER BY ope.addtime DESC \
LIMIT 0, 10", (user_gbsfmid,user_gbsfmid))
q_addedups = query.fetchone()
str_response = str_response + "__Most recent additions of your uploads (not by you)__\n"
while q_addedups is not None:
str_response = str_response + str(q_addedups[0]) + " (" + str(q_addedups[1]) + ")\n"
q_addedups = query.fetchone()
return str_response
#Query the database for list of songs
def gbsfm_query( query_type, user_gbsfmid, querystring ):
#print(query_type, user_gbsfmid, querystring)
unformatted = ['sup', 'dongid', 'dongid24no', 'user', 'artistid']
if not query_type in unformatted:
querystring = '%{}%'.format(querystring)
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
if query_type == 'aup': #Any unplayed song
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, playlist_album.`name` \
FROM playlist_song AS ps \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON ps.album_id = playlist_album.id \
WHERE NOT \
EXISTS (SELECT * FROM playlist_oldplaylistentry \
WHERE song_id = ps.id) \
AND ps.banned = 0 \
order by rand() limit 10")
elif query_type == 'random': #Any random song
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, playlist_album.`name` \
FROM playlist_song AS ps \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON ps.album_id = playlist_album.id \
order by rand() limit 10")
elif query_type == 'unplayed': #Any song uploaded by the user that is unplayed
query.execute ("SELECT playlist_song.id, playlist_artist.`name` as artist, playlist_song.title, playlist_album.`name` as album \
FROM playlist_song \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
WHERE playlist_song.uploader_id = %s \
and playlist_song.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
and playlist_song.id not in (SELECT song_id FROM \
playlist_oldplaylistentry WHERE song_id = playlist_song.id) \
AND playlist_song.banned = 0 \
order by rand() limit 10", [user_gbsfmid])
elif query_type == 'genre': #Any song with specified genre
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, playlist_album.`name` \
FROM playlist_song AS ps \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON ps.album_id = playlist_album.id \
WHERE ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
AND ps.genre like %s \
AND ps.banned = 0 \
ORDER BY rand() LIMIT 10", (querystring,))
elif query_type == 'userany': #Any song uploaded by the user
query.execute ("SELECT playlist_song.id, playlist_artist.`name` as artist, playlist_song.title, playlist_album.`name` as album \
FROM playlist_song \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
WHERE playlist_song.uploader_id = %s \
and playlist_song.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
AND playlist_song.banned = 0 \
order by rand() limit 10", [user_gbsfmid])
elif query_type == 'faves': #Any favorite of the user
query.execute ("SELECT playlist_userprofile_favourites.song_id, \
playlist_artist.`name`, playlist_song.title, \
playlist_album.`name`FROM playlist_userprofile \
INNER JOIN playlist_userprofile_favourites ON playlist_userprofile_favourites.userprofile_id = playlist_userprofile.id \
INNER JOIN playlist_song ON playlist_userprofile_favourites.song_id = playlist_song.id AND playlist_userprofile_favourites.song_id = playlist_song.id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
WHERE playlist_userprofile.user_id = %s \
and playlist_userprofile_favourites.song_id not in \
(SELECT playlist_oldplaylistentry.song_id \
FROM playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
order by RAND()", [user_gbsfmid])
elif query_type == 'album': #Query by album
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.album_id = pa.id and pa.name LIKE %s \
and ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
order by RAND() limit 10", (querystring,))
elif query_type == 'artist': #Query by artist
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.album_id = pa.id and playlist_artist.name LIKE %s \
and ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
order by RAND() limit 10", (querystring,))
elif query_type == 'artistid': #Query by artistid
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.album_id = pa.id and playlist_artist.id = %s \
and ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
order by RAND() limit 10", (querystring,))
elif query_type == 'title': #Query by title
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.album_id = pa.id and ps.title LIKE %s \
and ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
and ps.id not in (select song_id from playlist_playlistentry) \
order by RAND() limit 10", (querystring,))
elif query_type == 'sup': #Get short dongs
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.length < %s \
and ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
and ps.id not in (select song_id from playlist_playlistentry) \
order by RAND() limit 10", [querystring])
elif query_type == 'user': #Add by username
query.execute ("SELECT playlist_song.id, playlist_artist.`name`, playlist_song.title, playlist_album.`name` as album \
FROM playlist_song \
INNER JOIN auth_user ON auth_user.id = playlist_song.uploader_id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
where playlist_song.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
and auth_user.username = %s \
order by RAND() limit 10", [querystring])
elif query_type == 'dongid': #Add by dong id
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.id = %s \
and ps.id not in (select song_id from playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
limit 1", [querystring])
elif query_type == 'dongid24no': #Add by dong id
query.execute ("SELECT ps.id, playlist_artist.`name`, ps.title, pa.`name` AS album \
FROM playlist_song AS ps \
INNER JOIN playlist_album AS pa ON ps.album_id = pa.id \
INNER JOIN playlist_artist ON ps.artist_id = playlist_artist.id \
WHERE ps.id = %s \
limit 1", [querystring])
elif query_type == 'lowrating': #Add by low rating
query.execute ("SELECT songid, artist, title, album FROM songs_rated WHERE songs_rated.score < 2 and no_votes > 5 order by rand() limit 10")
elif query_type == 'highrating': #Add by high rating
query.execute ("SELECT songid, artist, title, album FROM songs_rated WHERE songs_rated.score > 4.8 and no_votes > 5 order by rand() limit 10")
elif query_type == 'otherfav': #Add by high rating
discordid_fromuser = querystring[3:21]
query.execute ("select user_id from discord_auth where discord_id_long = %s", [discordid_fromuser])
query.execute ("SELECT playlist_userprofile_favourites.song_id, \
playlist_artist.`name`, playlist_song.title, \
playlist_album.`name`FROM playlist_userprofile \
INNER JOIN playlist_userprofile_favourites ON playlist_userprofile_favourites.userprofile_id = playlist_userprofile.id \
INNER JOIN playlist_song ON playlist_userprofile_favourites.song_id = playlist_song.id AND playlist_userprofile_favourites.song_id = playlist_song.id \
INNER JOIN playlist_artist ON playlist_song.artist_id = playlist_artist.id \
INNER JOIN playlist_album ON playlist_song.album_id = playlist_album.id \
WHERE playlist_userprofile.user_id = %s \
and playlist_userprofile_favourites.song_id not in \
(SELECT playlist_oldplaylistentry.song_id \
FROM playlist_oldplaylistentry \
WHERE playlist_oldplaylistentry.playtime > NOW()-INTERVAL 7*24 HOUR) \
order by RAND()", [query.fetchone()[0]])
db.commit()
#print(query.rowcount)
returnlist = []
if query.rowcount > 0:
row = query.fetchone()
while row is not None:
returnlist.append(row)
row = query.fetchone()
else:
print('Shiiiiiit')
#print(returnlist)
return returnlist #Returns (id, artist, songtitle, album) x up to 10 in a list
# -------------------------------------------------------------------
#Play
def gbsfm_play( query_type, user_gbsfmid, user_apikey, user_longuid, query_string ):
songlist = gbsfm_query(query_type, user_gbsfmid, query_string)
if len(songlist) > 0:
for song in songlist:
#Try to add the songs in the list, bail when one succeeds
add_success, add_msg = gbsfm_addsong(user_gbsfmid, user_apikey, song[0])
if add_success == 1:
str_addmessage = "Added _" + song[2] + "_ by _" + song[1] + "_ from _" + song[3] + "_ for " + user_longuid
added_songid = song[0]
break
if add_success == 0 and add_msg == "Sorry, you already have too many songs on the playlist":
str_addmessage = add_msg
added_songid = 0
break
else:
add_success = 0
str_addmessage = "Your query didn't return any results."
added_songid = 0
return add_success, str_addmessage, added_songid
#Adds a message to the list of botmessages one can react to
def gbsfm_add_botmessage( msgid, added_songid ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("insert into discord_botmessages (message_id, song_id) values (%s, %s)", (msgid, added_songid))
db.commit()
def gbsfm_auth( authid, discord_id, discord_id_long ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT auth_user.username, playlist_userprofile.api_key, playlist_userprofile.user_id \
FROM auth_user INNER JOIN playlist_userprofile ON auth_user.id = playlist_userprofile.user_id \
WHERE playlist_userprofile.api_key = %s", (authid,))
db.commit()
queryresult = query.fetchone()
if queryresult is None:
auth_success = 0
auth_message = "Wrong auth id!"
else:
sql_discord_id = str(discord_id)
sql_api_key = str(authid)
sql_user_id = queryresult[2]
sql_discord_id_long = str(discord_id_long)
query = db.cursor()
query.execute ("insert into discord_auth (discord_id, api_key, user_id, discord_id_long) values (%s, %s, %s, %s) \
ON DUPLICATE KEY UPDATE discord_id=%s, api_key=%s, user_id=%s, discord_id_long=%s",(sql_discord_id,sql_api_key,sql_user_id,sql_discord_id_long,sql_discord_id,sql_api_key,sql_user_id,sql_discord_id_long))
db.commit()
auth_success = 1
auth_message = "Correct auth id! You are now authed from " + str(discord_id)
return auth_success, auth_message
#Give tokens
def gbsfm_givetokens( token_recipient, token_recipient_short, token_amount ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT discord_auth.user_id, playlist_userprofile.tokens \
FROM discord_auth INNER JOIN playlist_userprofile ON \
discord_auth.user_id = playlist_userprofile.user_id WHERE \
discord_auth.discord_id_long = %s", (str(token_recipient),))
db.commit()
q_tokens = query.fetchone()
print(q_tokens)
token_current = q_tokens[1]
token_gbsfmid = q_tokens[0]
token_new = int(token_amount) + int(token_current)
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("update playlist_userprofile set tokens = %s where user_id = %s", (token_new, token_gbsfmid))
db.commit()
token_response = "You gave <@" + str(token_recipient) + "> " + str(token_amount) + " token(s)"
return token_response
#Check tokens
def gbsfm_gettokens( user_gbsfmid, user_longuid ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT playlist_userprofile.tokens FROM playlist_userprofile WHERE playlist_userprofile.user_id = %s", (user_gbsfmid,))
q_tokens = query.fetchone()
token_response = user_longuid + " has " + str(q_tokens[0]) +" tokens\n"
return token_response
#Play jingle
def gbsfm_jingle( user_longuid ):
add_success, str_addmessage, added_songid = gbsfm_play('artist', config.shoes_id, config.shoes_apikey, user_longuid, 'Jingles')
return add_success, str_addmessage, added_songid
#Check when your next dong is playing
def gbsfm_when ( user_gbsfmid ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("SELECT playlist_playlistentry.song_id, playlist_playlistentry.adder_id, \
playlist_playlistentry.playtime, playlist_playlistentry.playing, \
playlist_song.length FROM playlist_playlistentry \
INNER JOIN playlist_song ON playlist_playlistentry.song_id = playlist_song.id")
db.commit()
playlistentries = query.fetchall()
until_next = 0
dongfound = 0
result = 0
if len(playlistentries) < 1:
result = 1
responsemessage = "There's only one dong in the playlist..."
else:
for playlistnumber in playlistentries:
when_songid = playlistnumber[0]
when_adder_id = playlistnumber[1]
when_playtime = playlistnumber[2]
when_playing = playlistnumber[3]
when_length = playlistnumber[4]
if str(when_adder_id) == str(user_gbsfmid) and str(when_playing) == "1":
result = 2
break
if str(when_playing) == "1":
when_playtime_current_playing = playlistnumber[4]
when_playtime = time.mktime(when_playtime.timetuple())
when_playtime_now = int(time.time()) - 3600 #DST bodge
when_playtime_diff = when_playtime_now - when_playtime
when_playtime_diff = when_length - when_playtime_diff
if when_playtime_diff > 1:
until_next = until_next + when_playtime_diff
continue
if str(when_adder_id) == str(user_gbsfmid):
dongfound = 1
break
until_next = until_next + int(when_length)
if result == 2:
responsemessage = "The dong currently playing is yours..."
elif dongfound == 1 and result == 0:
responsemessage = time.strftime('%H:%M:%S', time.gmtime(int(until_next)))
responsemessage = "There is about " + str(responsemessage) +" until your next dong"
result = 4
if until_next < when_playtime_current_playing:
result = 3
responsemessage = "Your dong is next, hurry!" + "\n" + responsemessage
else:
result = 5
responsemessage = "No song in the playlist!"
return result, responsemessage # result = 1=only one song, 2=dong playing is mine, 3=dong is next, 4=normal, 5=no song
def gbsfm_vote ( vote, user_gbsfmid, user_apikey ):
votesuccess = False
extra_message = 0
votemacrolist_5 = ["gold", "awesome", "amazing", "boners", "excellent", "lovely", ":rock:", "metal", \
"smashing", "super", "great", "oldskool", "blood", "humppa", "classical"]
votemacrolist_49 = ["eurocheese"]
votemacrolist_48 = ["mastersord"]
votemacrolist_42 = ["alita"]
votemacrolist_4 = ["good"]
votemacrolist_40 = ["nihiliste"]
votemacrolist_39 = ["merijn"]
votemacrolist_37 = ["icetraigh"]
votemacrolist_3 = ["average", "ok", "mediocre", "meh"]
votemacrolist_27 = ["e"]
votemacrolist_2 = ["bad", "poor"]
votemacrolist_18 = []
votemacrolist_16 = ["phi"]
votemacrolist_11 = ["carmi", "wynorski"]
votemacrolist_1 = ["terrible", "awful", "dreadful", "cancer", "aids", "anime", "pepper"]
votemacrolist_techno = ["technotechnotechnotechno"]
votemacrolist_buttes = ["buttes"]
votemacrolist_hitler = ["hitler", "goering", "himmler", "goebbels", "aryansupremacy"]
votemacrolist_pi = ["pi"]
votemacrolist_tory = ["tory"]
votemacrolist_weed = ["weed"]
votemacrolist = votemacrolist_5 + votemacrolist_49 + votemacrolist_48 + votemacrolist_42 + votemacrolist_4 \
+ votemacrolist_40 + votemacrolist_39 + votemacrolist_37 + votemacrolist_3 + votemacrolist_27 \
+ votemacrolist_2 + votemacrolist_18 + votemacrolist_16 + votemacrolist_11 + votemacrolist_1 \
+ votemacrolist_techno + votemacrolist_buttes + votemacrolist_pi \
+ votemacrolist_tory + votemacrolist_weed
#If the length of the votesting is 1, and it's a number, it's a normal vote, no macro
if len(vote) == 1 and check_float(vote[0]):
votemode = 'normal'
#If the length of the votesting is 1, and it's not a number, it's a normal vote, with macro
elif len(vote) == 1 and not check_float(vote[0]):
votemode = 'normal_macro'
#If the length is 2 and the second argument is a number, it's and advanced vote, no macro
elif len(vote) == 2 and check_float(vote[1]) and vote[0][0] == '#':
votemode = 'advanced'
#If the length is 2 and the second argument is not a number, it's and advanced vote, with macro
elif len(vote) == 2 and not check_float(vote[1]):
votemode = 'advanced_macro'
#Vote doesn't comply, return errormessage
else:
print('fart')
#Get currently playing song
song_id, songtitle, artist, album = gbsfm_nowplaying()
if votemode == 'normal':
votenumber = vote[0]
votesuccess = True
elif votemode == 'advanced':
votenumber = vote[1]
song_id = vote[0][1:]
song = gbsfm_query('dongid24no', 0, song_id)
votesuccess = True
vote = vote[1]
elif votemode == 'normal_macro' or votemode == 'advanced_macro':
if votemode == 'advanced_macro':
song_id = vote[0][1:]
song = gbsfm_query('dongid24no', 0, song_id)
vote = vote[1]
else:
vote = vote[0]
if any(vote == vmacro for vmacro in votemacrolist):
if any(vote == vmacro for vmacro in votemacrolist_5):
votenumber = 5
elif any(vote == vmacro for vmacro in votemacrolist_49):
votenumber = 4.9
elif any(vote == vmacro for vmacro in votemacrolist_48):
votenumber = 4.8
elif any(vote == vmacro for vmacro in votemacrolist_42):
votenumber = 4.2
elif any(vote == vmacro for vmacro in votemacrolist_4):
votenumber = 4
elif any(vote == vmacro for vmacro in votemacrolist_40):
votenumber = 4.0
elif any(vote == vmacro for vmacro in votemacrolist_39):
votenumber = 3.9
elif any(vote == vmacro for vmacro in votemacrolist_37):
votenumber = 3.8
elif any(vote == vmacro for vmacro in votemacrolist_3):
votenumber = 3
elif any(vote == vmacro for vmacro in votemacrolist_27):
votenumber = 2.7
elif any(vote == vmacro for vmacro in votemacrolist_2):
votenumber = 2
elif any(vote == vmacro for vmacro in votemacrolist_18):
votenumber = 1.8
elif any(vote == vmacro for vmacro in votemacrolist_16):
votenumber = 1.6
elif any(vote == vmacro for vmacro in votemacrolist_11):
votenumber = 1.1
elif any(vote == vmacro for vmacro in votemacrolist_1):
votenumber = 1
elif any(vote == vmacro for vmacro in votemacrolist_techno):
votenumber = 5
extra_message = "Techno! Techno! Techno! Techno!"
elif any(vote == vmacro for vmacro in votemacrolist_buttes):
votenumber = 1.1
extra_message = "Buttes."
elif any(vote == vmacro for vmacro in votemacrolist_hitler):
votenumber = 1.8
extra_message = "shoes heil!"
elif any(vote == vmacro for vmacro in votemacrolist_pi):
voterandom = random.randint(1,101)
if 1 <= voterandom <= 25:
votenumber = 3
if 26 <= voterandom <= 50:
votenumber = 3.1
if 51 <= voterandom <= 75:
votenumber = 3.14159
if 76 <= voterandom <= 90:
votenumber = 3.2
if 91 <= voterandom <= 100:
votenumber = 4
elif any(vote == vmacro for vmacro in votemacrolist_tory):
votenumber = 2
extra_message = "Tory scum!"
elif any(vote == vmacro for vmacro in votemacrolist_weed):
votenumber = 4.20
votesuccess = True
else:
returnmessage = "That's not a valid vote-macro"
if votesuccess:
if votemode == 'normal':
returnmessage = "You voted " + str(votenumber) + " on " + songtitle + " by " + artist
if votemode == 'normal_macro':
returnmessage = "You voted " + vote + " (" + str(votenumber) + ") " + " on " + songtitle + " by " + artist
if votemode == 'advanced' or votemode == 'advanced_macro':
returnmessage = "You voted " + str(vote) + " on " + song[0][2] + " by " + song[0][1]
print(returnmessage)
if votemode == 'advanced_macro':
returnmessage = "You voted " + str(vote) + " (" + str(votenumber) + ") " + " on " + song[0][2] + " by " + song[0][1]
if len(str(extra_message)) > 1:
returnmessage = extra_message + "\n" + returnmessage
voteurl = config.gbsfm_baseurl + '/api/vote?userid=' + str(user_gbsfmid) + '&key=' + user_apikey + '&songid=' + str(song_id) + '&vote=' + str(votenumber)
vote_request = urllib.request.urlopen(voteurl)
vote_request.close()
else:
returnmessage = "Voting failed. What did you dooooooo?!"
return votesuccess, returnmessage
def gbsfm_streampw( getorset ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
if getorset == 'get':
query = db.cursor()
query.execute ("select `value` from playlist_settings WHERE `key`='stream_password'")
db.commit()
streampw = query.fetchone()
passwordstring = 'The livestream-password is: ' + streampw[0]
elif getorset == 'set':
streampw = streampw.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(10))
query = db.cursor()
query.execute ("UPDATE playlist_settings SET `value`=%s WHERE `key`='stream_password'", (streampw,))
db.commit()
passwordstring = 'The livestream-password has been set to: ' + streampw
return passwordstring
def gbsfm_reactionvote( vote_emoji, message_id, discord_userid_long ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
votelist_1 = ["6e155540116de9dcf5f233191cce07b0"]
votelist_2 = ["fd1e1b476fadbaa7125fa9d64b3b7629"]
votelist_3 = ["66575d5498439e1c15cb5023c22eb808"]
votelist_4 = ["c97aa9c5a6ba44498d146559094bb14c"]
votelist_5 = ["736fe4cd17cf2ee08f96c6b607134342"]
react_fav = ["6606f5c3e3a2abce20a8e7e0b5060ae1"]
votelist_combined = votelist_1 + votelist_2 + votelist_3 + votelist_4 + votelist_5 + react_fav
votenumber = 0
#Check if the message is voteable
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("select * from discord_botmessages where message_id = %s limit 1", [message_id])
db.commit()
queryresult = query.fetchone()
if queryresult == None:
queryresult = 'none'
voteresult = 'unvoteable'
votestring = 'n/a'
elif len(queryresult) == 3:
if any(vote_emoji == emoji for emoji in votelist_combined):
authbool, user_gbsfmid, user_apikey, user_shortuid, user_longuid = gbsfm_isauthed(discord_userid_long)
if authbool == 0:
voteresult = 'not_authed'
votestring = 'Not authed!'
elif authbool == 1:
user_longuid = "<@" + str(discord_userid_long) + ">"
if any(vote_emoji == emoji for emoji in votelist_1):
votenumber = 1
if any(vote_emoji == emoji for emoji in votelist_2):
votenumber = 2
if any(vote_emoji == emoji for emoji in votelist_3):
votenumber = 3
if any(vote_emoji == emoji for emoji in votelist_4):
votenumber = 4
if any(vote_emoji == emoji for emoji in votelist_5):
votenumber = 5
if any(vote_emoji == emoji for emoji in react_fav):
votenumber = 'fav'
else:
voteresult = 'unused_emoji'
votestring = 'n/a'
if not queryresult == 'none':
song = gbsfm_query('dongid24no', 0, queryresult[2])
if votenumber == 'fav':
favurl = config.gbsfm_baseurl + "/api/favourite?userid=" + str(user_gbsfmid) + "&key=" + user_apikey + "&songid=" + str(queryresult[2])
try:
fav_request = urllib.request.urlopen(favurl)
fav_result = fav_request.read()
fav_request.close()
fav_result = fav_result.decode("utf8")
voteresult = 'fav'
votestring = user_longuid + ' added ' + song[0][2] + ' by ' + song[0][2] + ' to favourites'
except urllib.error.HTTPError as error:
print(error.code)
print(error.read())
voteresult = 'error'
votestring = 'Something went wrong'
elif votenumber > 0:
voteurl = config.gbsfm_baseurl + "/api/vote?userid=" + str(user_gbsfmid) + "&key=" + user_apikey + "&songid=" + str(queryresult[2]) + "&vote=" + str(votenumber)
try:
vote_request = urllib.request.urlopen(voteurl)
vote_result = vote_request.read()
vote_request.close()
vote_result = vote_result.decode("utf8")
query = db.cursor()
query.execute ("SELECT SQL_NO_CACHE cast(avg(score) AS decimal(5,2)) from playlist_rating where song_id = %s", [queryresult[2]])
db.commit()
avgvote = query.fetchone()
print(avgvote[0])
voteresult = 'vote'
votestring = user_longuid + ' voted ' + str(votenumber) + ' on ' + song[0][2] + ' by ' + song[0][2] + '. It now has an average vote of ' + str(avgvote[0])
except urllib.error.HTTPError as error:
print(error.code)
print(error.read())
voteresult = 'error'
votestring = 'Something went wrong'
return voteresult, votestring
#Downloads song from youtube, then uploads it to gbsfm
def gbsfm_ytdlsong( userid, apikey, youtubeclip ):
upload_url = "https://gbs.fm/api/upload?userid=" + str(userid) + '&key=' + apikey
headers = {'Accept': '*/*', 'Content-Type': 'application/x-www-form-urlencoded'}
tempfilename = '/tmp/ytdlfile.' + str(int(time.time()))
ydl_opts = {
'outtmpl': tempfilename,
'format': 'bestaudio/best',
'cookiefile': '/shoes/cookies.txt'
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(youtubeclip, download=True)
cliptitle = info['title'] + '.' + info['ext']
cliplength = info['duration']
with open(tempfilename, "rb") as f:
input = f.read()
filedata = base64.urlsafe_b64encode(input).decode('ascii')
filedata = dict(filename=cliptitle, filecontents=filedata)
filedata = urllib.parse.urlencode(filedata)
filedata = filedata.encode('utf-8')
request = urllib.request.Request(upload_url, data=filedata, method='POST')
try:
urllib.request.urlopen(request)
dl_success = 1
msg = "Song uploaded to the site, I hope to god it's a good one..."
except urllib.error.HTTPError as error:
errmsg = error.read()
msg = "Sorry, " + errmsg.decode('utf-8')
dl_success = 0
finally:
os.remove(tempfilename)
return dl_success, msg
def gbsfm_undo( user_gbsfmid, user_longuid ):
db = MySQLdb.connect(host=config.mysql_dbhost, user=config.mysql_user, passwd=config.mysql_passwd, db=config.mysql_db, charset="utf8")
query = db.cursor()
query.execute ("select * from playlist_playlistentry where adder_id = %s order by id desc", [user_gbsfmid])
db.commit()
queryresult = query.fetchone()
print(queryresult)
if queryresult == None:
msg = user_longuid + "does not have any upcoming songs on the playlist."
elif queryresult[5] == 1:
msg = user_longuid + ' tried to delete the \"song\" you are currently listening to, too late!'
elif queryresult[7] == 1:
query = db.cursor()
query.execute ("delete from playlist_playlistentry where id = %s", [queryresult[0]])
db.commit()
query = db.cursor()
query.execute ("update playlist_userprofile set tokens = tokens + 1 where user_id = %s", [user_gbsfmid])
db.commit()
msg = 'The most recent song added by ' + user_longuid + ' to the playlist was deleted and the token was returned.'
else:
query = db.cursor()
query.execute ("delete from playlist_playlistentry where id = %s", [queryresult[0]])
db.commit()
msg = 'The most recent song added by ' + user_longuid + ' to the playlist was deleted'
return msg