added liking songs by tapping album art

liked songs change play button to heart
This commit is contained in:
Brandon4466
2023-03-04 17:17:36 -08:00
parent 88c877c50f
commit 8a5afa9a36
6 changed files with 44 additions and 22 deletions

2
.cache
View File

@@ -1 +1 @@
{"access_token": "BQC05wxeoZyHXRQVuQgm4-1Zqp2Uv8hf7Hg-wMyp0R7IIQMHwzLVINH31DaxsxO3aU_fCt9a3a4hihcgqZbVACu4IZLGU1TtN_P4mBpKbftuIxiDH7UMaOIGuwhvDKKkr0Zm_jqflBuJ0wdcaAD2CkYBzevBfMsXjtO87iD8ZoWwCh5mzWicZKnHho-tAlDaDI0FY-cgU_Xe_fU0S1Hf", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "AQCCSkV9mWV-ysqv_bMMERsUGgz7baaOfBGhqm2-6_3I-lpd2CKy8bs3GS_kNZy2D1YG6Z2dhKVUNTCE7MC0qcKypWZAvRw6nRlsM8816mTP6hpK67WWAuwq_qfnLVia1Oo", "scope": "user-library-modify user-library-read user-modify-playback-state user-read-playback-state", "expires_at": 1677969435} {"access_token": "BQDMY3TJbe_zGpD0_Hbhitu8zVPEwuXwPDv-KkVhqZ_itVTbYbUdwoqkUl0xFTpReD9_Hgix6RRsM7HyeWpr6r02dlVG8EzbVf9CDNclFSDH56lplmN_H8wbyZrz1DGqow-uVUZJpfDYrPizY2GclbsJLNj3pxguVNPwfbkLXjiOBK9LO1M4A-eoA8YzTCFxhdL5CC-tW2lucUDEaEpd", "token_type": "Bearer", "expires_in": 3600, "scope": "user-library-modify user-library-read user-modify-playback-state user-read-playback-state", "expires_at": 1677981348, "refresh_token": "AQDMo7jMg-YY6MpV64feNjby-v67EjBDVFxHtJM4Uax4geoYYKU_bYmu8Mpz-7h6XjTurxvaoUZSIyxr1b4dnLgnOwfvCtS2ozFGXifAXl7R4wMjCY79BvGesxGogf24j58"}

BIN
icons/pause-heart-black.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 983 B

