From 33925635cb07ebf4d5c2a8674dbfd8f401910f7d Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Sun, 23 Jun 2024 00:27:30 -0400 Subject: [PATCH] Implement spotify api and setup polling dashboard --- .gitignore | 1 + src/__pycache__/clock.cpython-311.pyc | Bin 4432 -> 0 bytes src/__pycache__/weather.cpython-311.pyc | Bin 1999 -> 0 bytes src/main.py | 12 +++++- src/spotify.py | 53 ++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) delete mode 100644 src/__pycache__/clock.cpython-311.pyc delete mode 100644 src/__pycache__/weather.cpython-311.pyc create mode 100644 src/spotify.py 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 b6de62b56eac953d5e430713d7ada4d330a5dda8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4432 zcma)9TW=f36`sA3E0MZTq$rY-5-N5q)3)vxOY$XXZMb%gC~fMbt%`!6IV+J4Z`obh z6-xzv2#QT0AU3MPE)oNLa9p`|iafMHANtS&{Q;yvK*Rt70u+7l+Xl{4o_fx#NQtry zbU1tVo0&6bW@o-RXZWX%j&=g+;oV!ye`_P;KUioaDD(7u8(#MaB~-|etgs{qh{cS! zB*9zCh*@z-v>l{}dO>#3PLN%+8>ElR9_j}i zpuHf2v=3y6hCxPXKga<(2=X=hI>;y;0{I3#0`e$*6XY>EOpntTJwZ>>Q*`7D_m{$w zTO>asbo66F#~z7!%I7pDOft#5m1n{fQ_PgELCrLim-DMS6K0rXq<_PNStgtY)7nif z$AmeiXz7)d$%J_(=h9$8oEQfi&?ZhyFqf9$dg8<+bD6ia%uQ67Vyc-+>!!xFrkRQZ z?3HGidpW(lSD0mP7?Ky-_<$U$d_?T5;)uKsjpxiY7z7QkF=aXLyuj94%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6-A8KO zCu`kPmQwFJcE_xA9fN#-)p=LcV~&Sw`Yo6YGK#aR0HBps`y48feHJ}E6xP>)KP4fK zHh3xtao1 zd<#|ILd~}TyA&F*TpJyAwevIYr`}IGH##l;dvP^Yu)b>WZ) zZ+hc{9vg6TT>&@G9pL84qhb*zK&3}e^Q2je{-DHTOc{O13+d2oB-6LAQaTg^1iU&n z{=Mw00Xp~%ulmx~cmffDl=*+87DG@d$*r1jb@ViJHvSxw7q96*gfc(nF)zO}>T^)R zWV&xTL%y+T|Nez9E|-%J(_j1l z+V}S(|2Y273;+Ic<)`odYq4_ey{iBHn*V(WJ%dB#3x8bN8JOK3n5_=X)duFQw|Bk1 zJ0n$ZwC0UgywQ5(wVlYwc4XwM)YHu4+|yh&a;_FR2N#;qz)tAocIf2R)oN(27Mimz zZd|cDU>=;bi?KGm1m_ZPhmN!McpUuaY6h`49{+GPk#Sn|^DuuNkq@j?3hVHr9}{ki zpErF5Yf&75$H;-W-Tc|2sLa@;DO6SIw@Fh3JXmash*uf5NmC3+N*_4ChG=&y^EP== zI8(t1#M+$u0^eX6Dws%3=E}og&P1Ar0{uGF>K9S)X&Z>pJ@ZfbUOnrq#(DiF)SQ7*JHe zZp13$KD!NZJK~`2LF{#OIuLg{xC?Q&qwho9<6uAH0OFwCi#SM>HtS8h4{-?baXXAS z;^2P71Bg%9gNR>q@au@9h-3B;;x`<81o2Vg3D(;KJonVwA^RZP6WB%?;&!_|H!Hvv Pz9_!My6;}n!1v~VDdq-- diff --git a/src/__pycache__/weather.cpython-311.pyc b/src/__pycache__/weather.cpython-311.pyc deleted file mode 100644 index 8b317c9ec674b873ff05205b619d221e80ce023a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1999 zcma)7&1)M+6rb5o$)A$tL`~ErZYd6HW33ByX-(W>8q)>`B{Zg{qOdI5wYAPlyO~|Z zSV%;N9ONKK4+bAX!6!E%IrLA+pCAW8#DY&b^`_#Ua_XC1?b@o_(wWt7-n^Oj=FNL= zo_-x29YWCVCAK$mEJE+-By3!tIJpVLKGKkeO;o~pj492SOrG&aHqZJam**JtEz;P> zNaGF}gkHigXn9^^?jec)olYg}@s6qMcCOH0_6OXboJ<3;k93sB8iFJ6V+02adX3znwK_KvI?O2M=W_hp9^%c_?VE7k(4a7cj z0mHU+dKpZ$2Ca_&We;;^52LYlR^w{SGoj+7IjtAi6ikIV>zCvmmeC$Dtj@a zKd9)A>v$~Wh{yTcMMB?`CxT)HG<&h|SdaG)_1FqANs8_PdB?WOj_!ysoeCNm2AWVt zH5DevnGB?>P6?oajwdGfX4+S>oe8-q9;YtsEw-=DcT%4=W8K8%Cf^;iUKiVA7X8+L z>JuBKfM~MaRHl7JZcofp+05RJ1NQlPJGF3>TIi$}K*f0aY5hrk_fhLnJ9d?f!ifWU zMKTTuacg%5%MGFq!+jj3VfG;{F($q`z3AWzsFghg;w0-jle zu*BUWgjqAno1U=dliqN})>KzlOv|!8K~wb-M2vSp#+8^LMnWLvP#dSFe8+o9e}{5L zxOyoJ!?~qN5On4NAQ;KZo#aQ)Cp*%Y?d0N7aBy6j-LckK z)9?S`&mVk$#s@sAF@wItw4gho0)x+nLWzPbux3!o2}bD z9=7;*%}FAyd}XzyD~dOyC?!j)n3Nt-lm`{n3|7bps1EF!rWCc~1@binhS8!!D9{Ju z+y!Vvy~kjFriUUBME+V2MKHng=^lz;h~=|%`gaHpqesAh3r7n_;H-4T>8Xsp9pvPj2yXazj@av-ShTpp=-tc=DCEDTF6E0vd?L7QJ Ld4K-Hf^W!QZCAq# 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()