diff --git a/NathanWoodburn.bsdesign b/NathanWoodburn.bsdesign index fcc1299..092fc08 100644 Binary files a/NathanWoodburn.bsdesign and b/NathanWoodburn.bsdesign differ diff --git a/blueprints/api.py b/blueprints/api.py index e3bf440..c5f94e4 100644 --- a/blueprints/api.py +++ b/blueprints/api.py @@ -7,7 +7,7 @@ from mail import sendEmail from tools import getClientIP, getGitCommit, json_response, parse_date, get_tools_data from blueprints import sol from dateutil import parser as date_parser -from blueprints.spotify import get_spotify_track +from blueprints.spotify import get_playing_spotify_track from cache_helper import get_nc_config, get_git_latest_activity # Constants @@ -176,7 +176,7 @@ def tools(): @app.route("/playing") def playing(): """Get the currently playing Spotify track.""" - track_info = get_spotify_track() + track_info = get_playing_spotify_track() if "error" in track_info: return json_response(request, track_info, HTTP_OK) return json_response(request, {"spotify": track_info}, HTTP_OK) diff --git a/blueprints/spotify.py b/blueprints/spotify.py index 0076c8f..8236891 100644 --- a/blueprints/spotify.py +++ b/blueprints/spotify.py @@ -15,7 +15,8 @@ SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize" SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token" SPOTIFY_CURRENTLY_PLAYING_URL = "https://api.spotify.com/v1/me/player/currently-playing" -SCOPE = "user-read-currently-playing user-read-playback-state" + +SCOPE = "user-read-currently-playing user-read-playback-state user-read-recently-played" ACCESS_TOKEN = None REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN") @@ -103,11 +104,11 @@ def callback(): @app.route("/playing") def currently_playing(): """Public endpoint showing your current track.""" - track = get_spotify_track() + track = get_playing_spotify_track() return json_response(request, {"spotify": track}, 200) -def get_spotify_track(): +def get_playing_spotify_track(): """Internal function to get current playing track without HTTP context.""" token = refresh_access_token() if not token: @@ -115,9 +116,9 @@ def get_spotify_track(): headers = {"Authorization": f"Bearer {token}"} response = requests.get(SPOTIFY_CURRENTLY_PLAYING_URL, headers=headers) - if response.status_code == 204: - return {"error": "Nothing is currently playing."} + # return {"error": "Nothing is currently playing."} + return get_last_spotify_track() elif response.status_code != 200: return {"error": "Spotify API error", "status": response.status_code} @@ -135,3 +136,30 @@ def get_spotify_track(): "duration_ms": data["item"].get("duration_ms", 1), } return track + + +def get_last_spotify_track(): + """Internal function to get last played track without HTTP context.""" + token = refresh_access_token() + if not token: + return {"error": "Failed to refresh access token"} + + headers = {"Authorization": f"Bearer {token}"} + response = requests.get( + "https://api.spotify.com/v1/me/player/recently-played", headers=headers + ) + if response.status_code != 200: + print("Spotify API error:", response.text) + return {"error": "Spotify API error", "status": response.status_code} + data = response.json() + if not data.get("items"): + return {"error": "No recently played tracks found."} + last_track_info = data["items"][0]["track"] + track = { + "song_name": last_track_info["name"], + "artist": ", ".join([artist["name"] for artist in last_track_info["artists"]]), + "album_name": last_track_info["album"]["name"], + "album_art": last_track_info["album"]["images"][0]["url"], + "played_at": data["items"][0]["played_at"], + } + return track diff --git a/curl.py b/curl.py index 046ccf2..932cb63 100644 --- a/curl.py +++ b/curl.py @@ -2,7 +2,7 @@ from flask import render_template from tools import getAddress, get_tools_data, getClientIP import os from functools import lru_cache -from blueprints.spotify import get_spotify_track +from blueprints.spotify import get_playing_spotify_track from cache_helper import get_git_latest_activity, get_projects as get_projects_cached @@ -61,7 +61,7 @@ def curl_response(request): "index.ascii", repo=get_current_project(), ip=getClientIP(request), - spotify=get_spotify_track(), + spotify=get_playing_spotify_track(), ), 200, {"Content-Type": "text/plain; charset=utf-8"},