Implement spotify api and setup polling dashboard
This commit is contained in:
parent
659fb7ad43
commit
33925635cb
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
.env.sh
|
.env.sh
|
||||||
venv/
|
venv/
|
||||||
|
.cache
|
||||||
|
Binary file not shown.
Binary file not shown.
12
src/main.py
12
src/main.py
@ -1,17 +1,25 @@
|
|||||||
from weather import Weather
|
from weather import Weather
|
||||||
|
import json
|
||||||
from clock import Clock
|
from clock import Clock
|
||||||
|
from spotify import Spotify
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
weather_manager = Weather()
|
weather_manager = Weather()
|
||||||
weather_manager.start_update_loop()
|
|
||||||
clock_manager = Clock()
|
clock_manager = Clock()
|
||||||
|
spotify_manager = Spotify()
|
||||||
|
|
||||||
|
weather_manager.start_update_loop()
|
||||||
clock_manager.start_update_loop()
|
clock_manager.start_update_loop()
|
||||||
|
spotify_manager.start_update_loop()
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print(clock_manager.time)
|
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)
|
sleep(10)
|
||||||
|
|
||||||
|
|
||||||
|
53
src/spotify.py
Normal file
53
src/spotify.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user