2018-05-08 03:02:02 +12:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
2018-09-23 04:17:11 +12:00
|
|
|
import traceback
|
2018-05-08 03:02:02 +12:00
|
|
|
|
|
|
|
from discord.ext import commands
|
|
|
|
from base64 import urlsafe_b64encode
|
|
|
|
|
2018-10-30 14:00:37 +13:00
|
|
|
import utils
|
2018-05-08 03:02:02 +12:00
|
|
|
|
|
|
|
|
|
|
|
class Spotify:
|
2018-10-09 09:40:21 +13:00
|
|
|
"""Pretty self-explanatory"""
|
|
|
|
|
2018-05-08 03:02:02 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self._token = None
|
|
|
|
self._client_id = utils.spotify_id or ""
|
|
|
|
self._client_secret = utils.spotify_secret or ""
|
|
|
|
|
|
|
|
self._authorization = "{}:{}".format(self._client_id, self._client_secret)
|
|
|
|
self.headers = {
|
|
|
|
"Authorization": "Basic {}".format(
|
|
|
|
urlsafe_b64encode(self._authorization.encode()).decode()
|
|
|
|
)
|
|
|
|
}
|
2018-09-23 04:17:11 +12:00
|
|
|
self.task = self.bot.loop.create_task(self.api_token_task())
|
2018-05-08 03:02:02 +12:00
|
|
|
|
|
|
|
async def api_token_task(self):
|
2018-09-23 04:17:11 +12:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
delay = await self.get_api_token()
|
|
|
|
except Exception as error:
|
2018-09-24 07:23:27 +12:00
|
|
|
delay = 2400
|
2018-09-23 04:17:11 +12:00
|
|
|
with open("error_log", 'a') as f:
|
|
|
|
traceback.print_tb(error.__traceback__, file=f)
|
|
|
|
print('{0.__class__.__name__}: {0}'.format(error), file=f)
|
|
|
|
finally:
|
|
|
|
await asyncio.sleep(delay)
|
|
|
|
|
|
|
|
async def get_api_token(self):
|
2018-05-08 03:02:02 +12:00
|
|
|
url = "https://accounts.spotify.com/api/token"
|
|
|
|
opts = {"grant_type": "client_credentials"}
|
2018-09-24 07:23:27 +12:00
|
|
|
async with aiohttp.ClientSession(headers=self.headers) as session:
|
|
|
|
response = await session.post(url, data=opts)
|
|
|
|
data = await response.json()
|
|
|
|
self._token = data.get("access_token")
|
|
|
|
return data.get("expires_in")
|
2018-05-08 03:02:02 +12:00
|
|
|
|
|
|
|
@commands.group(invoke_without_command=True)
|
2018-09-24 10:34:14 +12:00
|
|
|
@utils.can_run(send_messages=True)
|
2018-05-08 03:02:02 +12:00
|
|
|
async def spotify(self, ctx, *, query):
|
|
|
|
"""Searches Spotify for a song, giving you the link you can use to listen in. Give the query to search for
|
|
|
|
and it will search by title/artist for the best match
|
|
|
|
|
|
|
|
EXAMPLE: !spotify Eminem
|
|
|
|
RESULT: Some Eminem song"""
|
|
|
|
|
|
|
|
# Setup the headers with the token that should be here
|
|
|
|
headers = {"Authorization": "Bearer {}".format(self._token)}
|
|
|
|
opts = {"q": query, "type": "track"}
|
|
|
|
url = "https://api.spotify.com/v1/search"
|
|
|
|
response = await utils.request(url, headers=headers, payload=opts)
|
|
|
|
try:
|
|
|
|
await ctx.send(response.get("tracks").get("items")[0].get("external_urls").get("spotify"))
|
|
|
|
except (KeyError, AttributeError, IndexError):
|
|
|
|
await ctx.send("Couldn't find a song for:\n{}".format(query))
|
|
|
|
|
|
|
|
@spotify.command()
|
2018-09-24 10:34:14 +12:00
|
|
|
@utils.can_run(send_messages=True)
|
2018-05-08 03:02:02 +12:00
|
|
|
async def playlist(self, ctx, *, query):
|
|
|
|
"""Searches Spotify for a playlist, giving you the link you can use to listen in. Give the query to search for
|
|
|
|
and it will search for the best match
|
|
|
|
|
|
|
|
EXAMPLE: !spotify Eminem
|
|
|
|
RESULT: Some Eminem song"""
|
|
|
|
# Setup the headers with the token that should be here
|
|
|
|
headers = {"Authorization": "Bearer {}".format(self._token)}
|
|
|
|
opts = {"q": query, "type": "playlist"}
|
|
|
|
url = "https://api.spotify.com/v1/search"
|
|
|
|
response = await utils.request(url, headers=headers, payload=opts)
|
|
|
|
try:
|
|
|
|
await ctx.send(response.get("playlists").get("items")[0].get("external_urls").get("spotify"))
|
|
|
|
except (KeyError, AttributeError, IndexError):
|
|
|
|
await ctx.send("Couldn't find a song for:\n{}".format(query))
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Spotify(bot))
|