14
14
15
15
"""Adds Deezer release and track search support to the autotagger"""
16
16
17
+ from __future__ import annotations
18
+
17
19
import collections
18
20
import time
19
21
25
27
from beets .dbcore import types
26
28
from beets .library import DateType
27
29
from beets .plugins import BeetsPlugin , MetadataSourcePlugin
28
- from beets .util .id_extractors import deezer_id_regex
29
30
30
31
31
32
class DeezerPlugin (MetadataSourcePlugin , BeetsPlugin ):
@@ -43,8 +44,6 @@ class DeezerPlugin(MetadataSourcePlugin, BeetsPlugin):
43
44
album_url = "https://api.deezer.com/album/"
44
45
track_url = "https://api.deezer.com/track/"
45
46
46
- id_regex = deezer_id_regex
47
-
48
47
def __init__ (self ):
49
48
super ().__init__ ()
50
49
@@ -75,21 +74,15 @@ def fetch_data(self, url):
75
74
return None
76
75
return data
77
76
78
- def album_for_id (self , album_id ):
79
- """Fetch an album by its Deezer ID or URL and return an
80
- AlbumInfo object or None if the album is not found.
81
-
82
- :param album_id: Deezer ID or URL for the album.
83
- :type album_id: str
84
- :return: AlbumInfo object for album.
85
- :rtype: beets.autotag.hooks.AlbumInfo or None
86
- """
87
- deezer_id = self ._get_id ("album" , album_id , self .id_regex )
88
- if deezer_id is None :
77
+ def album_for_id (self , album_id : str ) -> AlbumInfo | None :
78
+ """Fetch an album by its Deezer ID or URL."""
79
+ if not (deezer_id := self ._get_id (album_id )):
89
80
return None
90
- album_data = self .fetch_data (self .album_url + deezer_id )
91
- if album_data is None :
81
+
82
+ album_url = f"{ self .album_url } { deezer_id } "
83
+ if not (album_data := self .fetch_data (album_url )):
92
84
return None
85
+
93
86
contributors = album_data .get ("contributors" )
94
87
if contributors is not None :
95
88
artist , artist_id = self .get_artist (contributors )
@@ -132,7 +125,7 @@ def album_for_id(self, album_id):
132
125
tracks_data .extend (tracks_obj ["data" ])
133
126
134
127
tracks = []
135
- medium_totals = collections .defaultdict (int )
128
+ medium_totals : dict [ int | None , int ] = collections .defaultdict (int )
136
129
for i , track_data in enumerate (tracks_data , start = 1 ):
137
130
track = self ._get_track (track_data )
138
131
track .index = i
@@ -150,13 +143,15 @@ def album_for_id(self, album_id):
150
143
artist_id = artist_id ,
151
144
tracks = tracks ,
152
145
albumtype = album_data ["record_type" ],
153
- va = len (album_data ["contributors" ]) == 1
154
- and artist .lower () == "various artists" ,
146
+ va = (
147
+ len (album_data ["contributors" ]) == 1
148
+ and (artist or "" ).lower () == "various artists"
149
+ ),
155
150
year = year ,
156
151
month = month ,
157
152
day = day ,
158
153
label = album_data ["label" ],
159
- mediums = max (medium_totals .keys ()),
154
+ mediums = max (filter ( None , medium_totals .keys () )),
160
155
data_source = self .data_source ,
161
156
data_url = album_data ["link" ],
162
157
cover_art_url = album_data .get ("cover_xl" ),
@@ -204,12 +199,11 @@ def track_for_id(self, track_id=None, track_data=None):
204
199
:rtype: beets.autotag.hooks.TrackInfo or None
205
200
"""
206
201
if track_data is None :
207
- deezer_id = self ._get_id ("track" , track_id , self .id_regex )
208
- if deezer_id is None :
209
- return None
210
- track_data = self .fetch_data (self .track_url + deezer_id )
211
- if track_data is None :
202
+ if not (deezer_id := self ._get_id (track_id )) or not (
203
+ track_data := self .fetch_data (f"{ self .track_url } { deezer_id } " )
204
+ ):
212
205
return None
206
+
213
207
track = self ._get_track (track_data )
214
208
215
209
# Get album's tracks to set `track.index` (position on the entire
0 commit comments