diff --git a/.gitignore b/.gitignore index 62c09d0..4c4d5b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env.sh venv/ +.cache diff --git a/src/__pycache__/clock.cpython-311.pyc b/src/__pycache__/clock.cpython-311.pyc deleted file mode 100644 index b6de62b..0000000 Binary files a/src/__pycache__/clock.cpython-311.pyc and /dev/null differ diff --git a/src/__pycache__/weather.cpython-311.pyc b/src/__pycache__/weather.cpython-311.pyc deleted file mode 100644 index 8b317c9..0000000 Binary files a/src/__pycache__/weather.cpython-311.pyc and /dev/null differ diff --git a/src/main.py b/src/main.py index 6c5f60e..0844ade 100644 --- a/src/main.py +++ b/src/main.py @@ -1,17 +1,25 @@ from weather import Weather +import json from clock import Clock +from spotify import Spotify from time import sleep def main(): weather_manager = Weather() - weather_manager.start_update_loop() clock_manager = Clock() + spotify_manager = Spotify() + + weather_manager.start_update_loop() clock_manager.start_update_loop() + spotify_manager.start_update_loop() + while True: print(clock_manager.time) - print(weather_manager.weather["data"]["values"]["temperature"]) + print(f'{weather_manager.weather["data"]["values"]["temperature"]} degrees') + print(f"{spotify_manager.current_context_name}\n{spotify_manager.song_name} - {', '.join(spotify_manager.song_artists)}\n{spotify_manager.song_played / 1000} / {spotify_manager.song_length / 1000}") + print() sleep(10) diff --git a/src/spotify.py b/src/spotify.py new file mode 100644 index 0000000..2e2cc70 --- /dev/null +++ b/src/spotify.py @@ -0,0 +1,53 @@ +import spotipy +from spotipy.oauth2 import SpotifyOAuth +from time import sleep +import threading + +class Spotify: + def __init__(self): + self.sp = self.authenticate() + self.set_values() + pass + + def authenticate(self): + scope = ["user-read-playback-state", "user-modify-playback-state", "user-read-currently-playing", "user-read-playback-position", "user-library-modify"] + sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope)) + return sp + + def set_values(self): + song = self.sp.current_playback() + self.currently_playing = song + if song: + self.current_context = song["context"]["type"] + self.current_context_uri = song["context"]["uri"] + self.set_context() + self.song_artists = [] + for artist in song["item"]["artists"]: + self.song_artists.append(artist["name"]) + self.song_name = song["item"]["name"] + self.song_length = song["item"]["duration_ms"] + self.song_played = song["progress_ms"] + + def set_context(self): + if self.current_context == "artist": + artist = self.sp.artist(self.current_context_uri) + if artist: + self.current_context_name = artist["name"] + if self.current_context == "playlist": + playlist = self.sp.playlist(self.current_context_uri) + if playlist: + self.current_context_name = playlist["name"] + if self.current_context == "album": + album = self.sp.album(self.current_context_uri) + if album: + self.current_context_name = album["name"] + + def start_update_loop(self): + thread = threading.Thread(target=self.update_loop) + thread.daemon = True + thread.start() + + def update_loop(self): + while True: + sleep(5) + self.set_values()