From 9809fe0695086fa70837d31b62014cea5d8d77cf Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Sun, 26 Oct 2025 21:14:55 +1100 Subject: [PATCH] feat: Add spotify to curl route --- blueprints/spotify.py | 32 +++++++++++++++++++++++++++++--- curl.py | 7 +++++-- templates/index.ascii | 1 + templates/projects.ascii | 2 ++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/blueprints/spotify.py b/blueprints/spotify.py index 083d756..7e07e03 100644 --- a/blueprints/spotify.py +++ b/blueprints/spotify.py @@ -48,8 +48,6 @@ def refresh_access_token(): TOKEN_EXPIRES = time.time() + token_info.get("expires_in", 3600) return ACCESS_TOKEN - - @spotify_bp.route("/login") def login(): auth_query = ( @@ -119,4 +117,32 @@ def currently_playing(): "album_art": data["item"]["album"]["images"][0]["url"], "is_playing": data["is_playing"] } - return json_response(request, {"spotify":track}, 200) \ No newline at end of file + return json_response(request, {"spotify":track}, 200) + +def get_spotify_track(): + """Internal function to get current playing track without HTTP context.""" + token = refresh_access_token() + if not token: + return json_response(request, {"error": "Failed to refresh access token"}, 500) + + 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."} + elif response.status_code != 200: + return {"error": "Spotify API error", "status": response.status_code} + + data = response.json() + if not data.get("item"): + return {"error": "Nothing is currently playing."} + + + track = { + "song_name": data["item"]["name"], + "artist": ", ".join([artist["name"] for artist in data["item"]["artists"]]), + "album_name": data["item"]["album"]["name"], + "album_art": data["item"]["album"]["images"][0]["url"], + "is_playing": data["is_playing"] + } + return track \ No newline at end of file diff --git a/curl.py b/curl.py index e7c4f37..526f1f4 100644 --- a/curl.py +++ b/curl.py @@ -3,6 +3,7 @@ from tools import error_response, getAddress, get_tools_data, getClientIP import os from functools import lru_cache import requests +from blueprints.spotify import get_spotify_track def clean_path(path:str): @@ -32,7 +33,9 @@ def get_current_project(): repo_name = git["repo"]["name"] repo_name = repo_name.lower() repo_description = git["repo"]["description"] - return f"{repo_name} - {repo_description}" + if not repo_description: + return f"{repo_name}" + return f"{repo_name} - {repo_description}" @lru_cache(maxsize=1) @@ -81,7 +84,7 @@ def curl_response(request): # Handle special cases if path == "index": # Get current project - return render_template("index.ascii",repo=get_current_project(), ip=getClientIP(request)), 200, {'Content-Type': 'text/plain; charset=utf-8'} + return render_template("index.ascii",repo=get_current_project(), ip=getClientIP(request), spotify=get_spotify_track()), 200, {'Content-Type': 'text/plain; charset=utf-8'} if path == "projects": # Get projects return render_template("projects.ascii",header=get_header(),projects=get_projects()), 200, {'Content-Type': 'text/plain; charset=utf-8'} diff --git a/templates/index.ascii b/templates/index.ascii index 0ea8493..ea46740 100644 --- a/templates/index.ascii +++ b/templates/index.ascii @@ -24,6 +24,7 @@ I'm also one of the founders of Handshake AU [https://hns.au], working to grow Handshake adoption across Australia. I'm currently working on: {{ repo | safe }} +{% if not spotify.message %}Currently listening to: {{ spotify.song_name }} by {{ spotify.artist }}{% endif %} ───────────────────────────────────────────────  SKILLS  diff --git a/templates/projects.ascii b/templates/projects.ascii index 61f4067..4212f3d 100644 --- a/templates/projects.ascii +++ b/templates/projects.ascii @@ -5,3 +5,5 @@ {{projects}} +Look at more projects on my Git: https://git.woodburn.au +