Skip to content

Commit 6f623ee

Browse files
committed
Opt in deezer plugin.
1 parent a770cfb commit 6f623ee

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

beetsplug/deezer.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@
2626
from beets import ui
2727
from beets.autotag import AlbumInfo, TrackInfo
2828
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+
)
3034

3135
if TYPE_CHECKING:
3236
from beets.library import Item, Library
33-
from beetsplug._typing import JSONDict
3437

38+
from ._typing import JSONDict
3539

36-
class DeezerPlugin(MetadataSourcePlugin[Response], BeetsPlugin):
37-
data_source = "Deezer"
3840

41+
class DeezerPlugin(SearchApiMetadataSourcePlugin[IDResponse]):
3942
item_types = {
4043
"deezer_track_rank": types.INTEGER,
4144
"deezer_track_id": types.INTEGER,
@@ -63,7 +66,7 @@ def func(lib: Library, opts, args):
6366

6467
def album_for_id(self, album_id: str) -> AlbumInfo | None:
6568
"""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)):
6770
return None
6871

6972
album_url = f"{self.album_url}{deezer_id}"
@@ -145,11 +148,14 @@ def album_for_id(self, album_id: str) -> AlbumInfo | None:
145148
)
146149

147150
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.
149156
150-
Returns a TrackInfo object or None if the track is not found.
151157
"""
152-
if not (deezer_id := self._get_id(track_id)):
158+
if not (deezer_id := self.extract_release_id(track_id)):
153159
self._log.debug("Invalid Deezer track_id: {}", track_id)
154160
return None
155161

@@ -162,11 +168,13 @@ def track_for_id(self, track_id: str) -> None | TrackInfo:
162168
# Get album's tracks to set `track.index` (position on the entire
163169
# release) and `track.medium_total` (total number of tracks on
164170
# 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+
):
169176
return None
177+
170178
try:
171179
album_tracks_data = album_tracks_obj["data"]
172180
except KeyError:
@@ -187,7 +195,6 @@ def _get_track(self, track_data: JSONDict) -> TrackInfo:
187195
"""Convert a Deezer track object dict to a TrackInfo object.
188196
189197
:param track_data: Deezer Track object dict
190-
:return: TrackInfo object for track
191198
"""
192199
artist, artist_id = self.get_artist(
193200
track_data.get("contributors", [track_data["artist"]])
@@ -211,7 +218,7 @@ def _get_track(self, track_data: JSONDict) -> TrackInfo:
211218

212219
@staticmethod
213220
def _construct_search_query(
214-
filters: dict[str, str], keywords: str = ""
221+
filters: SearchFilter, keywords: str = ""
215222
) -> str:
216223
"""Construct a query string with the specified filters and keywords to
217224
be provided to the Deezer Search API
@@ -242,14 +249,14 @@ def _search_api(
242249
"radio",
243250
"user",
244251
],
245-
filters: dict[str, str],
252+
filters: SearchFilter,
246253
keywords="",
247-
) -> Sequence[Response]:
254+
) -> Sequence[IDResponse]:
248255
"""Query the Deezer Search API for the specified ``keywords``, applying
249256
the provided ``filters``.
250257
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.
253260
:return: JSON data for the class:`Response <Response>` object or None
254261
if no search results are returned.
255262
"""
@@ -269,7 +276,7 @@ def _search_api(
269276
e,
270277
)
271278
return ()
272-
response_data = response.json().get("data", [])
279+
response_data: Sequence[IDResponse] = response.json().get("data", [])
273280
self._log.debug(
274281
"Found {} result(s) from {} for '{}'",
275282
len(response_data),

0 commit comments

Comments
 (0)