Skip to content

Commit

Permalink
Added `build_track to node/client.
Browse files Browse the repository at this point in the history
`build_track` takes in a tracks unique base64 encoded identifier and builds and returns a `Track` object.
  • Loading branch information
EvieePy committed Mar 30, 2020
1 parent 5b50f98 commit f593c59
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion wavelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__author__ = 'EvieePy'
__license__ = 'MIT'
__copyright__ = 'Copyright 2019-2020 (c) PythonistaGuild'
__version__ = '0.5.03'
__version__ = '0.6.0'

from .client import Client
from .errors import *
Expand Down
29 changes: 29 additions & 0 deletions wavelink/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,35 @@ async def get_tracks(self, query: str) -> Optional[list]:

return await node.get_tracks(query)

async def build_track(self, identifier: str):
"""|coro|
Build a track object with a valid track identifier.
Parameters
------------
identifier: str
The tracks unique Base64 encoded identifier. This is usually retrieved from various lavalink events.
Returns
---------
:class:`wavelink.player.Track`
The track built from a Base64 identifier.
Raises
--------
ZeroConnectedNodes
There are no :class:`wavelink.node.Node`s currently connected.
BuildTrackError
Decoding and building the track failed.
"""
node = self.get_best_node()

if node is None:
raise ZeroConnectedNodes

return await node.build_track(identifier)

def _get_players(self) -> dict:
players = []

Expand Down
4 changes: 4 additions & 0 deletions wavelink/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ class ZeroConnectedNodes(WavelinkException):

class AuthorizationFailure(WavelinkException):
"""Exception raised when an invalid password is provided toa node."""


class BuildTrackError(WavelinkException):
"""Exception raised when a track is failed to be decoded and re-built."""
31 changes: 31 additions & 0 deletions wavelink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ async def get_tracks(self, query: str) -> Union[list, TrackPlaylist, None]:

return tracks

async def build_track(self, identifier: str) -> Track:
"""|coro|
Build a track object with a valid track identifier.
Parameters
------------
identifier: str
The tracks unique Base64 encoded identifier. This is usually retrieved from various lavalink events.
Returns
---------
:class:`wavelink.player.Track`
The track built from a Base64 identifier.
Raises
--------
BuildTrackError
Decoding and building the track failed.
"""
async with self.session.get(f'{self.rest_uri}/decodetrack?track={identifier}',
headers={'Authorization': self.password}) as resp:
data = await resp.json()

if not resp.status == 200:
raise BuildTrackError(f'Failed to build track. Status: {data["status"]}, Error: {data["error"]}.'
f'Check the identfier is correct and try again.')

track = Track(id_=identifier, info=data)
return track

def get_player(self, guild_id: int) -> Optional[Player]:
"""Retrieve a player object associated with the Node.
Expand Down

0 comments on commit f593c59

Please sign in to comment.