26
26
from beets import ui
27
27
from beets .autotag import AlbumInfo , TrackInfo
28
28
from beets .dbcore import types
29
- from beets .plugins import BeetsPlugin , MetadataSourcePlugin , Response
29
+ from beets .metadata_plugins import (
30
+ IDResponse ,
31
+ SearchApiMetadataSourcePlugin ,
32
+ SearchFilter ,
33
+ )
30
34
31
35
if TYPE_CHECKING :
32
36
from beets .library import Item , Library
33
- from beetsplug ._typing import JSONDict
34
37
38
+ from ._typing import JSONDict
35
39
36
- class DeezerPlugin (MetadataSourcePlugin [Response ], BeetsPlugin ):
37
- data_source = "Deezer"
38
40
41
+ class DeezerPlugin (SearchApiMetadataSourcePlugin [IDResponse ]):
39
42
item_types = {
40
43
"deezer_track_rank" : types .INTEGER ,
41
44
"deezer_track_id" : types .INTEGER ,
@@ -63,7 +66,7 @@ def func(lib: Library, opts, args):
63
66
64
67
def album_for_id (self , album_id : str ) -> AlbumInfo | None :
65
68
"""Fetch an album by its Deezer ID or URL."""
66
- if not (deezer_id := self ._get_id (album_id )):
69
+ if not (deezer_id := self .extract_release_id (album_id )):
67
70
return None
68
71
69
72
album_url = f"{ self .album_url } { deezer_id } "
@@ -145,11 +148,14 @@ def album_for_id(self, album_id: str) -> AlbumInfo | None:
145
148
)
146
149
147
150
def track_for_id (self , track_id : str ) -> None | TrackInfo :
148
- """Fetch a track by its Deezer ID or URL.
151
+ """Fetch a track by its Deezer ID or URL and return a
152
+ TrackInfo object or None if the track is not found.
153
+
154
+ :param track_id: (Optional) Deezer ID or URL for the track. Either
155
+ ``track_id`` or ``track_data`` must be provided.
149
156
150
- Returns a TrackInfo object or None if the track is not found.
151
157
"""
152
- if not (deezer_id := self ._get_id (track_id )):
158
+ if not (deezer_id := self .extract_release_id (track_id )):
153
159
self ._log .debug ("Invalid Deezer track_id: {}" , track_id )
154
160
return None
155
161
@@ -162,11 +168,13 @@ def track_for_id(self, track_id: str) -> None | TrackInfo:
162
168
# Get album's tracks to set `track.index` (position on the entire
163
169
# release) and `track.medium_total` (total number of tracks on
164
170
# the track's disc).
165
- album_tracks_obj = self .fetch_data (
166
- self .album_url + str (track_data ["album" ]["id" ]) + "/tracks"
167
- )
168
- if album_tracks_obj is None :
171
+ if not (
172
+ album_tracks_obj := self .fetch_data (
173
+ self .album_url + str (track_data ["album" ]["id" ]) + "/tracks"
174
+ )
175
+ ):
169
176
return None
177
+
170
178
try :
171
179
album_tracks_data = album_tracks_obj ["data" ]
172
180
except KeyError :
@@ -187,7 +195,6 @@ def _get_track(self, track_data: JSONDict) -> TrackInfo:
187
195
"""Convert a Deezer track object dict to a TrackInfo object.
188
196
189
197
:param track_data: Deezer Track object dict
190
- :return: TrackInfo object for track
191
198
"""
192
199
artist , artist_id = self .get_artist (
193
200
track_data .get ("contributors" , [track_data ["artist" ]])
@@ -211,7 +218,7 @@ def _get_track(self, track_data: JSONDict) -> TrackInfo:
211
218
212
219
@staticmethod
213
220
def _construct_search_query (
214
- filters : dict [ str , str ] , keywords : str = ""
221
+ filters : SearchFilter , keywords : str = ""
215
222
) -> str :
216
223
"""Construct a query string with the specified filters and keywords to
217
224
be provided to the Deezer Search API
@@ -242,14 +249,14 @@ def _search_api(
242
249
"radio" ,
243
250
"user" ,
244
251
],
245
- filters : dict [ str , str ] ,
252
+ filters : SearchFilter ,
246
253
keywords = "" ,
247
- ) -> Sequence [Response ]:
254
+ ) -> Sequence [IDResponse ]:
248
255
"""Query the Deezer Search API for the specified ``keywords``, applying
249
256
the provided ``filters``.
250
257
251
- :param query_type: The Deezer Search API method to use .
252
- :param keywords: (Optional) Query keywords to use.
258
+ :param filters: Field filters to apply .
259
+ :param keywords: Query keywords to use.
253
260
:return: JSON data for the class:`Response <Response>` object or None
254
261
if no search results are returned.
255
262
"""
@@ -269,7 +276,7 @@ def _search_api(
269
276
e ,
270
277
)
271
278
return ()
272
- response_data = response .json ().get ("data" , [])
279
+ response_data : Sequence [ IDResponse ] = response .json ().get ("data" , [])
273
280
self ._log .debug (
274
281
"Found {} result(s) from {} for '{}'" ,
275
282
len (response_data ),
0 commit comments