diff --git a/wavelink/node.py b/wavelink/node.py index c573925e..5c7fb1ab 100644 --- a/wavelink/node.py +++ b/wavelink/node.py @@ -901,7 +901,7 @@ def get_node(cls, identifier: str | None = None, /) -> Node: return sorted(nodes, key=lambda n: n._total_player_count or len(n.players))[0] @classmethod - async def fetch_tracks(cls, query: str, /) -> list[Playable] | Playlist: + async def fetch_tracks(cls, query: str, /, *, node: Node | None = None) -> list[Playable] | Playlist: """Search for a list of :class:`~wavelink.Playable` or a :class:`~wavelink.Playlist`, with the given query. Parameters @@ -909,6 +909,9 @@ async def fetch_tracks(cls, query: str, /) -> list[Playable] | Playlist: query: str The query to search tracks for. If this is not a URL based search you should provide the appropriate search prefix, e.g. "ytsearch:Rick Roll" + node: :class:`~wavelink.Node` | None + An optional :class:`~wavelink.Node` to use when fetching tracks. Defaults to ``None``, which selects the + most appropriate :class:`~wavelink.Node` automatically. Returns ------- @@ -929,6 +932,11 @@ async def fetch_tracks(cls, query: str, /) -> list[Playable] | Playlist: or an empty list if no results were found. This method no longer accepts the ``cls`` parameter. + + + .. versionadded:: 3.4.0 + + Added the ``node`` Keyword-Only argument. """ # TODO: Documentation Extension for `.. positional-only::` marker. @@ -940,8 +948,8 @@ async def fetch_tracks(cls, query: str, /) -> list[Playable] | Playlist: if potential: return potential - node: Node = cls.get_node() - resp: LoadedResponse = await node._fetch_tracks(encoded_query) + node_: Node = node or cls.get_node() + resp: LoadedResponse = await node_._fetch_tracks(encoded_query) if resp["loadType"] == "track": track = Playable(data=resp["data"]) diff --git a/wavelink/player.py b/wavelink/player.py index 455993c8..d961ccb7 100644 --- a/wavelink/player.py +++ b/wavelink/player.py @@ -370,7 +370,7 @@ async def _search(query: str | None) -> T_a: return [] try: - search: wavelink.Search = await Pool.fetch_tracks(query) + search: wavelink.Search = await Pool.fetch_tracks(query, node=self._node) except (LavalinkLoadException, LavalinkException): return [] diff --git a/wavelink/tracks.py b/wavelink/tracks.py index c8571488..83b76d27 100644 --- a/wavelink/tracks.py +++ b/wavelink/tracks.py @@ -37,6 +37,7 @@ if TYPE_CHECKING: from collections.abc import Iterator + from .node import Node from .types.tracks import ( PlaylistInfoPayload, PlaylistPayload, @@ -323,7 +324,9 @@ def raw_data(self) -> TrackPayload: return self._raw_data @classmethod - async def search(cls, query: str, /, *, source: TrackSource | str | None = TrackSource.YouTubeMusic) -> Search: + async def search( + cls, query: str, /, *, source: TrackSource | str | None = TrackSource.YouTubeMusic, node: Node | None = None + ) -> Search: """Search for a list of :class:`~wavelink.Playable` or a :class:`~wavelink.Playlist`, with the given query. .. note:: @@ -355,6 +358,9 @@ async def search(cls, query: str, /, *, source: TrackSource | str | None = Track LavaSrc Spotify based search. Defaults to :attr:`wavelink.TrackSource.YouTubeMusic` which is equivalent to "ytmsearch:". + node: :class:`~wavelink.Node` | None + An optional :class:`~wavelink.Node` to use when searching for tracks. Defaults to ``None``, which uses + the :class:`~wavelink.Pool`'s automatic node selection. Returns @@ -410,7 +416,7 @@ async def search(cls, query: str, /, *, source: TrackSource | str | None = Track check = yarl.URL(query) if check.host: - tracks: Search = await wavelink.Pool.fetch_tracks(query) + tracks: Search = await wavelink.Pool.fetch_tracks(query, node=node) return tracks if not prefix: @@ -419,7 +425,7 @@ async def search(cls, query: str, /, *, source: TrackSource | str | None = Track assert not isinstance(prefix, TrackSource) term: str = f"{prefix.removesuffix(':')}:{query}" - tracks: Search = await wavelink.Pool.fetch_tracks(term) + tracks: Search = await wavelink.Pool.fetch_tracks(term, node=node) return tracks