-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
1631 lines (1369 loc) · 79.4 KB
/
main.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 contextlib
import datetime
import json
import math
import multiprocessing
import os
import time
import tkinter as tk
import tkinter.font as tk_font
import urllib.error
import urllib.parse
import urllib.request
from tkinter import ttk
import requests
import sv_ttk
import win32api
import win32con
from PIL import Image, ImageTk
from dotenv import load_dotenv
from screeninfo import get_monitors
from winsdk.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager
def get_lyrics(song):
print(f"Searching song: {song}")
try:
body = find_lyrics(song)
except KeyError: # Unknown cause. API returns invalid?
body = None
if body is None:
print("Failed to find song")
return None
song.update_info(body)
print("Song found")
print(f"Searching lyrics: {song}")
lyrics = get_unsynced_lyrics(song, body)
if lyrics is None:
print("Failed to find lyrics")
synced_lyrics = get_synced_lyrics(song, body)
if synced_lyrics is None:
print("Failed to find synced lyrics")
return {
"synced_lyrics": synced_lyrics,
"lyrics": lyrics,
"song_info": song.get_info()
}
def find_lyrics(song):
global lyric_fetch_attempt, schedule_retry_lyric_fetch_time
duration = song.duration / 1000 if song.duration else ""
params = {
"q_album": song.album,
"q_artist": song.artist,
"q_artists": song.artist,
"q_track": song.title,
"track_spotify_id": song.uri,
"q_duration": duration,
"f_subtitle_length": math.floor(duration) if duration else "",
"usertoken": musixmatch_token,
}
req = urllib.request.Request(musixmatch_base_url + urllib.parse.urlencode(params, quote_via=urllib.parse.quote), headers=musixmatch_headers)
try:
response = urllib.request.urlopen(req).read()
except (urllib.error.HTTPError, urllib.error.URLError, ConnectionResetError) as e:
print(repr(e))
return
r = json.loads(response.decode())
if r["message"]["header"]["status_code"] != 200 and r["message"]["header"].get("hint") == "renew":
print("Invalid token")
return
body = r["message"]["body"]["macro_calls"]
if body["matcher.track.get"]["message"]["header"]["status_code"] != 200:
if body["matcher.track.get"]["message"]["header"]["status_code"] == 404:
print("Song not found.")
elif body["matcher.track.get"]["message"]["header"]["status_code"] == 401:
print("Timed out. Change the token or wait a few minutes before trying again.")
create_notification(f"Timed out. Retrying in {lyric_fail_reattempt_time} seconds...", "#f95353")
schedule_retry_lyric_fetch_time = time.time() + lyric_fail_reattempt_time
if lyric_fetch_attempt is None:
lyric_fetch_attempt = 2 # Next fetch attempt number
else:
lyric_fetch_attempt += 1
root.after(3000, clear_notification)
else:
print(f"Requested error: {body['matcher.track.get']['message']['header']}")
create_notification(f"{body['matcher.track.get']['message']['header']}", "#f95353")
lyric_fetch_attempt = None
root.after(3000, clear_notification)
return
elif isinstance(body["track.lyrics.get"]["message"].get("body"), dict):
if body["track.lyrics.get"]["message"]["body"]["lyrics"]["restricted"]:
print("Restricted lyrics.")
create_notification("Lyrics are restricted.", "#f95353")
lyric_fetch_attempt = None
root.after(3000, clear_notification)
return
lyric_fetch_attempt = None
return body
def get_unsynced_lyrics(song, body):
if song.is_instrumental:
lines = ["Instrumental"]
elif song.has_unsynced:
lyrics_body = body["track.lyrics.get"]["message"].get("body")
if lyrics_body is None:
return None
if lyrics := lyrics_body["lyrics"]["lyrics_body"]:
lines = list(filter(None, lyrics.split("\n")))
else:
return None
else:
return None
return lines
def get_synced_lyrics(song, body):
if song.is_instrumental:
synced_lyrics = [[0, song.get_info()["duration"]], "Instrumental"]
elif song.has_synced:
subtitle_body = body["track.subtitles.get"]["message"].get("body")
if subtitle_body is None:
return None
subtitle = subtitle_body["subtitle_list"][0]["subtitle"]
if not subtitle:
return None
old_lyric_lines = subtitle["subtitle_body"].split("\n")
# Remove double instrumental sections
previous_line_lyrics = None
lyric_lines = []
for line in old_lyric_lines:
if line.split("]")[1].strip() == "" and previous_line_lyrics == "":
pass
else:
lyric_lines.append(line.strip())
previous_line_lyrics = line.split("]")[1].strip()
def timestamp_to_epoch(timestamp): # 00:00.00 str
epoch = 0
epoch += int(timestamp.split(":")[0]) * 60000
epoch += int(timestamp.split(":")[1].split(".")[0]) * 1000
epoch += int(timestamp.split(".")[1]) * 10
return epoch
synced_lyrics = []
# Add blank line from 0 to first line
first_line_epoch = timestamp_to_epoch(lyric_lines[0].split("] ")[0].replace("[", "").strip())
if first_line_epoch != 0: # If first line does not start at 0
synced_lyrics.append([[0, first_line_epoch], ""])
# Append all lines
for index, line in enumerate(lyric_lines):
# Lyric Start Epoch
lyric_start = timestamp_to_epoch(line.split("]")[0].replace("[", ""))
# Lyric End Epoch
if index + 1 == len(lyric_lines): # If iterating over last item in list
lyric_end = song.get_info()["duration"]
else:
lyric_end = timestamp_to_epoch(lyric_lines[index + 1].split("]")[0].replace("[", ""))
# Lyrics Line String
if line.split("]")[1] == "": # Current line is empty / is an instrumental section
lyric = "" # or "♪"
else:
lyric = line.split("] ")[1]
synced_lyrics.append([[lyric_start, lyric_end], lyric])
# Add blank line to end if not already existent
if synced_lyrics[-1][1] != "":
print(synced_lyrics[-1][0][1])
synced_lyrics.append([[synced_lyrics[-1][0][1], song.get_info()["duration"]], ""])
else:
return None
return synced_lyrics
def spotifyapi_get_playing(access_token):
response = None
try:
response = requests.get("https://api.spotify.com/v1/me/player/currently-playing",
headers={
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
})
return response.json()
except requests.exceptions.ConnectionError:
print("\033[91m[ERROR] Connection Failed - Check internet connection\033[0m")
return None
except json.decoder.JSONDecodeError:
print(response)
print("\033[91m[ERROR] Spotify not open - Cannot retrieve playing data\033[0m")
return None
def spotifyapi_get_playback_state(access_token):
response = None
try:
response = requests.get("https://api.spotify.com/v1/me/player",
headers={
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
})
return response.json()
except requests.exceptions.ConnectionError:
print("\033[91m[ERROR] Connection Failed - Check internet connection\033[0m")
return None
except json.decoder.JSONDecodeError:
print(response)
print("\033[91m[ERROR] Spotify not open - Cannot retrieve playback state\033[0m")
return None
def spotifyapi_refresh_token():
try:
print("Refreshing spotify token...")
query = "https://accounts.spotify.com/api/token"
response = requests.post(query, data={"grant_type": "refresh_token", "refresh_token": spotify_refresh_token}, headers={"Authorization": f"Basic {spotify_base64_token}"})
print("Posted request with response:")
print(f"\033[90mCode {response.status_code}: {response.reason}\033[0m")
with contextlib.suppress(json.decoder.JSONDecodeError):
print(f"\033[90m{response.json()}\033[0m")
print("Spotify token refreshed")
return response.json()["access_token"]
except requests.exceptions.ConnectionError:
print("\033[91m[ERROR] Connection Failed - Check internet connection\033[0m")
print("Retrying in 10 seconds...")
time.sleep(1000)
spotifyapi_refresh_token()
async def get_media_info():
sessions = await MediaManager.request_async()
if current_session := sessions.get_current_session():
print(current_session.source_app_user_model_id)
if current_session.source_app_user_model_id in ["Chrome", "Spotify.exe"]:
info = await current_session.try_get_media_properties_async()
info_dict = {song_attr: info.__getattribute__(song_attr) for song_attr in dir(info) if song_attr[0] != '_'} # song_attr[0] != '_' ignores system attributes
info_dict["genres"] = list(info_dict["genres"]) # Convert winsdk vector to list
print(info_dict)
return info_dict
return None
class Song(object):
def __init__(self, artist, title, album="", uri=""):
self.artist = artist
self.title = title
self.album = album
self.uri = uri
self.duration = 0
self.has_synced = False
self.has_unsynced = False
self.is_instrumental = False
self.lyrics = None
self.subtitles = None
self.coverart_url = None
def __str__(self) -> str:
return f"{self.artist} - {self.title}"
def get_info(self):
return {
"coverart_url": self.coverart_url,
"title": self.title,
"artist": self.artist,
"album": self.album,
"duration": self.duration,
"has_synced": self.has_synced,
"has_unsynced": self.has_unsynced,
"is_instrumental": self.is_instrumental
}
def update_info(self, body):
meta = body["matcher.track.get"]["message"]["body"]
if not meta:
return
coverart_sizes = ["100x100", "350x350", "500x500", "800x800"]
coverart_urls = list(filter(None, [meta["track"][f"album_coverart_{size}"] for size in coverart_sizes]))
self.coverart_url = coverart_urls[-1] if coverart_urls else None
self.title = meta["track"]["track_name"]
self.artist = meta["track"]["artist_name"]
self.album = meta["track"]["album_name"]
self.duration = meta["track"]["track_length"] * 1000
self.has_synced = meta["track"]["has_subtitles"]
self.has_unsynced = meta["track"]["has_lyrics"] # or meta["track"]["has_lyrics_crowd"]
self.is_instrumental = meta["track"]["instrumental"]
def round_rectangle_points(x1, y1, x2, y2, radius=25): # Generating points for a rounded rectangle
return [x1 + radius, y1,
x1 + radius, y1,
x2 - radius, y1,
x2 - radius, y1,
x2, y1,
x2, y1 + radius,
x2, y1 + radius,
x2, y2 - radius,
x2, y2 - radius,
x2, y2,
x2 - radius, y2,
x2 - radius, y2,
x1 + radius, y2,
x1 + radius, y2,
x1, y2,
x1, y2 - radius,
x1, y2 - radius,
x1, y1 + radius,
x1, y1 + radius,
x1, y1]
def create_notification(text, bg):
notification_text.config(text=text, bg=bg)
notification_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
canvas_notification.grid(row=2, column=0, sticky="nsew")
root.update()
def clear_notification():
notification_text.config(text="", bg="white")
notification_text.pack_forget()
canvas_notification.grid_forget()
root.update()
def make_api_call(spotify_access_token, code_directory):
global last_api_call_time
spotify_playing = spotifyapi_get_playing(spotify_access_token)
spotify_state = spotifyapi_get_playback_state(spotify_access_token)
last_api_call_time = time.time()
# print(f"\033[90m{spotify_playing}\033[0m")
if spotify_playing is None and spotify_state is None:
returns = {}
else:
returns = {
"api_call_timestamp": time.time(),
"spotify_playing": spotify_playing,
"spotify_state": spotify_state,
"last_updated_time": last_api_call_time,
"returns_received": False
}
with open(f"{code_directory}\\api_data.json", "w+") as file:
json.dump(returns, file, indent=4)
return
def get_api_data():
try:
with open(f"{code_directory}\\api_data.json", "r+") as file:
data = json.load(file)
except json.decoder.JSONDecodeError: # Occurs during the time when file is being written to
time.sleep(0.1)
print("Data retrieval reattempt")
data = get_api_data() # Reattempts to get data
return data
def create_rectangle(x1, y1, x2, y2):
global images
canvas_synced_lyrics.delete("rectangle")
images.clear()
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
alpha = int(.3 * 255) # .5 alpha
fill = root.winfo_rgb("#000000") + (alpha,)
x = max(x2 - x1, 0)
y = max(y2 - y1, 0)
image = Image.new("RGBA", (x, y), fill)
images.append(ImageTk.PhotoImage(image))
rectangle = canvas_synced_lyrics.create_image(x1, y1, image=images[-1], anchor="nw", tags="rectangle")
root.update()
return rectangle
def update_synced_lyrics(lyrics, duration): # sourcery skip: low-code-quality
global selected_lyric_line, rectangle_created, rectangle_status, rectangle, original_rect, rect_x1, rect_y1, rect_x2, rect_y2, target_x1, target_y1, target_x2, target_y2
global automatic_scroll, previous_auto_scroll_fraction
lyric_sync_buffer = -50 # Buffer in ms. Negative for earlier, positive for later.
duration = duration - lyric_sync_buffer
# rectangle_status: "creating" / "moving" / "teleporting" / "destroying" / None
if selected_lyric_line is None: # Song changed
print("Song changed, resetting lyric lines")
selected_lyric_line = 0
automatic_scroll = True
previous_auto_scroll_fraction = 0
canvas_synced_lyrics.yview_moveto(0)
for index, lyric_data in enumerate(lyrics): # Loop through lyric ranges
if lyric_data[0][0] <= duration <= lyric_data[0][1]: # If duration is in range, index is the current lyric line
if selected_lyric_line != index and rectangle_status is None: # If line changed, rectangle_status is None ensures animation is finished before continuing
target_text = lyrics_text_list[index] # May raise index error?
if canvas_synced_lyrics.itemcget(lyrics_text_list[index], "text").strip() == "":
target_x1, target_y1, target_x2, target_y2 = 5, canvas_synced_lyrics.bbox(target_text)[1], 5, canvas_synced_lyrics.bbox(target_text)[3] # Target location/bbox of the text
else:
target_x1, target_y1, target_x2, target_y2 = canvas_synced_lyrics.bbox(target_text)[0], canvas_synced_lyrics.bbox(target_text)[1], canvas_synced_lyrics.bbox(target_text)[2] + 2, canvas_synced_lyrics.bbox(target_text)[3] # Target location/bbox of the text
if rectangle_created and canvas_synced_lyrics.itemcget(lyrics_text_list[index], "text").strip() == "":
print("Blank line, destroying")
rectangle_status = "destroying"
# Rectangle not created and current line is not blank, create rectangle
if not rectangle_created and canvas_synced_lyrics.itemcget(lyrics_text_list[index], "text").strip() != "": # First line, start starting animation
print("Preparing to create")
rectangle_status = "creating"
elif rectangle_created and canvas_synced_lyrics.itemcget(lyrics_text_list[index], "text").strip() == "":
print("Blank line, destroying")
rectangle_status = "destroying"
# Expected lyric line, move down
elif index == selected_lyric_line + 1:
print(f"Next lyric line ({selected_lyric_line} -> {index})")
print("Moving rectangle down")
rectangle_status = "moving"
rect_x1 = canvas_synced_lyrics.bbox(rectangle)[0]
rect_y1 = canvas_synced_lyrics.bbox(rectangle)[1]
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2]
rect_y2 = canvas_synced_lyrics.bbox(rectangle)[3]
original_rect = (rect_x1, rect_y1, rect_x2, rect_y2)
# Unexpected jump, remove and create new rectangle
else:
print(f"Lyric line jumped ({selected_lyric_line} -> {index})")
print("Lyrics jumped, teleporting rectangle")
rectangle_status = "teleporting"
print(f"New lyric hover: \"{lyrics[index][1]}\"")
print(f"New lyric time range: {lyrics[index][0]}")
selected_lyric_line = index
break
if rectangle_status is not None:
if rectangle_status == "moving":
# x1 never changes, y1 changes for movement, x2 changes for line length, y2 changes for movement
# Move rectangle
# Movement push down y1
if round(rect_y1) >= target_y1: # Down movement is completed
rect_y1 = target_y1
else:
animation_completion = 1 - (target_y1 - rect_y1) / (target_y1 - original_rect[1]) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (target_y1 - original_rect[1])
try:
rect_y1 = canvas_synced_lyrics.bbox(rectangle)[1] + math.ceil(move_pixels)
except TypeError:
rectangle_status = None
print("TypeError raised: moving > push down y1")
# Movement push down y2
if round(rect_y2) >= target_y2: # Down movement is completed
rect_y2 = target_y2
else:
animation_completion = 1 - (target_y2 - rect_y2) / (target_y2 - original_rect[3]) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (target_y2 - original_rect[3])
try:
rect_y2 = canvas_synced_lyrics.bbox(rectangle)[3] + math.ceil(move_pixels)
except TypeError:
rectangle_status = None
print("TypeError raised: moving > push down y2")
# Calculate rectangle shape change
# Resize by adjusting x2
# sourcery skip: merge-duplicate-blocks, merge-else-if-into-elif
if original_rect[2] >= target_x2: # If rectangle has to shrink
if round(rect_x2) <= target_x2:
rect_x2 = target_x2
else:
animation_completion = 1 - (rect_x2 - target_x2) / (original_rect[2] - target_x2) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (original_rect[2] - target_x2)
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2] - math.ceil(move_pixels)
else: # If rectangle has to expand
if round(rect_x2) >= target_x2:
rect_x2 = target_x2
else:
animation_completion = 1 - (target_x2 - rect_x2) / (target_x2 - original_rect[2]) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (target_x2 - original_rect[2])
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2] + math.ceil(move_pixels)
rectangle = create_rectangle(rect_x1, rect_y1, rect_x2, rect_y2) # Create the rectangle
if rect_x1 == target_x1 and rect_y1 == target_y1 and rect_x2 == target_x2 and rect_y2 == target_y2: # If rectangle matches text box
print("Moving animation done")
rectangle_status = None
if rectangle_status == "teleporting":
rectangle = create_rectangle(target_x1, target_y1, target_x2, target_y2) # Create the rectangle
print("Teleport done")
rectangle_status = None
if rectangle_status == "destroying":
# Rectangle is created, prepare to destroy
if rectangle_created:
rectangle_created = False
rect_x1 = canvas_synced_lyrics.bbox(rectangle)[0]
rect_y1 = canvas_synced_lyrics.bbox(rectangle)[1]
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2]
rect_y2 = canvas_synced_lyrics.bbox(rectangle)[3]
original_rect = (rect_x1, rect_y1, rect_x2, rect_y2)
target_x1, target_y1, target_x2, target_y2 = (rect_x1 + rect_x2) / 2, rect_y1, (rect_x1 + rect_x2) / 2, rect_y2
# Rectangle being removed, continue to destroy
else:
# Movement up and down are not required because rectangle destroy pushes from both sides
# Movement push left
if round(rect_x1) >= target_x1: # Left movement is completed
rect_x1 = target_x1
else:
animation_completion = 1 - (target_x1 - rect_x1) / (target_x1 - original_rect[0]) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (target_x1 - original_rect[0])
rect_x1 = canvas_synced_lyrics.bbox(rectangle)[0] + math.ceil(move_pixels)
# Movement push right
if round(rect_x2) <= target_x2: # Right movement is completed
rect_x2 = target_x2
else:
animation_completion = 1 - (rect_x2 - target_x2) / (original_rect[2] - target_x2) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (original_rect[2] - target_x2)
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2] - math.ceil(move_pixels)
rectangle = create_rectangle(rect_x1, rect_y1, rect_x2, rect_y2) # Create the rectangle
if rect_x1 == target_x1 and rect_y1 == target_y1 and rect_x2 == target_x2 and rect_y2 == target_y2: # If rectangle matches text box
print("Destroy animation done")
canvas_synced_lyrics.delete("rectangle")
rectangle_status = None
if rectangle_status == "creating":
# Rectangle is not created, create a new rectangle
if not rectangle_created:
rectangle_created = True
rectangle = create_rectangle((target_x1 + target_x2) / 2, target_y1, (target_x1 + target_x2) / 2 + 1, target_y2) # Create first rectangle
rect_x1 = canvas_synced_lyrics.bbox(rectangle)[0]
rect_y1 = canvas_synced_lyrics.bbox(rectangle)[1] # Equal to target_y1
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2]
rect_y2 = canvas_synced_lyrics.bbox(rectangle)[3] # Equal to target_y2
original_rect = (rect_x1, rect_y1, rect_x2, rect_y2)
# Rectangle has already been created, expand rectangle
else:
# Movement up and down are not required because rectangle creation starts at y1 - y2
# Movement push left
if round(rect_x1) <= target_x1: # Left movement is completed
rect_x1 = target_x1
else:
animation_completion = 1 - (rect_x1 - target_x1) / (original_rect[0] - target_x1) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (original_rect[0] - target_x1)
rect_x1 = canvas_synced_lyrics.bbox(rectangle)[0] - math.ceil(move_pixels)
# Movement push right
if round(rect_x2) >= target_x2: # Right movement is completed
rect_x2 = target_x2
else:
animation_completion = 1 - (target_x2 - rect_x2) / (target_x2 - original_rect[2]) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
move_pixels = (animation_acceleration[animation_completion_percentage] / 100) / 2 * (target_x2 - original_rect[2])
rect_x2 = canvas_synced_lyrics.bbox(rectangle)[2] + math.ceil(move_pixels)
rectangle = create_rectangle(rect_x1, rect_y1, rect_x2, rect_y2) # Create the rectangle
# Rectangle matches the size of text box, create animation completed
if rect_x1 == target_x1 and rect_y1 == target_y1 and rect_x2 == target_x2 and rect_y2 == target_y2: # If rectangle matches text box
print("Creation animation done")
rectangle_status = None
# Automatic scroll
if lyrics_height is not None and lyrics_height > canvas_middle.winfo_height(): # If all lyrics cannot be displayed/lyrics are covered
# print("Scroll needed") # DEBUG
# print(canvas_synced_lyrics.winfo_reqheight())
if automatic_scroll: # If automatic scroll is enabled
if not synced_lyrics_scrollbar.winfo_ismapped(): # If scroll bar is not packed, pack
canvas_synced_lyrics.pack_forget()
synced_lyrics_scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Scrollbar must be packed before lyrics canvas
canvas_synced_lyrics.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Center rectangle to the middle of the screen
with contextlib.suppress(TypeError, tk.TclError): # TypeError caused by non-existent rectangle/rectangle has been destroyed. TclError unknown cause, occurs after advertisement.
rectangle_height = canvas_synced_lyrics.bbox(rectangle)[3] - canvas_synced_lyrics.bbox(rectangle)[1]
rectangle_y_pos = canvas_synced_lyrics.bbox(rectangle)[1] # y1 (Top left of rectangle)
rectangle_y_center = rectangle_y_pos + (rectangle_height / 2)
visible_canvas_height = canvas_middle.winfo_height()
# noinspection PyTypeChecker
fraction = (rectangle_y_center - (visible_canvas_height / 2)) / lyrics_height # (Center of rectangle - half of visible canvas height) / height of lyrics.
# If scroll wants to jump more than x pixels
fraction_per_pixel = 1 / visible_canvas_height
max_jump_range = 3 # Maximum jump in pixels
if previous_auto_scroll_fraction is not None and not previous_auto_scroll_fraction - (fraction_per_pixel * max_jump_range) < fraction < previous_auto_scroll_fraction + (fraction_per_pixel * max_jump_range): # If previous is not in range of current by 5 pixels
global target_fraction, start_fraction
# Start jump
target_fraction = float(fraction)
start_fraction = float(previous_auto_scroll_fraction)
# If currently jumping
if previous_auto_scroll_fraction is not None and target_fraction is not None and start_fraction is not None:
if target_fraction > start_fraction: # Going down
animation_completion = 1 - (target_fraction - previous_auto_scroll_fraction) / (target_fraction - start_fraction) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
if previous_auto_scroll_fraction >= target_fraction:
print("Jump down completed")
target_fraction = None
start_fraction = None
else:
fraction_change = (animation_acceleration[animation_completion_percentage] / 100) * (target_fraction - start_fraction) * 2 # x2 for double speed
fraction = previous_auto_scroll_fraction + fraction_change
elif target_fraction < start_fraction: # Going up
animation_completion = 1 - (previous_auto_scroll_fraction - target_fraction) / (start_fraction - target_fraction) # Animation completion percentage: 1 - Distance from current rectangle to target rectangle / Distance from original rectangle to target rectangle
animation_completion_percentage = int(min(round(animation_completion * 100, -1), 90))
if previous_auto_scroll_fraction <= target_fraction:
print("Jump up completed")
target_fraction = None
start_fraction = None
else:
fraction_change = (animation_acceleration[animation_completion_percentage] / 100) * (start_fraction - target_fraction) * 2 # x2 for double speed
fraction = previous_auto_scroll_fraction - fraction_change
previous_auto_scroll_fraction = float(fraction)
canvas_synced_lyrics.yview_moveto(fraction)
elif synced_lyrics_scrollbar.winfo_ismapped(): # If scroll bar is packed, unpack
synced_lyrics_scrollbar.pack_forget()
def updater(): # sourcery skip: low-code-quality
global track_info, lyrics, previous_track_info, previous_not_playing, track_start_time, playing, shuffle, repeat
global track_progress
global text_track_title_slide_completed, text_track_title_slide_queued, text_artists_slide_completed, text_artists_slide_queued, text_next_slide
global last_api_call_time, spotify_access_token, spotify_last_refresh_time, schedule_retry_lyric_fetch_time, lyric_fetch_attempt
global selected_lyric_line, rectangle_status, rectangle_created, lyrics_height
global root_after_id, multiprocessing_process_id
if override_cancel:
root_after_id = root.after(15, updater)
return
# Expected timing for in between API calls
with contextlib.suppress(TypeError): # TypeError: First run, track_info & playing = None
if playing: # If previously playing, playing starts as None
track_progress = ((time.time() - track_start_time) * 1000)
progress_bar["value"] = track_progress / track_info["duration_ms"] * 100
text_progress_start.config(text=datetime.datetime.fromtimestamp(track_progress / 1000).strftime("%M:%S"))
text_progress_end.config(text=datetime.datetime.fromtimestamp(track_info["duration_ms"] / 1000).strftime("%M:%S"))
root.update()
# Update synced lyrics
# noinspection PyUnresolvedReferences
if playing and lyrics is not None and lyrics["synced_lyrics"] is not None and lyrics["lyrics"] is not ["Instrumental"]:
try:
update_synced_lyrics(lyrics["synced_lyrics"], track_progress)
except TypeError:
# print("TypeError exception unhandled:")
# traceback.print_exc()
pass
# Sliding track title
if (text_track_title.winfo_width() > canvas_topbar.winfo_width() - exit_button.winfo_width() - minimize_button.winfo_width()) and (text_next_slide is None or time.time() >= text_next_slide): # If text track title does not fit and next slide is not queued or queued time is up
y_pos = text_track_title.winfo_y()
x_pos = text_track_title.winfo_x()
text_track_title.place_forget()
text_next_slide = None
if 6 >= x_pos >= 5 and not text_track_title_slide_queued: # If slide has completed a rotation
text_track_title.place(x=5, y=y_pos) # Fix minor differences
text_track_title_slide_completed = True
if text_track_title_slide_completed and text_artists_slide_completed and text_next_slide is None: # If both texts have finished sliding and next slide is not queued
text_next_slide = time.time() + 3 # Text stays for 3 seconds before sliding again
text_track_title_slide_queued = True
text_artists_slide_queued = True
elif (x_pos + text_track_title.winfo_width()) < 0: # If right side of text if past left side of parent, then restart slide
text_track_title.place(x=root.winfo_width() - exit_button.winfo_width(), y=y_pos)
else: # Slide text
text_track_title.place(x=x_pos - 1, y=y_pos) # Speed of text slide (int)
text_track_title_slide_queued = False
text_track_title_slide_completed = False
elif text_track_title.winfo_x() != 5: # Track title text does not need to slide but is not in correct position
y_pos = text_track_title.winfo_y()
text_track_title.place_forget()
text_track_title.place(x=5, y=y_pos)
# Sliding artists text
if (text_artists.winfo_width() > canvas_topbar.winfo_width() - exit_button.winfo_width()) and (text_next_slide is None or time.time() >= text_next_slide): # If text track title does not fit and next slide is not queued or queued time is up
y_pos = text_artists.winfo_y()
x_pos = text_artists.winfo_x()
text_artists.place_forget()
text_next_slide = None
if 6 >= x_pos >= 5 and not text_artists_slide_queued: # If slide has completed a rotation
text_artists.place(x=5, y=y_pos) # Fix minor differences
text_artists_slide_completed = True
if text_track_title_slide_completed and text_artists_slide_completed and text_next_slide is None: # If both texts have finished sliding and next slide is not queued
text_next_slide = time.time() + 3 # Text stays for 3 seconds before sliding again
text_track_title_slide_queued = True
text_artists_slide_queued = True
elif (x_pos + text_artists.winfo_width()) < 0: # If right side of text if past left side of parent, then restart slide
text_artists.place(x=root.winfo_width(), y=y_pos) # Exit button slide is not included because it is covering the text
else: # Slide text
text_artists.place(x=x_pos - 1, y=y_pos) # Speed of text slide (int)
text_artists_slide_queued = False
text_artists_slide_completed = False
elif text_artists.winfo_x() != 5: # Track title text does not need to slide but is not in correct position
y_pos = text_artists.winfo_y()
text_artists.place_forget()
text_artists.place(x=5, y=y_pos)
root.update()
# Create/Remove automatic scroll button
if not automatic_scroll: # If automatic scroll is disabled
if not lyrics_enable_auto_scroll.winfo_ismapped(): # If enable automatic scroll button is not placed
if synced_lyrics_scrollbar.winfo_ismapped():
lyrics_enable_auto_scroll.place(relx=1.0, rely=1.0, x=-20, y=-5, anchor="se")
else:
lyrics_enable_auto_scroll.place(relx=1.0, rely=1.0, x=-5, y=-5, anchor="se")
else: # If automatic scroll is enabled
if lyrics_enable_auto_scroll.winfo_ismapped(): # If enable automatic scroll button is placed
lyrics_enable_auto_scroll.place_forget()
# Refresh Spotify access token if necessary
if spotify_last_refresh_time < int(time.time()) - 1800:
print("Refreshing spotify token due to time out")
spotify_access_token = spotifyapi_refresh_token()
spotify_last_refresh_time = time.time()
# API call update every 1.5 seconds
if time.time() > last_api_call_time + 1.5:
last_api_call_time = time.time()
multiprocessing_process_id = multiprocessing.Process(target=make_api_call, args=(spotify_access_token, code_directory))
multiprocessing_process_id.start()
api_data = get_api_data()
if api_data != {} and not api_data["returns_received"]:
# Set returns_received to True to retrieve file after every update
with open(f"{code_directory}\\api_data.json", "r+") as file:
data = json.load(file)
data["returns_received"] = True
file.seek(0)
file.truncate(0)
json.dump(data, file, indent=4)
try:
api_call_timestamp = api_data["api_call_timestamp"]
spotify_playing = api_data["spotify_playing"]
spotify_state = api_data["spotify_state"]
track_info = {
"track_name": spotify_playing["item"]["name"],
"artists": [artist["name"] for artist in list(spotify_playing["item"]["artists"])],
"artist_names": ", ".join(artist["name"] for artist in list(spotify_playing["item"]["artists"])),
"album": spotify_playing["item"]["album"]["name"],
"album_cover_link": spotify_playing["item"]["album"]["images"][-1]["url"],
"duration_ms": spotify_playing["item"]["duration_ms"],
"link": spotify_playing["item"]["external_urls"]["spotify"],
"uri": spotify_playing["item"]["uri"]
}
track_progress = spotify_playing["progress_ms"]
playing = spotify_playing["is_playing"]
shuffle = spotify_state["shuffle_state"]
repeat = spotify_state["repeat_state"]
track_start_time = api_call_timestamp - (track_progress / 1000) # Use api_call_timestamp to prevent file save/recall delay
# Track Title and Artists Text
text_track_title.config(text=track_info["track_name"])
text_artists.config(text=track_info["artist_names"])
# Progress bar
progress_bar["value"] = track_progress / track_info["duration_ms"] * 100
# Progress texts
text_progress_start.config(text=datetime.datetime.fromtimestamp(track_progress / 1000).strftime("%M:%S"))
text_progress_end.config(text=datetime.datetime.fromtimestamp(track_info["duration_ms"] / 1000).strftime("%M:%S"))
# Play button
if playing:
image = ImageTk.PhotoImage(Image.open(f"{code_directory}/assets/pause.png").resize((35, 35), Image.Resampling.LANCZOS))
else:
image = ImageTk.PhotoImage(Image.open(f"{code_directory}/assets/play.png").resize((35, 35), Image.Resampling.LANCZOS))
button_play.config(image=image)
button_play.photo = image
# Shuffle button
if shuffle:
image = ImageTk.PhotoImage(Image.open(f"{code_directory}/assets/shuffle_selected.png").resize((15, 15), Image.Resampling.LANCZOS))
else:
image = ImageTk.PhotoImage(Image.open(f"{code_directory}/assets/shuffle.png").resize((15, 15), Image.Resampling.LANCZOS))
button_shuffle.config(image=image)
button_shuffle.photo = image
# Repeat button
if repeat is not None:
image = ImageTk.PhotoImage(Image.open(f"{code_directory}/assets/repeat_{repeat}.png").resize((15, 15), Image.Resampling.LANCZOS))
else:
image = ImageTk.PhotoImage(Image.open(f"{code_directory}/assets/repeat_off.png").resize((15, 15), Image.Resampling.LANCZOS))
button_repeat.config(image=image)
button_repeat.photo = image
root.update()
previous_not_playing = False
except (TypeError, json.decoder.JSONDecodeError, KeyError) as exception:
# TypeError: Advertisement playing
# JSONDecodeError: Not playing Spotify
# KeyError: Unknown cause
if not previous_not_playing:
print("Currently not playing")
previous_not_playing = True
playing = False
track_info = None
previous_track_info = None
# Track Title and Artists Text
if type(exception) == TypeError:
text_track_title.config(text="[Advertisement]")
else:
text_track_title.config(text="[Not Playing]")
text_artists.config(text="")
# Progress bar
progress_bar["value"] = 0
# Progress texts
text_progress_start.config(text="--:--")
text_progress_end.config(text="--:--")
# Lyrics
canvas_synced_lyrics.delete("all")
canvas_synced_lyrics.pack_forget()
synced_lyrics_scrollbar.pack_forget()
scroll_lyrics_listbox.delete(0, tk.END)
scroll_lyrics_listbox.pack_forget()
scroll_lyrics_scrollbar.pack_forget()
frame_lyrics_info.place_forget()
root.update()
root_after_id = root.after(15, updater)
return
# Song is different from previously/Song has changed
# noinspection PyTypeChecker
if track_info != previous_track_info or (schedule_retry_lyric_fetch_time is not None and time.time() >= schedule_retry_lyric_fetch_time):
schedule_retry_lyric_fetch_time = None
if track_info != previous_track_info:
previous_track_info = track_info.copy()
print("Song changed:")
print(f"\033[90m{track_info}\033[0m")
lyric_fetch_attempt = None
else:
print(f"Retrying lyric fetch... (Attempt {lyric_fetch_attempt})")
canvas_synced_lyrics.delete("all")
canvas_synced_lyrics.pack_forget()
synced_lyrics_scrollbar.pack_forget()
scroll_lyrics_listbox.delete(0, tk.END)
scroll_lyrics_listbox.pack_forget()
scroll_lyrics_scrollbar.pack_forget()
lyrics_center_text.config(text="Loading lyrics...")
if lyric_fetch_attempt is not None:
lyrics_center_subtitle.config(text=f"Attempt {lyric_fetch_attempt}")
frame_lyrics_info.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.update()
lyrics = get_lyrics(Song(track_info["artist_names"], track_info["track_name"]))
print(f"\033[90m{lyrics}\033[0m")
lyrics_center_subtitle.config(text="")
frame_lyrics_info.place_forget()
# Instrumental song
# noinspection PyTypeChecker
if lyrics is not None and (lyrics["song_info"])["is_instrumental"]:
lyrics_center_text.config(text="Instrumental")
frame_lyrics_info.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
# Synced lyrics
elif lyrics is not None and lyrics["synced_lyrics"] is not None:
print("Creating synced lyrics")
selected_lyric_line = None
rectangle_status = None
rectangle_created = False
lyrics_text_list.clear()
lyrics_height = 0
for i in range(len(lyrics["synced_lyrics"])):
if i == 0 or i + 1 == len(lyrics["synced_lyrics"]): # First and last item in list
font_size = 1
else:
font_size = 9
text = lyrics["synced_lyrics"][i][1]
lyrics_text = canvas_synced_lyrics.create_text(5, lyrics_height, text=text, font=(tk.font.nametofont("TkDefaultFont").actual()["family"], font_size), anchor=tk.NW, justify=tk.LEFT, width=canvas_synced_lyrics.winfo_width() - 10) # Max text width (-10 px for scroll bar)
lyrics_text_list.append(lyrics_text)
lyrics_height += canvas_synced_lyrics.bbox(lyrics_text)[3] - canvas_synced_lyrics.bbox(lyrics_text)[1]
lyrics_height += 2 # 2 px line spacing
root.update()
# Set scroll region
canvas_synced_lyrics_bbox = list(canvas_synced_lyrics.bbox("all"))
canvas_synced_lyrics_bbox[0] = canvas_synced_lyrics_bbox[0] - 5 # 5 px margin left
canvas_synced_lyrics_bbox[1] = canvas_synced_lyrics_bbox[1] - 5 # 5 px margin top
canvas_synced_lyrics_bbox = tuple(canvas_synced_lyrics_bbox) # Tuple needs to be converted to list then back to be edited
canvas_synced_lyrics.configure(scrollregion=canvas_synced_lyrics_bbox)
synced_lyrics_scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Scrollbar must be packed before lyrics canvas
canvas_synced_lyrics.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
elif lyrics is not None and lyrics["lyrics"] is not None:
print("Creating scrollable lyrics")
scroll_lyrics_listbox.insert(tk.END, "These lyrics are not synced yet.")
scroll_lyrics_listbox.insert(tk.END, "-------------------------------------")
for line in lyrics["lyrics"]:
scroll_lyrics_listbox.insert(tk.END, line)
scroll_lyrics_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(5, 0))
scroll_lyrics_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
else:
print("No lyrics")
lyrics_center_text.config(text="Lyrics Unavailable")
frame_lyrics_info.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.update()
root_after_id = root.after(15, updater)
if __name__ == "__main__":
# Code start
code_directory = os.path.dirname(os.path.realpath(__file__))
load_dotenv()
musixmatch_base_url = "https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get?format=json&namespace=lyrics_richsynched&subtitle_format=musixmatchm&app_id=web-desktop-app-v1.0&"
# noinspection SpellCheckingInspection
musixmatch_headers = {"authority": "apic-desktop.musixmatch.com", "cookie": "x-musixmatchm-token-guid="}
custom_musixmatch_token = os.getenv("MUSIXMATCH_TOKEN")
# noinspection SpellCheckingInspection
# If you do not have a musixmatch token, then use the following public token. This may not work 100% of the time.