139 lines
5 KiB
Python
Executable file
139 lines
5 KiB
Python
Executable file
import tkinter as tk
|
|
from tkinter import ttk
|
|
from PIL import Image, ImageTk
|
|
import requests
|
|
from io import BytesIO
|
|
import spotipy
|
|
from spotipy.oauth2 import SpotifyOAuth
|
|
|
|
# Replace these with your Spotify app credentials
|
|
SPOTIPY_CLIENT_ID = 'add-your-client-id-here'
|
|
SPOTIPY_CLIENT_SECRET = 'add-your-client-secret-here'
|
|
SPOTIPY_REDIRECT_URI = 'http://localhost:8888/callback'
|
|
|
|
# Initialize Spotipy client with authorization
|
|
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
|
|
client_secret=SPOTIPY_CLIENT_SECRET,
|
|
redirect_uri=SPOTIPY_REDIRECT_URI,
|
|
scope='user-read-playback-state user-modify-playback-state'))
|
|
|
|
def get_current_playback():
|
|
try:
|
|
playback = sp.current_playback()
|
|
if playback is None or playback['item'] is None:
|
|
return None, None, None, None, None
|
|
track_name = playback['item']['name']
|
|
artists = ', '.join([artist['name'] for artist in playback['item']['artists']])
|
|
album_art_url = playback['item']['album']['images'][0]['url']
|
|
progress_ms = playback['progress_ms']
|
|
duration_ms = playback['item']['duration_ms']
|
|
return track_name, artists, album_art_url, progress_ms, duration_ms
|
|
except Exception as e:
|
|
print(f"Error fetching playback: {e}")
|
|
return None, None, None, None, None
|
|
|
|
def update_playback_info():
|
|
track_name, artists, album_art_url, progress_ms, duration_ms = get_current_playback()
|
|
if track_name and artists and album_art_url:
|
|
root.title(f"{track_name} - {artists}") # Update window title
|
|
track_label.config(text=f"{track_name}")
|
|
artist_label.config(text=f"{artists}")
|
|
response = requests.get(album_art_url)
|
|
img_data = response.content
|
|
img = Image.open(BytesIO(img_data))
|
|
img = img.resize((200, 200), Image.Resampling.LANCZOS)
|
|
img_tk = ImageTk.PhotoImage(img)
|
|
cover_label.config(image=img_tk)
|
|
cover_label.image = img_tk
|
|
if progress_ms and duration_ms:
|
|
progress_percentage = (progress_ms / duration_ms) * 100
|
|
progress_var.set(progress_percentage)
|
|
current_time = format_time(progress_ms)
|
|
total_time = format_time(duration_ms)
|
|
progress_label_current.config(text=f"{current_time}")
|
|
progress_label_total.config(text=f"{total_time}")
|
|
else:
|
|
progress_var.set(0)
|
|
progress_label_current.config(text="")
|
|
progress_label_total.config(text="")
|
|
else:
|
|
root.title("Spotify Now Playing")
|
|
track_label.config(text="No track playing")
|
|
artist_label.config(text="")
|
|
cover_label.config(image="")
|
|
cover_label.image = None
|
|
progress_var.set(0)
|
|
progress_label_current.config(text="")
|
|
progress_label_total.config(text="")
|
|
root.after(1000, update_playback_info) # Update every second
|
|
|
|
def format_time(ms):
|
|
seconds = ms // 1000
|
|
minutes = seconds // 60
|
|
seconds = seconds % 60
|
|
return f"{minutes}:{seconds:02d}"
|
|
|
|
def play_song():
|
|
sp.start_playback()
|
|
|
|
def pause_song():
|
|
sp.pause_playback()
|
|
|
|
def next_song():
|
|
sp.next_track()
|
|
|
|
def previous_song():
|
|
sp.previous_track()
|
|
|
|
# Create the main window
|
|
root = tk.Tk()
|
|
root.title("Spotify Now Playing")
|
|
|
|
# Create and place the widgets
|
|
cover_label = ttk.Label(root)
|
|
cover_label.pack(pady=(10, 5))
|
|
|
|
track_label = ttk.Label(root, text="Track: ", font=('Helvetica', 12, 'bold'))
|
|
track_label.pack(padx=20, pady=5)
|
|
|
|
artist_label = ttk.Label(root, text="Artists: ", font=('Helvetica', 10))
|
|
artist_label.pack(padx=20, pady=5)
|
|
|
|
# Progress bar with current and total time labels
|
|
progress_var = tk.DoubleVar()
|
|
progress_frame = ttk.Frame(root)
|
|
progress_frame.pack(fill='x', padx=20, pady=10)
|
|
|
|
progress_bar = ttk.Progressbar(progress_frame, variable=progress_var, maximum=100)
|
|
progress_bar.pack(fill='x', side='top')
|
|
|
|
progress_labels_frame = ttk.Frame(progress_frame)
|
|
progress_labels_frame.pack(fill='x', side='top')
|
|
|
|
progress_label_current = tk.Label(progress_labels_frame, text="", anchor='w')
|
|
progress_label_current.pack(side='left')
|
|
|
|
progress_label_total = tk.Label(progress_labels_frame, text="", anchor='e')
|
|
progress_label_total.pack(side='right')
|
|
|
|
# Playback controls
|
|
control_frame = ttk.Frame(root)
|
|
control_frame.pack(pady=10)
|
|
|
|
prev_button = ttk.Button(control_frame, text="Previous", command=previous_song)
|
|
prev_button.grid(row=0, column=0, padx=5)
|
|
|
|
play_button = ttk.Button(control_frame, text="Play", command=play_song)
|
|
play_button.grid(row=0, column=1, padx=5)
|
|
|
|
pause_button = ttk.Button(control_frame, text="Pause", command=pause_song)
|
|
pause_button.grid(row=0, column=2, padx=5)
|
|
|
|
next_button = ttk.Button(control_frame, text="Next", command=next_song)
|
|
next_button.grid(row=0, column=3, padx=5)
|
|
|
|
# Start updating playback info
|
|
update_playback_info()
|
|
|
|
# Start the Tkinter event loop
|
|
root.mainloop()
|