added synced lyrics
This commit is contained in:
@@ -13,6 +13,8 @@ import os
|
||||
import pyautogui
|
||||
import threading
|
||||
import platform
|
||||
import syncedlyrics
|
||||
import textwrap
|
||||
|
||||
# Set the Spotify app's client ID and client secret
|
||||
client_id = "69b82a34d0fb40be80b020eae8e80f25"
|
||||
@@ -76,8 +78,13 @@ def oauthLogin():
|
||||
threading.Thread(target=oauthLogin).start()
|
||||
|
||||
token = spotipy.util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
|
||||
|
||||
# Create a Spotify object with the user's authorization token
|
||||
spotify = spotipy.Spotify(auth=token)
|
||||
def createToken():
|
||||
spotify = spotipy.Spotify(auth=token)
|
||||
return spotify
|
||||
|
||||
spotify = createToken()
|
||||
|
||||
# Create the tkinter window
|
||||
root = ttk.Tk()
|
||||
@@ -160,11 +167,24 @@ def get_devices():
|
||||
unloadSearching_Devices()
|
||||
loadDevices_list()
|
||||
devices_list.delete(0, ttk.END)
|
||||
try:
|
||||
list_of_devices = spotify.devices()
|
||||
except (spotipy.exceptions.SpotifyException, requests.exceptions.HTTPError):
|
||||
createToken()
|
||||
pass
|
||||
list_of_devices = spotify.devices()
|
||||
for num_of_device, garbage in enumerate(list_of_devices["devices"]):
|
||||
devices_list.insert(num_of_device, list_of_devices["devices"][num_of_device]["name"])
|
||||
root.after(6500, get_devices)
|
||||
|
||||
def loadLyrics_pressed():
|
||||
unloadNow_playing()
|
||||
loadLyrics()
|
||||
|
||||
def unloadLyrics_pressed():
|
||||
unloadLyrics()
|
||||
loadNow_playing()
|
||||
|
||||
# def hide_devices():
|
||||
# get_devices_button.grid()
|
||||
# devices_list.grid_remove()
|
||||
@@ -176,6 +196,7 @@ play_img = ttk.PhotoImage(file="icons/play-circle-x2.png")
|
||||
pause_img = ttk.PhotoImage(file="icons/pause-circle-x2.png")
|
||||
next_img = ttk.PhotoImage(file="icons/skip-next-x2.png")
|
||||
previous_img = ttk.PhotoImage(file="icons/skip-previous-x2.png")
|
||||
lyrics_img = ttk.PhotoImage(file="icons/lyrics.png")
|
||||
# album_art_img = ttk.PhotoImage(file="album_art.png")
|
||||
|
||||
# canvas = ttk.Canvas(root, width=480, height=320)
|
||||
@@ -184,6 +205,7 @@ previous_img = ttk.PhotoImage(file="icons/skip-previous-x2.png")
|
||||
|
||||
frame_artist_song = tk.Frame(root)
|
||||
frame_controls = tk.Frame(root)
|
||||
lyrics_button = tk.Frame(root)
|
||||
|
||||
root.grid_rowconfigure(0, weight=1)
|
||||
root.grid_rowconfigure(1, weight=1)
|
||||
@@ -219,13 +241,21 @@ progress_bar = tk.Progressbar(root, orient=ttk.HORIZONTAL, length=480)
|
||||
searching_for_devices_label = tk.Label(root, text="Searching for Devices...", font=("Helvetica", 24))
|
||||
device_name_label = tk.Label(frame_artist_song, text="", font=("Helvetica", 12))
|
||||
# background_image_label = tk.Label(root, image=album_art_img)
|
||||
lyrics_label = tk.Label(root, text="", font=("Helvetica", 32))
|
||||
loadLyrics_button = ttk.Button(lyrics_button, image=lyrics_img, command=loadLyrics_pressed, borderwidth=0)
|
||||
now_playing_button = tk.Button(root, text="Now Playing", command=unloadLyrics_pressed)
|
||||
|
||||
root.bind("<Return>", search)
|
||||
|
||||
# Function to update the song label with the current track's name
|
||||
def update_song_label():
|
||||
global lrc
|
||||
# Get the current playback information
|
||||
current_playback = spotify.current_playback()
|
||||
try:
|
||||
current_playback = spotify.current_playback()
|
||||
except (spotipy.exceptions.SpotifyException, requests.exceptions.HTTPError):
|
||||
createToken()
|
||||
pass
|
||||
# If there is no current playback, set the text of the song label to "No playback"
|
||||
if current_playback is None:
|
||||
# nothing_playing_obj = '{"item": {"artists": [{"name": "Nothing Playing"}],"duration_ms": 0,"name": "Nothing Playing"},"progress_ms": 0}'
|
||||
@@ -233,6 +263,12 @@ def update_song_label():
|
||||
get_devices()
|
||||
# Update the song label every 1 second
|
||||
else:
|
||||
try:
|
||||
track_name = current_playback["item"]["name"]
|
||||
pass
|
||||
except TypeError:
|
||||
sleep(1)
|
||||
update_song_label()
|
||||
track_name = current_playback["item"]["name"]
|
||||
artist_name = current_playback["item"]["artists"][0]["name"]
|
||||
track_duration = current_playback["item"]["duration_ms"]
|
||||
@@ -255,11 +291,17 @@ def update_song_label():
|
||||
# print("this is the same image")
|
||||
# else:
|
||||
# pass
|
||||
loadNow_playing()
|
||||
# loadNow_playing()
|
||||
if track_name == song_label.cget("text"):
|
||||
track_progress_label.config(text=("{}:{:02d}".format(track_progress_min, track_progress_sec)))
|
||||
track_progress_formatted = ("{}:{:02d}".format(track_progress_min, track_progress_sec))
|
||||
track_progress_label.config(text=track_progress_formatted)
|
||||
progress_bar.config(maximum=track_duration)
|
||||
progress_bar.config(value=track_progress)
|
||||
for line in str(lrc).splitlines():
|
||||
if track_progress_formatted in line:
|
||||
lyric = line.split("]")[1]
|
||||
wrapped_lyric = textwrap.fill(lyric, 21)
|
||||
lyrics_label.config(text=wrapped_lyric)
|
||||
root.after(1000, update_song_label)
|
||||
else:
|
||||
# album_art_data = Image.open(requests.get(album_art_url, stream=True).raw)
|
||||
@@ -269,6 +311,7 @@ def update_song_label():
|
||||
artist_label.config(text=artist_name)
|
||||
track_duration_label.config(text=("{}:{:02d}".format(track_duration_min, track_duration_sec)))
|
||||
volumeslider_button.set(value=current_volume)
|
||||
lrc = syncedlyrics.search("[" + track_name + "] [" + artist_name + "]")
|
||||
root.after(1000, update_song_label)
|
||||
# if album_art_url == "12345":
|
||||
# open_url = urlopen(album_art_url)
|
||||
@@ -290,9 +333,11 @@ def update_song_label():
|
||||
def loadNow_playing():
|
||||
# background_image_label.place(x=0, y=0)
|
||||
volumeslider_button.grid(row=1, column=1, rowspan=3, sticky="e", padx=(0,20))
|
||||
lyrics_button.grid(row=1, column=1, padx=(0,380))
|
||||
frame_artist_song.grid(row=1, column=1, pady=(0,5))
|
||||
frame_controls.grid(row=2, column=1, pady=(20,0))
|
||||
device_name_label.grid(pady=(0,10))
|
||||
loadLyrics_button.grid()
|
||||
device_name_label.grid()
|
||||
artist_label.grid()
|
||||
song_label.grid()
|
||||
previous_button.grid(row=0, column=0, padx=(0,10))
|
||||
@@ -310,6 +355,7 @@ def unloadNow_playing():
|
||||
play_button.grid_forget()
|
||||
next_button.grid_forget()
|
||||
progress_bar.grid_forget()
|
||||
loadLyrics_button.grid_forget()
|
||||
|
||||
def loadDevices_list():
|
||||
devices_list.grid(row=1, column=1, pady=10)
|
||||
@@ -325,11 +371,17 @@ def loadSearching_Devices():
|
||||
def unloadSearching_Devices():
|
||||
searching_for_devices_label.grid_forget()
|
||||
|
||||
def loadLyrics():
|
||||
now_playing_button.grid(row=0, column=1)
|
||||
lyrics_label.grid(row=1, column=1, pady=20)
|
||||
|
||||
def unloadLyrics():
|
||||
now_playing_button.grid_forget()
|
||||
lyrics_label.grid_forget()
|
||||
|
||||
# Start updating the song label
|
||||
|
||||
wait_for_connection()
|
||||
loadNow_playing()
|
||||
update_song_label()
|
||||
|
||||
# Run the GUI
|
||||
|
||||
Reference in New Issue
Block a user