update README.md and add v0.1

This commit is contained in:
rintyuu 2024-06-24 20:39:01 -07:00
parent 3f14a776eb
commit 79328a33fc
2 changed files with 141 additions and 0 deletions

View file

@ -14,4 +14,6 @@ import spotipy
from spotipy.oauth2 import SpotifyOAuth
```
in your spotify application in your developer portal, make sure you set `http://localhost:8888/callback` as one of your callbacks or else the code will not function as intended.
im too lazy to format this

139
v0.1/nowplaying.py Executable file
View file

@ -0,0 +1,139 @@
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()