BIN
icons/pause-heart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
icons/play-heart-black.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
icons/play-heart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@@ -97,8 +97,20 @@ def controlPrevious():
def likeSong(): def likeSong():
if spotify.current_user_saved_tracks_contains(tracks=[(spotify.current_playback()["item"]["id"])])[0] is False: if spotify.current_user_saved_tracks_contains(tracks=[(spotify.current_playback()["item"]["id"])])[0] is False:
spotify.current_user_saved_tracks_add(tracks=[(spotify.current_playback()["item"]["id"])]) spotify.current_user_saved_tracks_add(tracks=[(spotify.current_playback()["item"]["id"])])
if is_bright is True:
play_button.config(image=play_heart_img_black)
pause_button.config(image=pause_heart_img_black)
else:
play_button.config(image=play_heart_img)
pause_button.config(image=pause_heart_img)
else: else:
spotify.current_user_saved_tracks_delete(tracks=[(spotify.current_playback()["item"]["id"])]) spotify.current_user_saved_tracks_delete(tracks=[(spotify.current_playback()["item"]["id"])])
if is_bright is True:
play_button.config(image=play_img_black)
pause_button.config(image=pause_img_black)
else:
play_button.config(image=play_img)
pause_button.config(image=pause_img)
def start_playback_on_device(): def start_playback_on_device():
# global count # global count
@@ -229,17 +241,21 @@ def getLyrics(artist_name, track_name):
play_img = ttk.PhotoImage(file="icons/play.png") play_img = ttk.PhotoImage(file="icons/play.png")
pause_img = ttk.PhotoImage(file="icons/pause.png") pause_img = ttk.PhotoImage(file="icons/pause.png")
play_heart_img = ttk.PhotoImage(file="icons/play-heart.png")
pause_heart_img = ttk.PhotoImage(file="icons/pause-heart.png")
next_img = ttk.PhotoImage(file="icons/next.png") next_img = ttk.PhotoImage(file="icons/next.png")
previous_img = ttk.PhotoImage(file="icons/previous.png") previous_img = ttk.PhotoImage(file="icons/previous.png")
play_img_black = ttk.PhotoImage(file="icons/play-black.png") play_img_black = ttk.PhotoImage(file="icons/play-black.png")
pause_img_black = ttk.PhotoImage(file="icons/pause-black.png") pause_img_black = ttk.PhotoImage(file="icons/pause-black.png")
play_heart_img_black = ttk.PhotoImage(file="icons/play-heart-black.png")
pause_heart_img_black = ttk.PhotoImage(file="icons/pause-heart-black.png")
next_img_black = ttk.PhotoImage(file="icons/next-black.png") next_img_black = ttk.PhotoImage(file="icons/next-black.png")
previous_img_black = ttk.PhotoImage(file="icons/previous-black.png") previous_img_black = ttk.PhotoImage(file="icons/previous-black.png")
album_art_img = "" album_art_img = ""
frame_artist_song = ttk.Frame(root, width=(1280/3), height=400, bg=bg_color) frame_artist_song = ttk.Frame(root, width=(1280/3), height=400, bg=bg_color)
album_art_frame = ttk.Frame(root, bg=bg_color) album_art_frame = ttk.Frame(root)
lyrics_label_frame = ttk.Frame(root, width=(1280/3), height=400, bg=bg_color) lyrics_label_frame = ttk.Frame(root, width=(1280/3), height=400, bg=bg_color)
lyrics_label_frame.grid_propagate(0) lyrics_label_frame.grid_propagate(0)
@@ -270,20 +286,19 @@ device_name_label = tk.Label(frame_artist_song, text="", font=("Helvetica", 12))
lyrics_label = tk.Label(lyrics_label_frame, text="", font=("Helvetica", 32), wraplength=(1280/3), justify=ttk.CENTER, background=bg_color) lyrics_label = tk.Label(lyrics_label_frame, text="", font=("Helvetica", 32), wraplength=(1280/3), justify=ttk.CENTER, background=bg_color)
album_art_label = tk.Label(album_art_frame, image=album_art_img) album_art_label = tk.Label(album_art_frame, image=album_art_img)
play_button.configure()
play_button.bind("<Button-1>", lambda e:controlPlay()) play_button.bind("<Button-1>", lambda e:controlPlay())
pause_button.bind("<Button-1>", lambda e:controlPause()) pause_button.bind("<Button-1>", lambda e:controlPause())
next_button.bind("<Button-1>", lambda e:controlNext()) next_button.bind("<Button-1>", lambda e:controlNext())
previous_button.bind("<Button-1>", lambda e:controlPrevious()) previous_button.bind("<Button-1>", lambda e:controlPrevious())
album_art_label.bind("<Double-Button-1>", lambda e:likeSong()) album_art_label.bind("<Button-1>", lambda e:likeSong())
# devices_list.bind("<Button-1>", lambda e:wakeup()) # devices_list.bind("<Button-1>", lambda e:wakeup())
import json import json
# Function to update the song label with the current track's name # Function to update the song label with the current track's name
def update_song_label(): def update_song_label():
global lrc global lrc
global album_art_img global album_art_img
global is_bright
# Get the current playback information # Get the current playback information
current_playback = spotify.current_playback() current_playback = spotify.current_playback()
# If there is no current playback, set the text of the song label to "No playback" # If there is no current playback, set the text of the song label to "No playback"
@@ -294,26 +309,14 @@ def update_song_label():
else: else:
if current_playback.get("item"): if current_playback.get("item"):
track_name = current_playback["item"]["name"] track_name = current_playback["item"]["name"]
artist_name = current_playback["item"]["artists"][0]["name"]
track_duration = current_playback["item"]["duration_ms"]
track_progress = current_playback["progress_ms"] track_progress = current_playback["progress_ms"]
playing_status = current_playback["is_playing"] playing_status = current_playback["is_playing"]
device_name = current_playback["device"]["name"]
album_art_url = current_playback["item"]["album"]["images"][0]["url"]
track_progress_min = track_progress//(1000*60)%60 track_progress_min = track_progress//(1000*60)%60
track_progress_sec = (track_progress//1000)%60 track_progress_sec = (track_progress//1000)%60
else: else:
track_name = "Loading..." track_name = "Loading..."
artist_name = ""
track_duration = 1
playing_status = True
device_name = ""
album_art_url = ""
track_progress_min = 0
track_progress_sec = 1
if track_name == song_label.cget("text"): if track_name == song_label.cget("text"):
track_progress_formatted = ("{}:{:02d}".format(track_progress_min, track_progress_sec)) track_progress_formatted = ("{}:{:02d}".format(track_progress_min, track_progress_sec))
progress_bar.config(maximum=track_duration)
progress_bar.config(value=track_progress) progress_bar.config(value=track_progress)
for line in str(lrc).splitlines(): for line in str(lrc).splitlines():
if track_progress_formatted in line: if track_progress_formatted in line:
@@ -321,10 +324,16 @@ def update_song_label():
lyrics_label.config(text=lyric) lyrics_label.config(text=lyric)
root.after(800, update_song_label) root.after(800, update_song_label)
else: else:
artist_name = current_playback["item"]["artists"][0]["name"]
threading.Thread(target=getLyrics, args=(artist_name, track_name)).start() threading.Thread(target=getLyrics, args=(artist_name, track_name)).start()
track_id = current_playback["item"]["id"]
track_duration = current_playback["item"]["duration_ms"]
device_name = current_playback["device"]["name"]
album_art_url = current_playback["item"]["album"]["images"][0]["url"]
device_name_label.config(text=device_name) device_name_label.config(text=device_name)
song_label.config(text=track_name) song_label.config(text=track_name)
artist_label.config(text=artist_name) artist_label.config(text=artist_name)
progress_bar.config(maximum=track_duration)
lyrics_label.config(text="") lyrics_label.config(text="")
album_art_img_data = requests.get(album_art_url).content album_art_img_data = requests.get(album_art_url).content
album_art_img_open = Image.open(BytesIO(album_art_img_data)) album_art_img_open = Image.open(BytesIO(album_art_img_data))
@@ -348,22 +357,36 @@ def update_song_label():
previous_button.config(background=bg_color) previous_button.config(background=bg_color)
lyrics_label_frame.config(background=bg_color) lyrics_label_frame.config(background=bg_color)
lyrics_label.config(background=bg_color) lyrics_label.config(background=bg_color)
if spotify.current_user_saved_tracks_contains(tracks=[track_id])[0] is True:
liked_song = True
else:
liked_song = False
if math.sqrt(0.299 * (get_colors(album_art_img_open)[0] ** 2) + 0.587 * (get_colors(album_art_img_open)[1] ** 2) + 0.114 * (get_colors(album_art_img_open)[2] ** 2)) > 170: if math.sqrt(0.299 * (get_colors(album_art_img_open)[0] ** 2) + 0.587 * (get_colors(album_art_img_open)[1] ** 2) + 0.114 * (get_colors(album_art_img_open)[2] ** 2)) > 170:
is_bright = True
if liked_song is True:
play_button.config(image=play_heart_img_black)
pause_button.config(image=pause_heart_img_black)
else:
play_button.config(image=play_img_black)
pause_button.config(image=pause_img_black)
song_label.config(foreground="black") song_label.config(foreground="black")
artist_label.config(foreground="black") artist_label.config(foreground="black")
device_name_label.config(foreground="black") device_name_label.config(foreground="black")
lyrics_label.config(foreground="black") lyrics_label.config(foreground="black")
play_button.config(image=play_img_black)
pause_button.config(image=pause_img_black)
next_button.config(image=next_img_black) next_button.config(image=next_img_black)
previous_button.config(image=previous_img_black) previous_button.config(image=previous_img_black)
else: else:
is_bright = False
if liked_song is True:
play_button.config(image=play_heart_img)
pause_button.config(image=pause_heart_img)
else:
play_button.config(image=play_img)
pause_button.config(image=pause_img)
song_label.config(foreground="white") song_label.config(foreground="white")
artist_label.config(foreground="white") artist_label.config(foreground="white")
device_name_label.config(foreground="white") device_name_label.config(foreground="white")
lyrics_label.config(foreground="white") lyrics_label.config(foreground="white")
play_button.config(image=play_img)
pause_button.config(image=pause_img)
next_button.config(image=next_img) next_button.config(image=next_img)
previous_button.config(image=previous_img) previous_button.config(image=previous_img)
# print(oauth.get_cached_token()["access_token"]) # print(oauth.get_cached_token()["access_token"])
@@ -378,7 +401,6 @@ def update_song_label():
elif playing_status == False: elif playing_status == False:
pause_button.grid_forget() pause_button.grid_forget()
play_button.grid(row=3, column=1, pady=(100,0)) play_button.grid(row=3, column=1, pady=(100,0))
else: else:
pass pass