-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease_robbe.py
More file actions
1050 lines (852 loc) · 38.5 KB
/
release_robbe.py
File metadata and controls
1050 lines (852 loc) · 38.5 KB
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
"""
Get and manage new songs and artists from Spotify
:Author: Andreas Lindlbauer (@alindl)
:Copyright: (c) 2021 Andreas Lindlbauer
"""
__license__ = "EUPL"
__docformat__ = 'reStructuredText'
import difflib
import subprocess
import math
import os.path
import sys
from enum import Enum
import pygame
from dialog import Dialog
import spotipy
import spotipy.util as util
import file_interaction as fi
import config_io as conf
from colorama import Fore, Back, Style
# Currently set to 28 Aug 2020
class States(Enum):
"""
Enum to represent the different states
"""
START = "START"
EXIT = "EXIT"
DONE = "DONE"
NEW_RELEASES = "NR"
TOP_10_GREY = "TG"
CONF = "CONF"
DATE = "DATE"
LISTS = "LISTS"
ALLOWLIST = "AL"
GREYLIST = "GL"
BLOCKLIST = "BL"
POP_GREY = "PG"
SOURCE = "SOURCE"
# NOTE Using int would improve performance, but result in worse legibility
DIALOG = Dialog(dialog="dialog")
ARTISTS_SET = set()
ARTISTS_DICT = {}
ALL_SONGS = set()
LIST_DICT = {
fi.Lists.ALLOWLIST.value: fi.Lists.ALLOWLIST,
fi.Lists.GREYLIST.value: fi.Lists.GREYLIST,
fi.Lists.BLOCKLIST.value: fi.Lists.BLOCKLIST,
fi.Lists.DELETE.value: fi.Lists.DELETE
}
def main():
"""
One function to start them all
"""
if os.path.isfile('mach_die_robbe.mp3'):
pygame.mixer.init()
pygame.mixer.music.load("mach_die_robbe.mp3")
pygame.mixer.music.play(-1)
state = States.START
while True:
while state not in (States.NEW_RELEASES, States.TOP_10_GREY):
state = menu(state)
if state == States.EXIT:
clear_screen()
sys.exit()
scope = 'playlist-read-collaborative \
playlist-read-private \
playlist-modify-private \
playlist-modify-public \
user-follow-read'
token = util.prompt_for_user_token(conf.get_key('Auth', 'username'), scope,
client_id=conf.get_key('Auth', 'client_id'),
client_secret=conf.get_key('Auth', 'client_secret'),
redirect_uri='http://localhost:8888/callback/')
if token:
spot_conn = spotipy.Spotify(auth=token)
#spot_conn.trace = False
fi.sort_all_lists(DIALOG)
get_songs(spot_conn, state)
if not add_songs_to_playlist(spot_conn):
size = get_window_size((5, 5), (10, 28))
DIALOG.msgbox(text="\n\nSomething didn't go right, "+ \
"while adding songs to your playlist.",
height=size[0], width=size[1])
else:
conf.write_time()
text="""
██████╗░░█████╗░███╗░░██╗███████╗██╗
██╔══██╗██╔══██╗████╗░██║██╔════╝██║
██║░░██║██║░░██║██╔██╗██║█████╗░░██║
██║░░██║██║░░██║██║╚████║██╔══╝░░╚═╝
██████╔╝╚█████╔╝██║░╚███║███████╗██╗
╚═════╝░░╚════╝░╚═╝░░╚══╝╚══════╝╚═╝"""
size = get_window_size((12, 0), (41, 0))
DIALOG.msgbox(text, height=size[0], width=size[1])
state = States.START
clear_screen()
fi.sort_all_lists(DIALOG)
else:
print("Can't get token for", conf.get_key('Auth', 'username'))
clear_screen()
def start_menu():
"""
Main menu
"""
text = """
████████████
██░░░░░░░░░░░░██
░█▀▀█ █▀▀ █ █▀▀ █▀▀█ █▀▀ █▀▀ ░█▀▀█ █▀▀█ █▀▀▄ █▀▀▄ █▀▀ ██░░░░░░░░░░░░░░░░██
░█▄▄▀ █▀▀ █ █▀▀ █▄▄█ ▀▀█ █▀▀ ░█▄▄▀ █ █ █▀▀▄ █▀▀▄ █▀▀ ██░░░░ ░░░░░░ ░░██
░█ ░█ ▀▀▀ ▀▀▀ ▀▀▀ ▀ ▀ ▀▀▀ ▀▀▀ ░█ ░█ ▀▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ██░░░░ ██░░░░░░██ ░░██
██░░░░ ██░░░░░░██ ░░██
██░░░░░░░░░░██░░░░░░░░██
██░░░░████░░░░░░████░░██
██░░░░██░░░░░░██░░░░░░██
██████████░░░░░░░░████░░░░░░████
██░░░░░░██░░░░░░░░██░░░░░░░░░░██ ██
██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██
████████ ██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░████░░░░░░░░░░░░░░░░██░░░░░░░░░░░░░░░░██████
██░░░░██░░░░░░░░░░░░░░░░░░░░░░░░██░░░░░░░░░░██░░██░░░░░░████
████░░░░░░░░░░██░░░░░░░░░░░░██░░░░░░░░░░██░░██░░░░░░██░░░░██
██░░░░░░░░░░░░██████░░░░░░░░██████░░░░░░░░████ ████░░░░░░░░██
████████████ ██████████░░░░░░░░░░██ ████████
██████████
"""
choices = [(States.NEW_RELEASES.value,
"Get new releases"),
(States.TOP_10_GREY.value,
"Fetch top 10 of all artists on greylist"),
(States.CONF.value,
"Configure credentials"),
(States.POP_GREY.value,
"Decide on Greylist entries (to allow/block-list or delete)"),
(States.DATE.value,
"Set start date to check releases from"),
(States.LISTS.value,
"View/Edit List (Allow, Grey, Block)"),
(States.SOURCE.value,
"Modify source of artists (playlist/allowlist/following)"),
(States.EXIT.value,
"Quit")]
size = get_window_size((31, len(choices)),
(48, len(max(choices, key=lambda item: len(item[1]))[1])))
code, state = DIALOG.menu(text, choices=choices, no_tags=True,
no_cancel=True, height=size[0], width=size[1], tab_len=1)
if code != DIALOG.OK:
return States.EXIT
return States(state)
def rel_menu():
"""
Start going for new releases from artists from your source.
Also check if source was set
"""
conf_set, _ = conf.get_credentials()
if conf_set:
if not conf.get_key('Other', 'source'):
return States.SOURCE
return States.NEW_RELEASES
return States.CONF
def top_menu():
"""
Pick top 10 releases from all artists of your greylist.
"""
conf_set, _ = conf.get_credentials()
if conf_set:
return States.TOP_10_GREY
return States.CONF
def date_menu():
"""
Menu to pick a day, that will be used to get releases from there to today.
"""
# NOTE Weird way to say that
last_check = conf.read_time()
code, date = DIALOG.calendar("From which date on do you want to add releases?",
day=last_check.day,
month=last_check.month,
year=last_check.year,
title="Configure checking date")
if code == DIALOG.OK:
conf.set_key('Other', 'last_check', str(conf.convert_list_to_ts(date)))
return States.START
def do_exit():
"""
Exit programm, but clear screen just before that
"""
return States.EXIT
def menu(state):
"""
Menu to check all configs
"""
if state == States.START:
output = start_menu()
elif state == States.NEW_RELEASES:
output = rel_menu()
elif state == States.TOP_10_GREY:
output = top_menu()
elif state == States.CONF:
output = configure_prog()
elif state == States.DATE:
output = date_menu()
elif state == States.LISTS:
output = list_chooser()
elif state == States.POP_GREY:
output = edit_chooser(fi.Lists.GREYLIST, States.POP_GREY)
elif state == States.SOURCE:
output = source_chooser()
else:
output = do_exit()
return output
# Somehow, pylint doesn't like my switch-case implementation, too many lambdas
#switcher = {
# States.START: lambda: start_menu(),
# States.NEW_RELEASES: lambda: rel_menu(),
# States.TOP_10_GREY: lambda: top_menu(),
# States.CONF: lambda: configure_prog(),
# States.DATE: lambda: date_menu(),
# States.LISTS: lambda: list_chooser(),
# States.POP_GREY: lambda: edit_chooser(fi.Lists.GREYLIST, States.POP_GREY),
# States.SOURCE: lambda: source_chooser()
# }
#return switcher.get(state, lambda : do_exit())()
def source_chooser():
"""
Choose which source to get artists from
"""
text = """ Where should the list of artists come from? """
choices = [("playlists", "Chosen Playlists"),
("allowlist", "Allowlist"),
("saved", "Artists I follow")]
size = get_window_size((8, len(choices)),
(25, len(max(choices, key=lambda item: len(item[1]))[1])))
code, source = DIALOG.menu(text, choices=choices, no_tags=True, height=size[0], width=size[1])
if code == DIALOG.OK:
conf.set_key('Other', 'source', source)
return States.START
def get_window_size(height_boundries, width_boundries):
"""
Calculate best suiting window size
"""
max_height, max_width = DIALOG.maxsize()
height, width = None, None
if height_boundries is not None:
min_height, input_height = height_boundries
input_height = input_height + min_height
golden_height = round((max_height/3)*2)
if input_height < golden_height:
height = input_height
else:
height = golden_height
if min_height > golden_height:
height = max_height # or = 0 ??
if width_boundries is not None:
min_width, input_width = width_boundries
input_width = input_width + min_width
golden_width = round((max_width/3)*2)
if input_width < golden_width:
width = input_width
else:
width = golden_width
if min_width > golden_width:
width = max_width # or = 0 ??
return height, width
#height = min_height if input_height < min_height else input_height
#if height > max_height:
# height = 0
#width = min_width if input_width < min_width else input_width
#if width > max_width:
# width = 0
def list_and_push_artists(from_list):
"""
Get whole list and decide on artists
"""
choices = []
artists, size_of_list = fi.get_list(from_list)
if size_of_list < 1:
DIALOG.msgbox("List is empty.")
return False
longest_artist_name = 0
for i, artist in enumerate(artists):
#artist = artists[i]
if isinstance(artist, list):
choices.append((str(i), artist[0], False))
if len(artist[0]) > longest_artist_name:
longest_artist_name = len(artist[0])
else:
choices.append((str(i), artist, False))
if len(artist) > longest_artist_name:
longest_artist_name = len(artist)
text = r"Which artists would you like to push from \Zu\Zb%s\Zn?" % (from_list.name)
size = get_window_size((10, len(choices)), (22, longest_artist_name))
code, tags = DIALOG.checklist(text=text, cancel_label="Back", colors=True,
choices=choices, no_tags=True, height=size[0], width=size[1])
if code == DIALOG.OK:
text = r"\ZuWhere\Zn should these artists be pushed to?"
choices = [(fi.Lists.ALLOWLIST.value, "Push to Allowlist "),
(fi.Lists.BLOCKLIST.value, "Push to Blocklist"),
(fi.Lists.DELETE.value, "Push into /dev/null")]
for choice in choices:
if choice[0] == from_list.value:
choices.remove(choice)
size = get_window_size((7, len(choices)),
(25, len(max(choices, key=lambda item: len(item[1]))[1])))
code, to_list = DIALOG.menu(text, choices=choices, cancel_label="Abort", no_tags=True,
height=size[0], width=size[1], colors=True)
else:
return True
if code == DIALOG.OK:
to_list = LIST_DICT[to_list]
for i in tags:
fi.move_artist_between_lists(from_list, to_list, artists[int(i)])
return True
return False
def search_and_push_artist(from_list):
"""
Give name of specific artist,
- Look up entry from list.
- If found decide on fate
- If not found, maybe get whole list or try again?
"""
text = """Name of artist"""
entry = None
size = get_window_size((8, 0),
(40, 0))
code, artist = DIALOG.inputbox(text, cancel_label="Abort",
height=size[0], width=size[1])
if code == DIALOG.OK:
entry = fi.check_if_on_list(from_list, [artist])
else:
return True
if entry:
text = r"""Found \Zb%s\Zn! Where should this artist be pushed to? """ % (entry[0])
choices = [(fi.Lists.ALLOWLIST.value, "Push to Allowlist "),
(fi.Lists.BLOCKLIST.value, "Push to Blocklist"),
(fi.Lists.DELETE.value, "Push into /dev/null")]
for choice in choices:
if choice[0] == from_list.value:
choices.remove(choice)
size = get_window_size((7, len(choices)),
(30, len(max(choices, key=lambda item: len(item[1]))[1])))
code, to_list = DIALOG.menu(text, choices=choices, cancel_label="Abort", no_tags=True,
height=size[0], width=size[1], colors=True)
if code == DIALOG.OK:
to_list = LIST_DICT[to_list]
if fi.move_artist_between_lists(from_list, to_list, entry):
return True
return False
def edit_chooser(from_list, referrer):
"""
Choose how to edit the list
"""
text = r"""Do you have a specific artist in mind or
do you want to check the whole \Zb%s\Zn? """ % (from_list.name)
choices = [("specific", "Specific artist"), ("list", "Whole list")]
size = get_window_size((8, len(choices)),
(30, len(max(choices, key=lambda item: len(item[1]))[1])))
code, tag = DIALOG.menu(text, choices=choices, no_tags=True,
colors=True, height=size[0], width=size[1])
while code == DIALOG.OK:
if tag == "specific":
if not search_and_push_artist(from_list):
return referrer
if tag == "list":
if not list_and_push_artists(from_list):
return referrer
size = get_window_size((8, len(choices)),
(30, len(max(choices, key=lambda item: len(item[1]))[1])))
code, tag = DIALOG.menu(text, choices=choices, cancel_label="Back", no_tags=True,
colors=True, height=size[0], width=size[1])
if referrer == States.POP_GREY:
return States.START
return States.LISTS
def list_chooser():
"""
Choose which list to view/edit
"""
text = r"""\ZuWhich\Zn list do you want to edit? """
choices = [(fi.Lists.ALLOWLIST.value, "Allowlist"),
(fi.Lists.GREYLIST.value, "Greylist"),
(fi.Lists.BLOCKLIST.value, "Blocklist")]
size = get_window_size((7, len(choices)),
(29, len(max(choices, key=lambda item: len(item[1]))[1])))
code, from_list = DIALOG.menu(text, choices=choices, colors=True,
no_tags=True, height=size[0], width=size[1])
if code == DIALOG.OK:
from_list = LIST_DICT[from_list]
return edit_chooser(from_list, States.LISTS)
return States.START
def configure_prog():
"""
Edit Spotify credentials
"""
_, info = conf.get_credentials()
valid, error_msg = conf.check_credentials(info)
elements = [
("Username", 1, 1,
info[0] if info[0] else "xXspice_girls_4_lifeXx",
1, 20, 64, 0),
("Client ID", 2, 1,
info[1] if info[1] else "some32alphanumericcharacters",
2, 20, 40, 0),
("Client Secret", 3, 1,
info[2] if info[2] else "other32alphanumericcharacters",
3, 20, 40, 0),
("Country", 4, 1,
info[3] if info[3] else "US",
4, 20, 10, 2),
("Allowlist", 5, 1,
info[4] if info[4] else "allowlist.csv",
5, 20, 32, 32),
("Greylist", 6, 1,
info[5] if info[5] else "greylist.csv",
6, 20, 32, 32),
("Blocklist", 7, 1,
info[6] if info[6] else "whitelist.csv",
7, 20, 32, 32)
]
size = get_window_size((7, len(elements)+error_msg.count('\n')),
(30, len(max(info, key=len))))
code, fields = DIALOG.form("Please fill in your Spotify credentials:" + error_msg,
elements, height=size[0], width=size[1])
if code != DIALOG.OK:
return States.START
valid, error_msg = conf.check_credentials(fields)
while not valid:
elements = [
("Username", 1, 1, fields[0], 1, 20, 32, 32),
("Client ID", 2, 1, fields[1], 2, 20, 32, 32),
("Client Secret", 3, 1, fields[2], 3, 20, 32, 32),
("Country", 4, 1, fields[3], 4, 20, 10, 2),
("Allowlist", 5, 1, fields[4], 5, 20, 32, 32),
("Greylist", 6, 1, fields[5], 6, 20, 32, 32),
("Blocklist", 7, 1, fields[6], 7, 20, 32, 32)
]
size = get_window_size((7, len(elements)+error_msg.count('\n')),
(30, len(max(info, key=len))))
code, fields = DIALOG.form("Please fill in your Spotify credentials:" + error_msg,
elements, height=size[0], width=size[1])
if code != DIALOG.OK:
return States.START
valid, error_msg = conf.check_credentials(fields)
if code == DIALOG.OK:
conf.set_key('Auth', 'username', fields[0])
conf.set_key('Auth', 'client_id', fields[1])
conf.set_key('Auth', 'client_secret', fields[2])
conf.set_key('Other', 'country', fields[3])
conf.set_key('Lists', 'allowlist', fields[4])
conf.set_key('Lists', 'greylist', fields[5])
conf.set_key('Lists', 'blocklist', fields[6])
return States.START
def check_and_get_artist_id(spot_conn, name):
"""
Get artist id from Spotify
"""
results = None
if isinstance(name, str):
results = spot_conn.search(q=name, type='artist')
elif isinstance(name, list):
if len(name) == 1:
results = spot_conn.search(q=name[0], type='artist')
elif len(name) == 2:
return name[1]
else:
return None
else:
return None
items = results['artists']['items']
if len(items) > 0:
return items[0]['id']
return None
def buzz_filter(string):
"""
Filter out strings containing buzz words, except for the ones having anti_buzz words
"""
buzzwords = ['LIVE', '- Live', '(Live']
buzzwords_lowercase = ['(live', ' - live', ' live version',
' live from', ' live in', ' live at', 'instrumental',
'interlude', 'acoustic']
anti_buzzwords = ['RAC']
anti_buzzwords_lowercase = ['rac']
# Contains any buzzword from the list
has_buzzwords = any(buzz in string for buzz in buzzwords)
# Contains any anti buzzword from the list that would allow the use of a buzzword
has_anti_buzzwords = any(anti_buzz in string for anti_buzz in anti_buzzwords)
# Same for case-insensitive buzzwords.
has_lowercase_buzzwords = any(buzz in string.lower() for buzz in buzzwords_lowercase)
has_lowercase_anti_buzzwords = any(anti_buzz in string.lower() \
for anti_buzz in anti_buzzwords_lowercase)
return (not has_buzzwords or has_anti_buzzwords ) and \
(not has_lowercase_buzzwords or has_lowercase_anti_buzzwords)
def get_album_tracks(spot_conn, album, track_names, track_uris):
"""
Get all tracks from album ID
"""
tracks = []
num_tracks = 0
track_page = spot_conn.album_tracks(album['id'], limit=10)
tracks.extend(track_page['items'])
while track_page['next']:
track_page = spot_conn.next(track_page)
tracks.extend(track_page['items'])
for track in tracks:
track_name = track['name']
if buzz_filter(track_name):
track_names.append(track_name)
track_uris.append(track['uri'])
ALL_SONGS.add(track['uri'])
num_tracks += 1
return num_tracks
# NOTE How to filter remixes, if the remixer isn't the artist?
def get_artist_albums(spot_conn, artist_id):
"""
Get all albums from artist ID
"""
albums = []
album_page = spot_conn.artist_albums(artist_id, album_type='album,single', limit=10)
albums.extend(album_page['items'])
while album_page['next']:
album_page = spot_conn.next(album_page)
albums.extend(album_page['items'])
return albums
def delete_duplicate_songs(track_names, track_uris):
"""
Remove duplicate songs
"""
duplicates = 0
for track_first_idx in enumerate(track_names):
for track_second_idx in range(track_first_idx[0], len(track_uris)):
if track_first_idx[0] != track_second_idx:
seq = difflib.SequenceMatcher(a=track_names[track_first_idx[0]].lower(),
b=track_names[track_second_idx].lower())
if seq.ratio() > 0.9:
if track_uris[track_second_idx] in ALL_SONGS:
ALL_SONGS.remove(track_uris[track_second_idx])
duplicates += 1
return duplicates
def check_artist_albums(spot_conn, artist_info):
"""
Go through all album tracks of artists
"""
num_tracks = 0
track_names = []
track_uris = []
albums = get_artist_albums(spot_conn, artist_info[1])
#i = 0
#DIALOG.gauge_start(text="Gathering songs by %s ..." % (artist_info[0]), percent=0)
#total_albums = len(albums)
unique = set() # skip duplicate albums
for album in albums:
#DIALOG.gauge_update(math.floor((i/total_albums)*100))
album_name = album['name']
if album_name not in unique and album['album_type'] != 'compliation':
if conf.get_release_date(album) > conf.read_time():
num_tracks += get_album_tracks(spot_conn, album, track_names, track_uris)
unique.add(album_name)
#i += 1
#DIALOG.gauge_stop()
num_tracks -= delete_duplicate_songs(track_names, track_uris)
return num_tracks
def get_user_playlists(spot_conn):
"""
Gets a list of playlist information of all user playlists
Saved playlists from other people also get added
"""
playlists_page = spot_conn.current_user_playlists()
playlists = []
for playlist in playlists_page['items']:
playlists.append({'name': playlist['name'],
'owner': playlist['owner'],
'id': playlist['id']})
while playlists_page['next']:
playlists_page = spot_conn.next(playlists_page)
for playlist in playlists_page['items']:
playlists.append({'name': playlist['name'],
'owner': playlist['owner'],
'id': playlist['id']})
return playlists
def choose_dest_playlist(spot_conn):
"""
Choose playlist to add the songs into
"""
playlists = get_user_playlists(spot_conn)
choices = list(map(lambda playlist: (playlist['name'], ""), playlists))
text = r"""To which \ZbSpotify playlist\Zn should those %d songs be added to?""" % (len(ALL_SONGS))
size = get_window_size((10, len(choices)),
(15, len(max(choices, key=lambda item: len(item[0]))[0])))
code, tag = DIALOG.menu(text, choices=choices, colors=True, height=size[0], width=size[1])
if code == DIALOG.OK:
return [x for x in playlists if x['name'] == tag][0]['id']
return False
def choose_playlists(spot_conn):
"""
Choose playlists from all user playlists
"""
playlists = get_user_playlists(spot_conn)
choices = list(map(lambda playlist: (playlist['name'], "", False), playlists))
size = get_window_size((10, len(choices)),
(22, len(max(choices, key=lambda item: len(item[0]))[0])))
code, tags = DIALOG.checklist(text=r"Which \ZuSpotify playlists\Zn would you like to search for artists?",
choices=choices, height=size[0], width=size[1], colors=True)
if code == DIALOG.OK:
if len(tags) < 1:
return False
return [x for x in playlists if x['name'] in tags]
return False
def check_artist_top_songs(spot_conn, artist_id):
"""
Get Top 10 songs of artist, add them to the frey
"""
country = conf.get_key('Other', 'country')
top_tracks = spot_conn.artist_top_tracks(artist_id, country=country if country else "US")
num_tracks = 0
track_names = []
track_uris = []
tracks = top_tracks['tracks']
for track in tracks:
track_name = track['name']
track_names.append(track_name)
track_uris.append(track['uri'])
ALL_SONGS.add(track['uri'])
num_tracks += 1
num_tracks -= delete_duplicate_songs(track_names, track_uris)
return num_tracks
def get_artists_from_list(spot_conn, list_name):
"""
Get all artists from list
"""
i = 0
DIALOG.gauge_start(text="Gathering artists", percent=0)
this_list, length = fi.get_list(list_name)
#artist[0] == name; artist[1] == id
for artist in this_list:
DIALOG.gauge_update(math.floor((i/length)*100))
if isinstance(artist, list):
if len(artist) == 1:
artist_id = check_and_get_artist_id(spot_conn, artist[0])
fi.add_missing_id(list_name, [artist[0], artist_id], artist)
artist.append(artist_id)
elif isinstance(artist, str):
artist_id = check_and_get_artist_id(spot_conn, artist)
fi.add_missing_id(list_name, [artist, artist_id], [artist])
artist = [artist].append(artist_id)
else:
raise TypeError("Artist entry is neither list nor string type")
ARTISTS_DICT[artist[0]] = artist[1]
ARTISTS_SET.add(artist[1])
i += 1
DIALOG.gauge_stop()
def get_artists_from_followed(spot_conn):
"""
Get all artists that you are following
"""
artists_page = spot_conn.current_user_followed_artists(limit=10)
total_artists = artists_page['artists']['total']
clear_screen()
DIALOG.gauge_start(text="Gathering artists", percent=0)
for artist in artists_page['artists']['items']:
ARTISTS_SET.add(artist['id'])
ARTISTS_DICT[artist['name']] = artist['id']
i = len(artists_page['artists']['items'])
while artists_page['artists']['next']:
DIALOG.gauge_update(round((i/total_artists)*100))
artists_page = spot_conn.next(artists_page['artists'])
for artist in artists_page['artists']['items']:
ARTISTS_DICT[artist['name']] = artist['id']
ARTISTS_SET.add(artist['id'])
i += len(artists_page['artists']['items'])
_ = fi.remove_blocklisted(ARTISTS_DICT, ARTISTS_SET)
DIALOG.gauge_stop()
return True
def get_artists_from_playlist(spot_conn):
"""
Get all artists from specific playlist
"""
playlists = choose_playlists(spot_conn)
if not playlists:
return False
# Get set of all needed artist id's
fst_artist = True
#for playlist in playlists['items']:
i = 0
DIALOG.gauge_start(text="Gathering artists", percent=0)
for playlist in playlists:
DIALOG.gauge_update(round((i/len(playlists))*100))
i += 1
playlist_tracks = []
track_page = spot_conn.playlist_tracks(playlist['id'],
limit=100)
playlist_tracks.extend(track_page['items'])
while track_page['next']:
track_page = spot_conn.next(track_page)
playlist_tracks.extend(track_page['items'])
for track in playlist_tracks:
fst_artist = True
if not track['is_local']:
for artist in track['track']['artists']:
if fst_artist:
ARTISTS_SET.add(artist['id'])
ARTISTS_DICT[artist['name']] = artist['id']
fst_artist = False
_ = fi.remove_blocklisted(ARTISTS_DICT, ARTISTS_SET)
DIALOG.gauge_stop()
return True
def get_top_songs(spot_conn):
"""
Get top songs from artists
"""
inv_artists_dict = {v: k for k, v in ARTISTS_DICT.items()}
i = 0
size = get_window_size(None, (10, len(max(inv_artists_dict, key=len))))
DIALOG.gauge_start(text="Finding tracks", percent=0, width=size[1], colors=True)
for artist_id in ARTISTS_SET:
artist_name = inv_artists_dict[artist_id]
DIALOG.gauge_update(text=r"Getting top tracks by \Zb%s\Zn" % (artist_name),
percent=round((i/len(ARTISTS_SET))*100), update_text=True)
i += 1
check_artist_top_songs(spot_conn, artist_id)
DIALOG.gauge_stop()
def get_new_songs(spot_conn):
"""
Get new songs
"""
inv_artists_dict = {v: k for k, v in ARTISTS_DICT.items()}
i = 1
skip_all = False
size = get_window_size(None, (32, len(max(inv_artists_dict, key=len))))
DIALOG.gauge_start(text="Finding tracks", percent=0, width=size[1], colors=True)
for artist_id in ARTISTS_SET:
artist_name = inv_artists_dict[artist_id]
artist_info = [artist_name, artist_id]
DIALOG.gauge_update(text="Finding tracks by \Zb%s\Zn" % (artist_name),
percent=math.floor((i/len(ARTISTS_SET))*100), update_text=True)
i += 1
new_artist = False
if not fi.check_if_on_list(fi.Lists.ALLOWLIST, artist_info):
new_artist = True
# Oh nice, a new one
if fi.check_if_on_list(fi.Lists.GREYLIST, artist_info):
continue # Just ignore Greylist ones
#text = """This one is already on your greylist! \
# What should happen to %s?""" % (artist_name)
#choices = [("T", "Ignore new songs here, only add Top 10 songs")] + \
# standard_choices + \
# [("TA", "Ignore all new Greylist songs here, \
# add Top 10 songs of all Greylist entries")]
fi.add_to_list(fi.Lists.GREYLIST, artist_info)
fi.sort_list(fi.Lists.GREYLIST)
if not skip_all:
text = """\Zb%s\Zn is a new one! Added to greylist.
What should happen next?""" % (artist_name)
standard_choices = [
("I", "Ignore new songs by %s" % (artist_name)),
("IA", "Ignore new songs from ALL new artists"),
("W", "Add new songs and add %s to allowlist" % (artist_name)),
("WA", "Add new songs and send ALL new artists to allowlist"),
("B", "Add %s to blocklist" % (artist_name)),
("A", "Only add to new songs by %s" % (artist_name)),
("AA", "Add new songs from ALL new artists"),
("BA", "Send ALL new artists to blocklist"),
]
choices = standard_choices
#DIALOG.clear()
size = get_window_size((8, len(choices)),
(22, max(len(max(choices, key=lambda item: len(item[1]))[1]),
len(artist_name))))
code, tag = DIALOG.menu(text, choices=choices, no_tags=True, colors=True,
height=size[0], width=size[1])
if code != DIALOG.OK:
return False
if tag in ("AA", "WA", "IA", "BA"):
skip_all = True
size = get_window_size(None, (32, len(max(inv_artists_dict, key=len))))
DIALOG.gauge_start(text=r"Finding tracks by \Zb%s\Zn" % (artist_name),
colors=True, percent=round((i/len(ARTISTS_SET))*100))
else:
continue
if not new_artist or tag in ("A", "AA"):
check_artist_albums(spot_conn, artist_info)
elif tag in ("W", "WA"):
fi.add_to_list(fi.Lists.ALLOWLIST, artist_info)
fi.sort_list(fi.Lists.ALLOWLIST)
check_artist_albums(spot_conn, artist_info)
elif tag in ("B", "BA"):
fi.add_to_list(fi.Lists.BLOCKLIST, artist_info)
fi.sort_list(fi.Lists.BLOCKLIST)
elif tag in ("I", "IA"):
pass
else:
pass
DIALOG.gauge_stop()
return True
def get_songs(spot_conn, state):
"""
Fetch songs from any source
"""
source = conf.get_key('Other', 'source')
if state == States.TOP_10_GREY:
get_artists_from_list(spot_conn, fi.Lists.GREYLIST)
get_top_songs(spot_conn)
elif state == States.NEW_RELEASES:
if source == 'allowlist':
get_artists_from_list(spot_conn, fi.Lists.ALLOWLIST)
elif source == 'saved':
get_artists_from_followed(spot_conn)
else: # Playlist(s)
if not get_artists_from_playlist(spot_conn):
state = States.START