From 10d13e04f96f7d10da77641ce78205ddc7203639 Mon Sep 17 00:00:00 2001 From: matthewexe Date: Sat, 2 Aug 2025 05:31:20 +0200 Subject: [PATCH] fixed issues --- backend/auth/session.py | 4 ++-- backend/endpoints/spotify_api.py | 38 +++++++++++++++++++------------- backend/main.py | 4 +++- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/backend/auth/session.py b/backend/auth/session.py index d35b021..2f72fd9 100644 --- a/backend/auth/session.py +++ b/backend/auth/session.py @@ -19,8 +19,8 @@ class SessionData(): self.__creation_date = __creation_date or datetime.datetime.now() def nearly_expired(self, before=30): - delta_time = datetime.datetime.now() - self.__creation_date - before - return delta_time.seconds > self.__expires_in + delta_time = datetime.datetime.now() - self.__creation_date + return delta_time.seconds > self.__expires_in - before def is_expired(self): return self.nearly_expired(0) diff --git a/backend/endpoints/spotify_api.py b/backend/endpoints/spotify_api.py index 8c8ce99..9728d75 100644 --- a/backend/endpoints/spotify_api.py +++ b/backend/endpoints/spotify_api.py @@ -54,26 +54,34 @@ def search(query: str): "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, "Content-Type": "application/json" } - return requests.get(url, headers=header).json() + response = requests.get(url, headers=header) + + return response.json() except Exception as e: print(e) @music_router.get("/play") def play(song_id: str): - url = SPOTIFY_PLAY_URL + "me/player/play" - body = { "uris": [f"spotify:track:{song_id}"] } - header = { - "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, - "Content-Type": "application/json" - } - requests.put(url, data=body, headers=header).json() + try: + url = SPOTIFY_PLAY_URL + "me/player/play" + body = { "uris": [f"spotify:track:{song_id}"] } + header = { + "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, + "Content-Type": "application/json" + } + response = requests.put(url, data=body, headers=header) + return response.json() + except Exception as e: + print(e) @music_router.get("/current-song") def current_song(): - url = SPOTIFY_PLAY_URL + f"me/player/currently-playing?marketing=IT" - header = { - "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, - "Content-Type": "application/json" - } - return requests.get(url, headers=header).json() - + try: + url = SPOTIFY_PLAY_URL + f"me/player/currently-playing?marketing=IT" + header = { + "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, + "Content-Type": "application/json" + } + return requests.get(url, headers=header).json() + except Exception as e: + print(e) diff --git a/backend/main.py b/backend/main.py index 9eef6d5..1340f56 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,4 +1,7 @@ from dotenv import load_dotenv + +load_dotenv() + from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware @@ -7,7 +10,6 @@ from endpoints.queue import queue_router from routes import router from endpoints.spotify_api import music_router -load_dotenv() app = FastAPI(title="Simple Fullstack API")