pharmaia commited on
Commit
8ae7a56
·
verified ·
1 Parent(s): 56e3493

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -242,35 +242,54 @@ def load_config() -> AuthConfig:
242
  )
243
 
244
 
245
- spotify = SpotifyClient(load_config())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
 
247
 
248
  def spotify_auth_url(state: str = "") -> dict[str, Any]:
249
- return spotify.auth_url(state=state or None)
250
 
251
 
252
  def spotify_exchange_code(code: str, state: str = "") -> dict[str, Any]:
253
- return spotify.exchange_code(code=code, state=state or None)
254
 
255
 
256
  def spotify_me() -> dict[str, Any]:
257
- return spotify.me()
258
 
259
 
260
  def search_tracks(query: str, limit: int = 5) -> dict[str, Any]:
261
- return spotify.search_tracks(query=query, limit=limit)
262
 
263
 
264
  def create_playlist(name: str, description: str = "", public: bool = False) -> dict[str, Any]:
265
- return spotify.create_playlist(name=name, description=description, public=public)
266
 
267
 
268
  def add_tracks_to_playlist(playlist_id: str, track_ids_csv: str) -> dict[str, Any]:
269
  track_ids = [x.strip() for x in track_ids_csv.split(",") if x.strip()]
270
- return spotify.add_tracks(playlist_id=playlist_id, track_ids=track_ids)
271
 
272
 
273
  def create_playlist_from_search(name: str, query: str, limit: int = 10, public: bool = False) -> dict[str, Any]:
 
274
  tracks = spotify.search_tracks(query=query, limit=limit).get("results", [])
275
  if not tracks:
276
  raise RuntimeError("No tracks found for query.")
 
242
  )
243
 
244
 
245
+ _spotify: SpotifyClient | None = None
246
+ _config_error: str | None = None
247
+
248
+
249
+ def get_spotify() -> SpotifyClient:
250
+ global _spotify, _config_error
251
+ if _spotify is not None:
252
+ return _spotify
253
+ try:
254
+ _spotify = SpotifyClient(load_config())
255
+ _config_error = None
256
+ return _spotify
257
+ except Exception as exc:
258
+ _config_error = str(exc)
259
+ raise RuntimeError(
260
+ "Spotify config missing/invalid. Set HF Space Secrets: "
261
+ "SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REDIRECT_URI. "
262
+ f"Details: {_config_error}"
263
+ ) from exc
264
 
265
 
266
  def spotify_auth_url(state: str = "") -> dict[str, Any]:
267
+ return get_spotify().auth_url(state=state or None)
268
 
269
 
270
  def spotify_exchange_code(code: str, state: str = "") -> dict[str, Any]:
271
+ return get_spotify().exchange_code(code=code, state=state or None)
272
 
273
 
274
  def spotify_me() -> dict[str, Any]:
275
+ return get_spotify().me()
276
 
277
 
278
  def search_tracks(query: str, limit: int = 5) -> dict[str, Any]:
279
+ return get_spotify().search_tracks(query=query, limit=limit)
280
 
281
 
282
  def create_playlist(name: str, description: str = "", public: bool = False) -> dict[str, Any]:
283
+ return get_spotify().create_playlist(name=name, description=description, public=public)
284
 
285
 
286
  def add_tracks_to_playlist(playlist_id: str, track_ids_csv: str) -> dict[str, Any]:
287
  track_ids = [x.strip() for x in track_ids_csv.split(",") if x.strip()]
288
+ return get_spotify().add_tracks(playlist_id=playlist_id, track_ids=track_ids)
289
 
290
 
291
  def create_playlist_from_search(name: str, query: str, limit: int = 10, public: bool = False) -> dict[str, Any]:
292
+ spotify = get_spotify()
293
  tracks = spotify.search_tracks(query=query, limit=limit).get("results", [])
294
  if not tracks:
295
  raise RuntimeError("No tracks found for query.")