Add a spotify search for playlists and songs
This commit is contained in:
parent
7ee3725c7e
commit
3f7eaaa248
71
cogs/spotify.py
Normal file
71
cogs/spotify.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
from discord.ext import commands
|
||||
from base64 import urlsafe_b64encode
|
||||
|
||||
from . import utils
|
||||
|
||||
|
||||
class Spotify:
|
||||
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()
|
||||
)
|
||||
}
|
||||
self.bot.loop.create_task(self.api_token_task())
|
||||
|
||||
async def api_token_task(self):
|
||||
url = "https://accounts.spotify.com/api/token"
|
||||
opts = {"grant_type": "client_credentials"}
|
||||
while True:
|
||||
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")
|
||||
|
||||
await asyncio.wait(data.get("expires_in"))
|
||||
|
||||
@commands.group()
|
||||
@commands.guild_only()
|
||||
@utils.custom_perms(manage_guild=True)
|
||||
@utils.check_restricted()
|
||||
async def spotify(self, ctx, *, query):
|
||||
"""Searches Spotify for a song, giving you the link you can use to listen in"""
|
||||
# Setup the headers with the token that should be here
|
||||
headers = {"Authorization": "Bearer {}".format(self._token)}
|
||||
opts = {"q": query, "type": "track,artist"}
|
||||
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):
|
||||
await ctx.send("Couldn't find a song for:\n{}".format(query))
|
||||
|
||||
@spotify.command()
|
||||
@commands.guild_only()
|
||||
@utils.custom_perms(manage_guild=True)
|
||||
@utils.check_restricted()
|
||||
async def playlist(self, ctx, *, query):
|
||||
# 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):
|
||||
await ctx.send("Couldn't find a song for:\n{}".format(query))
|
||||
|
||||
|
||||
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(Spotify(bot))
|
Loading…
Reference in a new issue