async def lavalink_search(self, *, query: str, raw: bool = False, ctx: context.Context = None) -> objects.Search: async with self.client.session.get(url=f'{self.rest_url}/loadtracks?identifier={parse.quote(query)}', headers={'Authorization': self.password}) as response: data = await response.json() if raw is True: return data load_type = data.pop('loadType') if load_type == 'LOAD_FAILED': exception = data.get("exception") raise exceptions.VoiceError(f'There was an error of severity `{exception.get("severity")}` while loading tracks. Reason: `{exception.get("message")}`') elif load_type == 'NO_MATCHES': raise exceptions.VoiceError(f'The query `{query}` returned no tracks.') elif load_type == 'PLAYLIST_LOADED': playlist = objects.Playlist(playlist_info=data.get('playlistInfo'), raw_tracks=data.get('tracks'), ctx=ctx) return objects.Search(source=playlist.tracks[0].source, source_type='playlist', tracks=playlist.tracks, result=playlist) elif load_type == 'SEARCH_RESULT' or load_type == 'TRACK_LOADED': raw_tracks = data.get('tracks') if not raw_tracks: raise exceptions.VoiceError(f'The query `{query}` returned no tracks.') tracks = [objects.Track(track_id=track.get('track'), info=track.get('info'), ctx=ctx) for track in raw_tracks] return objects.Search(source=tracks[0].source, source_type='track', tracks=tracks, result=tracks)
async def spotify_search(self, *, query: str, spotify_type: str, spotify_id: str, ctx: context.Context = None) -> objects.Search: try: if spotify_type == 'track': result = await self.client.bot.spotify.get_track(spotify_id) spotify_tracks = [result] elif spotify_type == 'album': result = await self.client.bot.spotify.get_album(spotify_id) spotify_tracks = await result.get_tracks(limit=100) elif spotify_type == 'playlist': result = spotify.Playlist(self.client.bot.spotify, await self.client.bot.spotify_http.get_playlist(spotify_id)) spotify_tracks = await result.get_all_tracks() else: raise exceptions.VoiceError(f'The query `{query}` is not a valid spotify URL.') except spotify.NotFound: raise exceptions.VoiceError(f'The query `{query}` is not a valid spotify URL.') if not spotify_tracks: raise exceptions.VoiceError(f'The query `{query}` is a valid spotify URL however no tracks could be found for it.') tracks = [] for track in spotify_tracks: info = {'identifier': track.id, 'isSeekable': False, 'author': ', '.join([artist.name for artist in track.artists]), 'length': track.duration, 'isStream': False, 'position': 0, 'title': track.name, 'uri': track.url if track.url else 'spotify', 'thumbnail': track.images[0].url if track.images else None } tracks.append(objects.Track(track_id='', info=info, ctx=ctx)) return objects.Search(source='spotify', source_type=spotify_type, tracks=tracks, result=result)
async def decode_track(self, *, track_id: str, ctx: context.Context = None) -> objects.Track: async with self.client.session.get(f'{self.rest_url}/decodetrack?', headers={'Authorization': self.password}, params={'track': track_id}) as response: data = await response.json() if not response.status == 200: raise exceptions.VoiceError('Track id was not valid.') return objects.Track(track_id=track_id, info=data, ctx=ctx)