25
25
import re
26
26
import time
27
27
import webbrowser
28
- from typing import TYPE_CHECKING , Any , Literal , Sequence , Union
28
+ from typing import TYPE_CHECKING , Any , Literal , Sequence , Tuple , Union
29
29
30
30
import confuse
31
31
import requests
@@ -293,7 +293,13 @@ def album_for_id(self, album_id: str) -> AlbumInfo | None:
293
293
if not (spotify_id := self ._extract_id (album_id )):
294
294
return None
295
295
296
- album_data = self ._handle_response ("get" , self .album_url + spotify_id )
296
+ try :
297
+ album_data = self ._handle_response (
298
+ "get" , self .album_url + spotify_id
299
+ )
300
+ except APIError :
301
+ return None
302
+
297
303
if album_data ["name" ] == "" :
298
304
self ._log .debug ("Album removed from Spotify: {}" , album_id )
299
305
return None
@@ -324,7 +330,10 @@ def album_for_id(self, album_id: str) -> AlbumInfo | None:
324
330
tracks_data = album_data ["tracks" ]
325
331
tracks_items = tracks_data ["items" ]
326
332
while tracks_data ["next" ]:
327
- tracks_data = self ._handle_response ("get" , tracks_data ["next" ])
333
+ try :
334
+ tracks_data = self ._handle_response ("get" , tracks_data ["next" ])
335
+ except APIError :
336
+ return None
328
337
tracks_items .extend (tracks_data ["items" ])
329
338
330
339
tracks = []
@@ -397,22 +406,29 @@ def track_for_id(self, track_id: str) -> None | TrackInfo:
397
406
self ._log .debug ("Invalid Spotify ID: {}" , track_id )
398
407
return None
399
408
400
- if not (
401
- track_data := self ._handle_response (
402
- "get" , f"{ self .track_url } { spotify_id } "
403
- )
404
- ):
405
- self ._log .debug ("Track not found: {}" , track_id )
409
+ try :
410
+ if not (
411
+ track_data := self ._handle_response (
412
+ "get" , f"{ self .track_url } { spotify_id } "
413
+ )
414
+ ):
415
+ self ._log .debug ("Track not found: {}" , track_id )
416
+ return None
417
+ except APIError :
406
418
return None
407
419
408
420
track = self ._get_track (track_data )
409
421
410
- # Get album's tracks to set `track.index` (position on the entire
411
- # release) and `track.medium_total` (total number of tracks on
412
- # the track's disc).
413
- album_data = self ._handle_response (
414
- "get" , self .album_url + track_data ["album" ]["id" ]
415
- )
422
+ try :
423
+ # Get album's tracks to set `track.index` (position on the entire
424
+ # release) and `track.medium_total` (total number of tracks on
425
+ # the track's disc).
426
+ album_data = self ._handle_response (
427
+ "get" , self .album_url + track_data ["album" ]["id" ]
428
+ )
429
+ except APIError :
430
+ return None
431
+
416
432
medium_total = 0
417
433
for i , track_data in enumerate (album_data ["tracks" ]["items" ], start = 1 ):
418
434
if track_data ["disc_number" ] == track .medium :
@@ -707,7 +723,10 @@ def _fetch_info(self, items, write, force):
707
723
self ._log .debug ("No track_id present for: {}" , item )
708
724
continue
709
725
710
- popularity , isrc , ean , upc = self .track_info (spotify_track_id )
726
+ maybe_track_info = self .track_info (spotify_track_id )
727
+ if maybe_track_info is None :
728
+ continue
729
+ popularity , isrc , ean , upc = maybe_track_info
711
730
item ["spotify_track_popularity" ] = popularity
712
731
item ["isrc" ] = isrc
713
732
item ["ean" ] = ean
@@ -726,9 +745,15 @@ def _fetch_info(self, items, write, force):
726
745
if write :
727
746
item .try_write ()
728
747
729
- def track_info (self , track_id : str ):
748
+ def track_info (
749
+ self , track_id : str
750
+ ) -> Tuple [Any | None , Any | None , Any | None , Any | None ] | None :
730
751
"""Fetch a track's popularity and external IDs using its Spotify ID."""
731
- track_data = self ._handle_response ("get" , self .track_url + track_id )
752
+ try :
753
+ track_data = self ._handle_response ("get" , self .track_url + track_id )
754
+ except APIError :
755
+ return None
756
+
732
757
external_ids = track_data .get ("external_ids" , {})
733
758
popularity = track_data .get ("popularity" )
734
759
self ._log .debug (
0 commit comments