-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
1911 lines (1633 loc) · 56.3 KB
/
data.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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import mysql.connector as conn
import mysql.connector.errors
import threading
import datetime
import os
from setup import get_directories_path
with open(get_directories_path) as direc:
direc_dict = json.load(direc)
with open(direc_dict["apis"], 'r') as apis:
apis_dict = json.load(apis)
with open(direc_dict["cache_variables"]) as cache:
cache_dict = json.load(cache)
with open(direc_dict["mods"], 'r') as mods:
mods_dict = json.load(mods)
insta_settings_file = direc_dict["insta_file_path"]
default_prefix = apis_dict["command_prefix"]
async def write_cache():
x = 0
while True:
with open(direc_dict["cache_variables"], 'w') as cash:
json.dump(cache_dict, cash, indent=4)
x += 60
if x % 3600 == 0:
dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Written cache variables for 1 hour at {dt}.")
await asyncio.sleep(20)
def check_user_is_mod(ctx):
return find_moderator(ctx.user.id)
def check_user_is_owner(ctx):
if ctx.user.id in mods_dict["owners"]:
return True
else:
return False
database = apis_dict["database_name"]
username = apis_dict["database_user"]
password = apis_dict["database_password"]
db = conn.connect(
host='localhost',
user=username,
passwd=password,
database=database,
buffered=True
)
# --- MAKE DATABASE BACKUP ON DAY CYCLES --- #
def backup_database():
today = datetime.date.today()
os.system(f'sudo mysqldump -u root botdatabase > ./backupgfys/backup-file-{today}.sql')
print(f"Database backed up on {today} at {datetime.datetime.now()}")
threading.Timer(86400.0, backup_database).start()
backup_database()
# --- CUSTOM COMMANDS --- #
def add_command(name, link, added_by):
"""Add a new custom command to the database."""
cursor = db.cursor()
sql = "INSERT INTO custom_commands(CommandName, Command, AddedBy) VALUES (%s, %s, %s);"
val = (name, link, added_by)
cursor.execute(sql, val)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def get_commands(near=None):
"""Returns a dictionary of all commands in the database."""
cursor = db.cursor()
if near:
sql = """SELECT CommandName, Command FROM custom_commands
WHERE CommandName LIKE CONCAT(%s, '%')
ORDER BY CommandName;"""
cursor.execute(sql, (near,))
else:
sql = "SELECT CommandName, Command FROM custom_commands ORDER BY CommandName;"
cursor.execute(sql)
result = dict(cursor.fetchall())
cursor.close()
return result
def pick_commands(near=None):
"""Returns all commands in the database."""
cursor = db.cursor()
if near:
sql = """SELECT CommandName FROM custom_commands
WHERE CommandName LIKE CONCAT(%s, '%')
ORDER BY CommandName
LIMIT 25;"""
cursor.execute(sql, (near,))
else:
sql = "SELECT CommandName FROM custom_commands ORDER BY CommandName LIMIT 25;"
cursor.execute(sql)
result = [x[0] for x in cursor.fetchall()]
cursor.close()
return result
def find_command(command_name):
"""Finds a specific command by name in the database."""
cursor = db.cursor()
sql = """SELECT CommandName FROM custom_commands
WHERE CommandName = %s"""
val = command_name
cursor.execute(sql, val)
rowcount = cursor.rowcount
cursor.close()
return rowcount > 0
def remove_command(name):
"""Removes a command by name from the database."""
cursor = db.cursor()
# sql = """UPDATE Custom_Commands
# SET
# Custom_Commands.IsDeleted = 1
# WHERE
# Custom_Commands.CommandName = %s;"""
sql = """DELETE FROM custom_commands
WHERE CommandName = %s"""
value = (name,)
cursor.execute(sql, value)
db.commit()
row_count = cursor.rowcount
cursor.close()
return row_count > 0
# --- LINK COMMANDS --- #
def add_link(link, added_by):
"""Adds a link to the database."""
cursor = db.cursor()
sql = "INSERT INTO links(Link, AddedBy) VALUES (%s, %s);"
values = (link, added_by)
try:
cursor.execute(sql, values)
except Exception as e:
print(e)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def delete_link_from_database(link):
cursor = db.cursor()
sql = "DELETE FROM links WHERE Link = %s"
val = (link,)
try:
cursor.execute(sql, val)
except Exception as e:
print(e)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def gfy_v2_test(group, idol=None):
"""Returns rows of GroupId, MemberId, GroupName, IdolName, Link"""
cursor = db.cursor()
if idol:
sql = """SELECT g.GroupId, m.MemberId, g.RomanName, m.RomanName, l.Link FROM links l
JOIN link_members lm on l.LinkId = lm.LinkId
JOIN members m on lm.MemberId = m.MemberId
JOIN member_aliases ma on m.MemberId = ma.MemberId
JOIN groupz g on g.GroupId = m.GroupId
JOIN groupz_aliases ga on ga.GroupId = g.GroupId
WHERE ga.Alias = %s AND ma.Alias = %s;"""
vals = (group, idol)
else:
sql = """SELECT g.GroupId, m.MemberId, g.RomanName, m.RomanName, l.Link FROM links l
JOIN link_members lm on l.LinkId = lm.LinkId
JOIN members m on lm.MemberId = m.MemberId
JOIN member_aliases ma on m.MemberId = ma.MemberId
JOIN groupz g on g.GroupId = m.GroupId
JOIN groupz_aliases ga on ga.GroupId = g.GroupId
WHERE ga.Alias = %s"""
vals = (group,)
try:
cursor.execute(sql, vals)
except Exception as e:
print(e)
result = cursor.fetchall()
cursor.close()
return result
def gfy_v2_test_tags(group, idol, tag):
cursor = db.cursor()
sql = """SELECT g.GroupId, m.MemberId, g.RomanName, m.RomanName, l.Link FROM links l
JOIN link_members lm on l.LinkId = lm.LinkId
JOIN members m on lm.MemberId = m.MemberId
JOIN member_aliases ma on m.MemberId = ma.MemberId
JOIN groupz g on g.GroupId = m.GroupId
JOIN groupz_aliases ga on ga.GroupId = g.GroupId
JOIN link_tags lt on lt.LinkId = l.LinkId
JOIN tag_aliases ta on ta.TagId = lt.TagId
WHERE ga.Alias = %s AND ma.Alias = %s AND ta.Alias = (%s);"""
vals = (group, idol, tag)
try:
cursor.execute(sql, vals)
except Exception as e:
print(e)
result = cursor.fetchall()
cursor.close()
return result
def gfy_v2_test_tags_group_only(group, tag):
cursor = db.cursor()
sql = """SELECT g.GroupId, m.MemberId, g.RomanName, m.RomanName, l.Link FROM links l
JOIN link_members lm on l.LinkId = lm.LinkId
JOIN members m on lm.MemberId = m.MemberId
JOIN member_aliases ma on m.MemberId = ma.MemberId
JOIN groupz g on g.GroupId = m.GroupId
JOIN groupz_aliases ga on ga.GroupId = g.GroupId
JOIN link_tags lt on lt.LinkId = l.LinkId
JOIN tag_aliases ta on ta.TagId = lt.TagId
WHERE ga.Alias = %s AND ta.Alias = (%s);"""
vals = (group, tag)
try:
cursor.execute(sql, vals)
except Exception as e:
print(e)
result = cursor.fetchall()
cursor.close()
return result
def get_link_from_user_favourites(user_id):
pass
def add_link_to_user_favs(link, user_id):
cursor = db.cursor()
sql = """INSERT INTO user_favourites VALUES (
(SELECT LinkId FROM links WHERE Link = %s),
%s)"""
vals = (link, user_id)
try:
cursor.execute(sql, vals)
except Exception as e:
print(e)
result = cursor.rowcount
cursor.close()
db.commit()
return result > 0
def count_links():
"""Returns a count of all links, groups, and members."""
cursor = db.cursor()
sql = """SELECT
(SELECT COUNT(*) FROM links) as link_count,
(SELECT COUNT(*) FROM groupz) as group_count,
(SELECT COUNT(*) FROM members) as member_count"""
cursor.execute(sql)
result = cursor.fetchone()
cursor.close()
return result
def get_link_id(link):
"""Returns a links unique ID in the database."""
cursor = db.cursor()
sql = "SELECT LinkId FROM links WHERE Link = %s"
val = (link,)
cursor.execute(sql, val)
link_id = cursor.fetchone()[0]
cursor.close()
return link_id
def remove_link(group, member, link):
"""Removes a link from a member."""
cursor = db.cursor()
sql = """delete links FROM links
left JOIN groupz_aliases
ON groupz_aliases.Alias = %s
left JOIN groupz
ON groupz.GroupId = groupz_aliases.GroupId
left JOIN member_aliases
ON member_aliases.Alias = %s
left JOIN members
ON members.MemberId = member_aliases.MemberId
left JOIN link_members
ON members.MemberId = link_members.MemberId
WHERE links.Link = %s
and links.LinkId = link_members.LinkId;"""
value = (group, member, link)
cursor.execute(sql, value)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def add_link_to_member(member_id, link_id):
cursor = db.cursor()
sql = """INSERT INTO link_members(LinkId, MemberId) VALUES (%s, %s);"""
values = (link_id, member_id)
try:
cursor.execute(sql, values)
except Exception as e:
print(e)
return False
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
# --- TAG COMMANDS --- #
def add_tag(tag_name, added_by):
"""Adds a new tag to the database."""
cursor = db.cursor()
sql = "INSERT INTO tags(TagName, AddedBy) VALUES (%s, %s);"
value = (tag_name, added_by)
cursor.execute(sql, value)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def add_tag_alias(tag_id, alias, added_by):
cursor = db.cursor()
sql = "INSERT INTO tag_aliases(TagId, Alias, AddedBy) VALUES (%s, %s, %s);"
values = (tag_id, alias, added_by)
cursor.execute(sql, values)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def get_all_tag_names():
"""Returns all tag names."""
cursor = db.cursor()
sql = "SELECT TagName FROM tags ORDER BY TagName"
# sql = """SELECT Alias FROM Tag_Aliases ORDER BY Alias"""
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result
def get_all_tag_alias_names():
"""Returns all tag names."""
cursor = db.cursor()
sql = "SELECT Alias FROM tag_aliases ORDER BY Alias"
# sql = """SELECT Alias FROM Tag_Aliases ORDER BY Alias"""
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result
def get_tag_parent_from_alias(tag):
"""gets parent tag name"""
cursor = db.cursor()
sql = """SELECT TagName, tags.TagId FROM tags
LEFT JOIN tag_aliases ON tags.TagId = tag_aliases.TagId
WHERE tag_aliases.Alias = %s"""
try:
cursor.execute(sql, (tag,))
except Exception as e:
print(e)
result = cursor.fetchone()
cursor.close()
return result
def get_all_alias_of_tag(tag_id):
"""Returns all aliases of tag from db."""
cursor = db.cursor()
sql = """select alias from tag_aliases
left join tags
on tags.TagId = tag_aliases.TagId
where tag_aliases.TagId = %s"""
val = (tag_id,)
cursor.execute(sql, val)
result = cursor.fetchall()
cursor.close()
return result
def add_tag_alias_db(tag, alias, user_id):
"""Adds a new alias to an existing tag."""
cursor = db.cursor()
sql = """INSERT INTO tag_aliases(TagId, Alias, AddedBy)
VALUES ((SELECT TagId FROM tags WHERE TagName = %s), %s, %s)"""
val = (tag, alias, user_id)
cursor.execute(sql, val)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def remove_tag_alias_db(tag, alias):
"""Adds a new alias to an existing tag."""
cursor = db.cursor()
sql = """delete from tag_aliases where tag_aliases.TagId = ANY(select tags.TagId from tags WHERE TagName = %s)
AND tag_aliases.Alias = %s"""
val = (tag, alias)
cursor.execute(sql, val)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def find_tag_id(tag_name):
"""Returns the unique ID of a tag by name."""
cursor = db.cursor()
sql = "SELECT TagId FROM tags WHERE TagName = %s"
val = (tag_name,)
cursor.execute(sql, val)
tag_id = cursor.fetchone()
cursor.close()
return tag_id
def find_tags_on_link(link):
"""Returns a dict of tagnames and IDs that are on a link."""
cursor = db.cursor()
sql = """SELECT TagName, TagId, links.LinkId
FROM tags, links
WHERE
links.Link = %s
AND
tags.LinkId = links.LinkId;"""
val = (link,)
cursor.execute(sql, val)
tags = dict(cursor.fetchall())
cursor.close()
return tags
def pick_tags_on_link(link):
cursor = db.cursor()
sql = """SELECT TagName
FROM tags, links
WHERE
links.Link = %s
AND
tags.LinkId = links.LinkId;"""
val = (link,)
cursor.execute(sql, val)
tags = cursor.fetchall()
cursor.close()
return tags
def remove_tag(tag):
cursor = db.cursor()
sql = "DELETE FROM tags WHERE TagName = %s"
value = (tag,)
cursor.execute(sql, value)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def add_link_tags(link, tag_name):
cursor = db.cursor()
sql = """INSERT INTO link_tags(LinkId, TagId) SELECT links.LinkId, tags.TagId FROM links
LEFT JOIN tag_aliases ON tag_aliases.Alias = %s
LEFT JOIN tags ON tags.TagId = tag_aliases.TagId
WHERE links.Link = %s"""
values = (tag_name, link)
try:
cursor.execute(sql, values)
except Exception as e:
print(e)
return False
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def remove_tag_from_link(link, tag):
cursor = db.cursor()
sql = """DELETE FROM link_tags
WHERE LinkId = ANY(SELECT LinkId FROM links WHERE Link = %s)
AND TagId = ANY(SELECT TagId FROM tags WHERE TagName = %s)"""
values = (link, tag)
cursor.execute(sql, values)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def get_links_with_tag(tag_name):
cursor = db.cursor()
sql = """select link from links
inner join link_tags on link_tags.LinkId = links.LinkId
inner join tags on tags.TagId = link_tags.TagId
inner join tag_aliases on tags.TagId = tag_aliases.TagId
where tag_aliases.Alias = %s;"""
val = (tag_name,)
try:
cursor.execute(sql, val)
except Exception as e:
print(e)
result = cursor.fetchall()
cursor.close()
return result
def member_link_count(group_name, member_name):
cursor = db.cursor()
sql = """select count(*) from link_members
inner join members on members.RomanName = %s
inner join groupz on groupz.RomanName = %s
where groupz.GroupId = members.GroupId
and members.MemberId = link_members.MemberId"""
values = (member_name, group_name)
cursor.execute(sql, values)
result = cursor.fetchone()
cursor.close()
return result
# --- GROUPZ COMMANDS --- #
def add_group(group_name, added_by):
cursor = db.cursor()
sql = "INSERT INTO groupz(RomanName, AddedBy) VALUES (%s, %s);"
values = (group_name, added_by)
try:
cursor.execute(sql, values)
except mysql.connector.errors.IntegrityError:
return None
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def remove_group(group):
cursor = db.cursor()
# sql = """UPDATE Groupz, Members, Link_Members, Links
# SET
# Groupz.IsDeleted = 1,
# Members.IsDeleted = 1,
# Link_Members.IsDeleted = 1,
# Links.IsDeleted = 1
# WHERE
# Groupz.RomanName = %s
# AND
# Members.GroupId = Groupz.GroupId
# AND
# Link_Members.MemberId = Members.MemberId
# AND
# Links.LinkId = Link_Members.LinkId;"""
# sql = """DELETE groupz, members, member_aliases, groupz_aliases, link_members, link_tags, links FROM groupz
# left JOIN members
# ON members.GroupId is not null and members.GroupId = groupz.GroupId
# left JOIN member_aliases
# ON member_aliases.MemberId is not null and member_aliases.MemberId = members.MemberId
# left JOIN groupz_aliases
# ON groupz_aliases.GroupId is not null and groupz_aliases.GroupId = groupz.GroupId
# left JOIN link_members
# ON members.MemberId is not null and members.MemberId = link_members.MemberId
# left JOIN links
# ON links.LinkId is not null and links.LinkId = link_members.LinkId
# left JOIN link_tags
# ON links.LinkId is not null and links.LinkId = link_tags.LinkId
# WHERE groupz.RomanName = %s;"""
sql = "DELETE FROM groupz WHERE RomanName = %s;"
values = (group,)
try:
cursor.execute(sql, values)
except mysql.connector.errors.IntegrityError:
return None
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def find_group_id(group_name):
cursor = db.cursor()
sql = """SELECT groupz.GroupId, RomanName FROM groupz
left join groupz_aliases
on groupz_aliases.Alias = %s
where groupz.GroupId = groupz_aliases.GroupId;"""
value = (group_name,)
try:
cursor.execute(sql, value)
except Exception as e:
print(e)
return None
g_id = cursor.fetchone()
cursor.close()
return g_id
def find_group_id_and_name(group_name):
cursor = db.cursor()
sql = """SELECT groupz.GroupId, groupz.RomanName FROM groupz
left join groupz_aliases
on groupz_aliases.Alias = %s
where groupz.GroupId = groupz_aliases.GroupId;"""
value = (group_name,)
try:
cursor.execute(sql, value)
except Exception as e:
print(e)
return None
name_and_g_id = cursor.fetchone()
cursor.close()
return name_and_g_id
def find_group_and_member_id(group_name, member_name):
cursor = db.cursor()
sql = """select groupz.GroupId, members.MemberId from groupz, members
where groupz.RomanName = %s
and members.RomanName = %s"""
values = (group_name, member_name)
cursor.execute(sql, values)
result = cursor.fetchone()
cursor.close()
return result
def get_groups():
cursor = db.cursor()
sql = """SELECT RomanName FROM groupz
ORDER BY RomanName"""
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result
def pick_groups(near=None):
cursor = db.cursor()
if near:
sql = """SELECT RomanName FROM groupz
WHERE RomanName LIKE CONCAT(%s, '%')
ORDER BY RomanName
LIMIT 25
"""
cursor.execute(sql, (near,))
else:
sql = """SELECT RomanName FROM groupz
ORDER BY RomanName
LIMIT 25
"""
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return [x[0] for x in result]
def pick_group_members(group_name):
cursor = db.cursor()
sql = """SELECT RomanName FROM members
WHERE GroupId = (SELECT GroupId FROM groupz WHERE RomanName = %s)
ORDER BY RomanName;"""
val = (group_name,)
cursor.execute(sql, val)
result = cursor.fetchall()
cursor.close()
return [x[0] for x in result]
def pick_tags(near=None):
cursor = db.cursor()
if near:
sql = """SELECT Alias FROM tag_aliases
WHERE Alias LIKE CONCAT(%s, '%')
ORDER BY Alias
LIMIT 25
"""
cursor.execute(sql, (near,))
else:
sql = """SELECT Alias FROM tag_aliases
ORDER BY Alias
LIMIT 25
"""
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return [x[0] for x in result]
def pick_tags_on_idol(group_name, idol_name, near=None):
cursor = db.cursor()
if near:
sql = """SELECT TagName FROM tags
JOIN link_tags on tags.TagId = link_tags.TagId
JOIN link_members on link_members.LinkId = link_tags.LinkId
JOIN members on members.MemberId = link_members.MemberId
JOIN groupz on groupz.GroupId = members.GroupId
WHERE members.RomanName = %s AND groupz.RomanName = %s AND TagName LIKE CONCAT(%s, '%')
GROUP BY TagName
ORDER BY TagName
LIMIT 25"""
cursor.execute(sql, (idol_name, group_name, near,))
else:
sql = """SELECT TagName FROM tags
JOIN link_tags on tags.TagId = link_tags.TagId
JOIN link_members on link_members.LinkId = link_tags.LinkId
JOIN members on members.MemberId = link_members.MemberId
JOIN groupz on groupz.GroupId = members.GroupId
WHERE members.RomanName = %s AND groupz.RomanName = %s
GROUP BY TagName
ORDER BY TagName
LIMIT 25"""
cursor.execute(sql, (idol_name, group_name,))
result = cursor.fetchall()
cursor.close()
return [x[0] for x in result]
def add_group_alias_db(group_name, alias, user_id):
cursor = db.cursor()
sql = """INSERT INTO groupz_aliases(GroupId, Alias, AddedBy)
VALUES ((SELECT GroupId FROM groupz WHERE RomanName = %s), %s, %s)"""
val = (group_name, alias, user_id)
cursor.execute(sql, val)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def remove_group_alias_db(group_name, alias):
cursor = db.cursor()
sql = """delete from groupz_aliases where groupz_aliases.GroupId = ANY(
select groupz.GroupId from groupz WHERE RomanName = %s)
AND groupz_aliases.Alias = %s"""
val = (group_name, alias)
cursor.execute(sql, val)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def get_group_aliases(group_name):
cursor = db.cursor()
sql = "select Alias from groupz_aliases WHERE GroupId = %s"
val = (group_name,)
cursor.execute(sql, val)
result = cursor.fetchall()
cursor.close()
return result
# --- Members Commands --- #
def add_member(group_name, member_name, addedby):
cursor = db.cursor()
sql = """INSERT INTO members(GroupId, RomanName, AddedBy)
VALUES ((SELECT groupz.GroupId FROM groupz WHERE groupz.RomanName = %s),
%s, %s);"""
values = (group_name, member_name, addedby)
try:
cursor.execute(sql, values)
except mysql.connector.errors.IntegrityError:
return None
db.commit()
rowcount = cursor.rowcount
cursor.close()
return rowcount > 0
def remove_member(group_id, member_name):
cursor = db.cursor()
# sql = """UPDATE Members, Link_Members, Links
# SET
# Members.IsDeleted = 1,
# Link_Members.IsDeleted = 1,
# Links.IsDeleted = 1
# WHERE
# Members.MemberId = %s
# AND
# Link_Members.MemberId = Members.MemberId
# AND
# Links.LinkId = Link_Members.LinkId;"""
# sql = """DELETE Members, Link_Members, Links FROM Members
# left JOIN link_members
# ON members.MemberId = link_members.MemberId
# left JOIN links
# ON links.LinkId = link_members.LinkId
# WHERE members.RomanName = %s
# AND members.GroupId = %s;"""
sql = "DELETE FROM members WHERE RomanName = %s AND GroupId = %s"
values = (member_name, group_id)
try:
cursor.execute(sql, values)
except Exception as e:
print(e)
rowcount = cursor.rowcount
db.commit()
cursor.close()
return rowcount > 0
def find_member_id(group_id, member_name):
cursor = db.cursor()
# sql = "SELECT MemberId FROM Members WHERE RomanName = (%s) AND GroupId = (%s)"
sql = """SELECT members.MemberId, RomanName FROM members
left join member_aliases
on member_aliases.MemberId = members.MemberId
WHERE alias = %s AND members.GroupId = %s"""
val = (member_name, group_id)
try:
cursor.execute(sql, val)
except Exception as e:
print(e)
member_id = cursor.fetchone()
cursor.close()
return member_id
def find_member_id_and_name(group_id, member_name):
cursor = db.cursor()
# sql = "SELECT MemberId FROM Members WHERE RomanName = (%s) AND GroupId = (%s)"
sql = """SELECT members.MemberId, members.RomanName FROM members
left join member_aliases
on member_aliases.MemberId = members.MemberId
WHERE alias = %s AND members.GroupId = %s"""
val = (member_name, group_id)
try:
cursor.execute(sql, val)
except Exception as e:
print(e)
member_id_and_name = cursor.fetchone()
cursor.close()
return member_id_and_name
def add_member_alias_db(group, idol, alias, user_id):
cursor = db.cursor()
sql = """INSERT INTO member_aliases(MemberId, Alias, AddedBy) VALUES (
(SELECT MemberId FROM members
left join groupz on groupz.GroupId = members.GroupId
left join groupz_aliases on groupz.GroupId = groupz_aliases.GroupId
WHERE groupz_aliases.Alias = %s AND members.RomanName = %s),
%s, %s)"""
val = (group, idol, alias, user_id)
try:
cursor.execute(sql, val)
except Exception as e:
print(e)
db.commit()
rowcount = cursor.rowcount
cursor.close()
return rowcount > 0
def remove_member_alias_db(group, idol, alias):
cursor = db.cursor()
sql = """delete from member_aliases
where member_aliases.MemberId = ANY(
select members.MemberId from members
left join groupz on groupz.GroupId = members.GroupId
left join groupz_aliases on groupz.GroupId = groupz_aliases.GroupId
WHERE members.RomanName = %s AND groupz_aliases.Alias = %s
)
AND member_aliases.Alias = %s"""
val = (idol, group, alias)
cursor.execute(sql, val)
db.commit()
rowcount = cursor.rowcount
cursor.close()
return rowcount > 0
def find_member_aliases(member_id):
cursor = db.cursor()
sql = "select Alias from member_aliases where MemberId = %s ORDER BY Alias"
val = (member_id,)
cursor.execute(sql, val)
result = cursor.fetchall()
cursor.close()
return result
def get_members_of_group(group_name):
"""Returns a dict of members of a group."""
cursor = db.cursor()
sql = """SELECT members.RomanName FROM members
INNER JOIN groupz ON groupz.RomanName = %s
WHERE members.GroupId = groupz.GroupId;"""
val = (group_name,)
cursor.execute(sql, val)
members = cursor.fetchall()
cursor.close()
return members
def get_members_of_group_by_group_id(group_id):
cursor = db.cursor()
sql = """SELECT members.RomanName FROM members
INNER JOIN groupz ON groupz.GroupId = %s
WHERE members.GroupId = groupz.GroupId;"""
val = group_id
cursor.execute(sql, val)
members = cursor.fetchall()
cursor.close()
return members
def get_member_links(member_id):
cursor = db.cursor()
sql = """select Link from links
inner join link_members
on links.LinkId = link_members.LinkId
where link_members.MemberId = %s;"""
vals = (member_id,)
try:
cursor.execute(sql, vals)
except Exception as e:
print(e)
result = cursor.fetchall()
cursor.close()
return result
def get_members_of_group_and_link_count(group_id):
cursor = db.cursor()
sql = """SELECT members.RomanName,
(SELECT COUNT(*) FROM link_members WHERE link_members.MemberId = members.MemberId)
FROM members
INNER JOIN groupz ON groupz.GroupId = %s
WHERE members.GroupId = groupz.GroupId
ORDER BY members.RomanName"""
val = (group_id,)
cursor.execute(sql, val)
result = cursor.fetchall()
cursor.close()
return result
def get_member_links_with_tag(member_id, tag):
cursor = db.cursor()
sql = """select Link from links
left join link_members
on links.LinkId = link_members.LinkId
left join link_tags
on link_tags.LinkId = links.LinkId
left join tags
on tags.TagId = link_tags.TagId
left join tag_aliases
on tag_aliases.TagId = tags.TagId
WHERE tag_aliases.Alias = %s
AND link_members.MemberId = %s
"""
vals = (tag, member_id)
try:
cursor.execute(sql, vals)
except Exception as e:
print(e)
result = cursor.fetchall()
cursor.close()
return result
def count_links_of_member(member_id):
cursor = db.cursor()