fix: Add last played spotify song when none currently playing
This commit is contained in:
Binary file not shown.
@@ -7,7 +7,7 @@ from mail import sendEmail
|
|||||||
from tools import getClientIP, getGitCommit, json_response, parse_date, get_tools_data
|
from tools import getClientIP, getGitCommit, json_response, parse_date, get_tools_data
|
||||||
from blueprints import sol
|
from blueprints import sol
|
||||||
from dateutil import parser as date_parser
|
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
|
from cache_helper import get_nc_config, get_git_latest_activity
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
@@ -176,7 +176,7 @@ def tools():
|
|||||||
@app.route("/playing")
|
@app.route("/playing")
|
||||||
def playing():
|
def playing():
|
||||||
"""Get the currently playing Spotify track."""
|
"""Get the currently playing Spotify track."""
|
||||||
track_info = get_spotify_track()
|
track_info = get_playing_spotify_track()
|
||||||
if "error" in track_info:
|
if "error" in track_info:
|
||||||
return json_response(request, track_info, HTTP_OK)
|
return json_response(request, track_info, HTTP_OK)
|
||||||
return json_response(request, {"spotify": track_info}, HTTP_OK)
|
return json_response(request, {"spotify": track_info}, HTTP_OK)
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
|
|||||||
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
|
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
|
||||||
SPOTIFY_CURRENTLY_PLAYING_URL = "https://api.spotify.com/v1/me/player/currently-playing"
|
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
|
ACCESS_TOKEN = None
|
||||||
REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN")
|
REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN")
|
||||||
@@ -103,11 +104,11 @@ def callback():
|
|||||||
@app.route("/playing")
|
@app.route("/playing")
|
||||||
def currently_playing():
|
def currently_playing():
|
||||||
"""Public endpoint showing your current track."""
|
"""Public endpoint showing your current track."""
|
||||||
track = get_spotify_track()
|
track = get_playing_spotify_track()
|
||||||
return json_response(request, {"spotify": track}, 200)
|
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."""
|
"""Internal function to get current playing track without HTTP context."""
|
||||||
token = refresh_access_token()
|
token = refresh_access_token()
|
||||||
if not token:
|
if not token:
|
||||||
@@ -115,9 +116,9 @@ def get_spotify_track():
|
|||||||
|
|
||||||
headers = {"Authorization": f"Bearer {token}"}
|
headers = {"Authorization": f"Bearer {token}"}
|
||||||
response = requests.get(SPOTIFY_CURRENTLY_PLAYING_URL, headers=headers)
|
response = requests.get(SPOTIFY_CURRENTLY_PLAYING_URL, headers=headers)
|
||||||
|
|
||||||
if response.status_code == 204:
|
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:
|
elif response.status_code != 200:
|
||||||
return {"error": "Spotify API error", "status": response.status_code}
|
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),
|
"duration_ms": data["item"].get("duration_ms", 1),
|
||||||
}
|
}
|
||||||
return track
|
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
|
||||||
|
|||||||
4
curl.py
4
curl.py
@@ -2,7 +2,7 @@ from flask import render_template
|
|||||||
from tools import getAddress, get_tools_data, getClientIP
|
from tools import getAddress, get_tools_data, getClientIP
|
||||||
import os
|
import os
|
||||||
from functools import lru_cache
|
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
|
from cache_helper import get_git_latest_activity, get_projects as get_projects_cached
|
||||||
|
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ def curl_response(request):
|
|||||||
"index.ascii",
|
"index.ascii",
|
||||||
repo=get_current_project(),
|
repo=get_current_project(),
|
||||||
ip=getClientIP(request),
|
ip=getClientIP(request),
|
||||||
spotify=get_spotify_track(),
|
spotify=get_playing_spotify_track(),
|
||||||
),
|
),
|
||||||
200,
|
200,
|
||||||
{"Content-Type": "text/plain; charset=utf-8"},
|
{"Content-Type": "text/plain; charset=utf-8"},
|
||||||
|
|||||||
Reference in New Issue
Block a user