diff --git a/nowplaying.py b/nowplaying.py new file mode 100644 index 0000000..13db67e --- /dev/null +++ b/nowplaying.py @@ -0,0 +1,108 @@ +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 = 'your_spotify_client_id' +SPOTIPY_CLIENT_SECRET = 'your_spotify_client_secret' +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: + track_label.config(text=f"Track: {track_name}") + artist_label.config(text=f"Artists: {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_var.set((progress_ms / duration_ms) * 100) + else: + track_label.config(text="No track playing") + artist_label.config(text="") + cover_label.config(image="") + cover_label.image = None + progress_var.set(0) + root.after(1000, update_playback_info) # Update every second + +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 +track_label = ttk.Label(root, text="Track: ") +track_label.pack(padx=20, pady=5) + +artist_label = ttk.Label(root, text="Artists: ") +artist_label.pack(padx=20, pady=5) + +cover_label = ttk.Label(root) +cover_label.pack(padx=20, pady=20) + +# Progress bar +progress_var = tk.DoubleVar() +progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100) +progress_bar.pack(fill='x', padx=20, pady=10) + +# 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()