Spaces:
Running
Running
feat: update song details fetching logic to include playback status
Browse files
api.py
CHANGED
|
@@ -49,7 +49,7 @@ async def spotify_banner(request: Request, user: str = "arpy8", color: str = "f7
|
|
| 49 |
color = color or "f70000"
|
| 50 |
|
| 51 |
context = {
|
| 52 |
-
"title_text": "Now playing" if
|
| 53 |
"color": color,
|
| 54 |
"song_name": result["song"],
|
| 55 |
"artist_name": result["artist"],
|
|
|
|
| 49 |
color = color or "f70000"
|
| 50 |
|
| 51 |
context = {
|
| 52 |
+
"title_text": "Now playing" if result["is_playing"] else "Last played",
|
| 53 |
"color": color,
|
| 54 |
"song_name": result["song"],
|
| 55 |
"artist_name": result["artist"],
|
utils.py
CHANGED
|
@@ -31,6 +31,7 @@ class SongDetailFetcher:
|
|
| 31 |
self.lastfm_api_key = os.getenv("LASTFM_API_KEY")
|
| 32 |
self.no_scrobble_url = f"https://ws.audioscrobbler.com/2.0/?api_key={self.lastfm_api_key}&method=User.getrecenttracks&user={self.user}&format=json&limit=1"
|
| 33 |
self.scrobble_url = f"https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user={self.user}&limit=1&extended=1&api_key={self.lastfm_api_key}&format=json"
|
|
|
|
| 34 |
|
| 35 |
def get_base64_image(self, url):
|
| 36 |
try:
|
|
@@ -50,22 +51,25 @@ class SongDetailFetcher:
|
|
| 50 |
|
| 51 |
def get_details(self):
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
thumbnail = self.get_base64_image(
|
| 62 |
|
| 63 |
return {
|
| 64 |
"song": name,
|
| 65 |
"artist": artist,
|
| 66 |
"thumbnail": thumbnail,
|
| 67 |
"url": url,
|
| 68 |
-
"
|
| 69 |
}
|
| 70 |
|
| 71 |
except Exception as e:
|
|
|
|
| 31 |
self.lastfm_api_key = os.getenv("LASTFM_API_KEY")
|
| 32 |
self.no_scrobble_url = f"https://ws.audioscrobbler.com/2.0/?api_key={self.lastfm_api_key}&method=User.getrecenttracks&user={self.user}&format=json&limit=1"
|
| 33 |
self.scrobble_url = f"https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user={self.user}&limit=1&extended=1&api_key={self.lastfm_api_key}&format=json"
|
| 34 |
+
# self.no_scrobble_url = f"https://ws.audioscrobbler.com/2.0/?api_key={self.lastfm_api_key}&method=User.getrecenttracks&user={self.user}&format=json&limit=1"
|
| 35 |
|
| 36 |
def get_base64_image(self, url):
|
| 37 |
try:
|
|
|
|
| 51 |
|
| 52 |
def get_details(self):
|
| 53 |
try:
|
| 54 |
+
scrobble_rslt = requests.get(self.scrobble_url)
|
| 55 |
+
no_scrobble_rslt = requests.get(self.no_scrobble_url)
|
| 56 |
+
|
| 57 |
+
scrobble_rslt = json.loads(scrobble_rslt.text)['recenttracks']["track"][0]
|
| 58 |
+
no_scrobble_rslt = json.loads(no_scrobble_rslt.text)['recenttracks']["track"][0]
|
| 59 |
|
| 60 |
+
artist = no_scrobble_rslt["artist"]["#text"]
|
| 61 |
+
name = no_scrobble_rslt["name"]
|
| 62 |
+
is_playing = "@attr" in no_scrobble_rslt
|
| 63 |
+
url = no_scrobble_rslt["url"]
|
| 64 |
+
|
| 65 |
+
thumbnail = self.get_base64_image(scrobble_rslt["image"][-1]["#text"])
|
| 66 |
|
| 67 |
return {
|
| 68 |
"song": name,
|
| 69 |
"artist": artist,
|
| 70 |
"thumbnail": thumbnail,
|
| 71 |
"url": url,
|
| 72 |
+
"is_playing": is_playing,
|
| 73 |
}
|
| 74 |
|
| 75 |
except Exception as e:
|