-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathcustom_source.py
45 lines (33 loc) · 2.14 KB
/
custom_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from lavalink.models import (DeferredAudioTrack, LoadResult, LoadType,
PlaylistInfo, Source)
class LoadError(Exception): # We'll raise this if we have trouble loading our track.
pass
class CustomAudioTrack(DeferredAudioTrack):
# A DeferredAudioTrack allows us to load metadata now, and a playback URL later.
# This makes the DeferredAudioTrack highly efficient, particularly in cases
# where large playlists are loaded.
async def load(self, client): # Load our 'actual' playback track using the metadata from this one.
result: LoadResult = await client.get_tracks('ytsearch:{0.title} {0.author}'.format(self)) # Search for our track on YouTube.
if result.load_type != LoadType.SEARCH or not result.tracks: # We're expecting a 'SEARCH' due to our 'ytsearch' prefix above.
raise LoadError
first_track = result.tracks[0] # Grab the first track from the results.
base64 = first_track.track # Extract the base64 string from the track.
self.track = base64 # We'll store this for later, as it allows us to save making network requests
# if this track is re-used (e.g. repeat).
return base64
class CustomSource(Source):
def __init__(self):
super().__init__(name='custom') # Initialising our custom source with the name 'custom'.
async def load_item(self, client, query: str):
if 'keyword' in query:
# track_metadata = http.get("https://our.provider/api/{}".format(query))
track = CustomAudioTrack({ # Create an instance of our CustomAudioTrack.
'identifier': '27cgqh0VRhVeM61ugTnorD', # Fill it with metadata that we've obtained from our source's provider.
'isSeekable': True,
'author': 'DJ Seinfeld',
'length': 296000,
'isStream': False,
'title': 'These Things Will Come To Be',
'uri': 'https://open.spotify.com/track/27cgqh0VRhVeM61ugTnorD'
}, requester=0) # Init requester with a default value.
return LoadResult(LoadType.TRACK, [track], playlist_info=PlaylistInfo.none())