Skip to content

Commit

Permalink
Allow a Node to be passed to Playable or fetch_tracks.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Jun 10, 2024
1 parent e03374d commit f6444d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
14 changes: 11 additions & 3 deletions wavelink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,14 +901,17 @@ 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
----------
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
-------
Expand All @@ -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.
Expand All @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion wavelink/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []

Expand Down
12 changes: 9 additions & 3 deletions wavelink/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
if TYPE_CHECKING:
from collections.abc import Iterator

from .node import Node
from .types.tracks import (
PlaylistInfoPayload,
PlaylistPayload,
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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


Expand Down

0 comments on commit f6444d5

Please sign in to comment.