Skip to content

Commit 97b45ca

Browse files
committed
fix: add device caching
1 parent e81a4f5 commit 97b45ca

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

app/spotify/service.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ def play(self, track: Track) -> None:
4444
raise TrackNotFoundError(e.msg)
4545

4646
def refresh_devices(self) -> list[Device]:
47-
self.__devices = self.__get_devices_from_api()
48-
self.__current_device_index = 0
47+
devices_from_api = self.__get_devices_from_api()
48+
49+
if devices_from_api != self.__devices:
50+
self.__devices = devices_from_api
51+
self.__current_device_index = 0
52+
4953
return self.__devices
5054

5155
def next_device(self) -> Device | None:
@@ -68,10 +72,33 @@ def get_current_device(self) -> Device | None:
6872

6973
return self.__devices[self.__current_device_index]
7074

75+
def get_current_device_index(self) -> int:
76+
return self.__current_device_index
77+
7178
def get_devices(self) -> list[Device]:
72-
print(self.__devices)
7379
return self.__devices
7480

81+
def get_devices_count(self) -> int:
82+
return len(self.__devices)
83+
7584
def __get_devices_from_api(self) -> list[Device]:
7685
devices_json = self.__api.devices()["devices"]
7786
return [Device(**device) for device in devices_json]
87+
88+
89+
90+
if __name__ == "__main__":
91+
import os
92+
93+
SPOTIFY_CLIENT_ID = "f194ec87794e4505ace8cb76a65b37df"
94+
SPOTIFY_CLIENT_SECRET = "324b3a763f704dbeb0764f9e2bf8d53a"
95+
96+
service = SpotifyService(
97+
client_id=SPOTIFY_CLIENT_ID,
98+
client_secret=SPOTIFY_CLIENT_SECRET,
99+
redirect_uri="http://localhost:8080"
100+
)
101+
102+
print(service.get_devices())
103+
104+
service.play(Track(id="2bqS0QtnXGjOYs3z6VtSyW"))

0 commit comments

Comments
 (0)