2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
2019-01-29 22:34:28 +00:00
|
|
|
#--require-config spotify-client-id
|
|
|
|
#--require-config spotify-client-secret
|
|
|
|
|
2019-01-29 22:39:13 +00:00
|
|
|
import base64, json, time
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-04-06 12:56:12 +00:00
|
|
|
|
2019-01-29 22:34:28 +00:00
|
|
|
URL_SEARCH = "https://api.spotify.com/v1/search"
|
|
|
|
URL_TOKEN = "https://accounts.spotify.com/api/token"
|
2016-04-06 12:56:12 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-01-29 22:34:28 +00:00
|
|
|
def on_load(self):
|
|
|
|
self._token = None
|
|
|
|
self._token_expires = None
|
|
|
|
|
|
|
|
def _get_token(self):
|
2019-01-29 22:46:00 +00:00
|
|
|
if self._token and time.time() < (self._token_expires+10):
|
2019-01-29 22:34:28 +00:00
|
|
|
return self._token
|
|
|
|
else:
|
|
|
|
client_id = self.bot.config["spotify-client-id"]
|
|
|
|
client_secret = self.bot.config["spotify-client-secret"]
|
|
|
|
bearer = "%s:%s" % (client_id, client_secret)
|
2019-01-29 22:37:44 +00:00
|
|
|
bearer = base64.b64encode(bearer.encode("utf8")).decode("utf8")
|
2019-01-29 22:34:28 +00:00
|
|
|
|
|
|
|
page = utils.http.request(URL_TOKEN, method="POST",
|
|
|
|
headers={"Authorization": "Basic %s" % bearer},
|
2019-01-29 22:38:31 +00:00
|
|
|
post_data={"grant_type": "client_credentials"},
|
2019-01-29 22:34:28 +00:00
|
|
|
json=True)
|
2019-01-29 22:46:00 +00:00
|
|
|
|
|
|
|
token = page.data["access_token"]
|
|
|
|
self._token = token
|
|
|
|
self._token_expires = time.time()+page.data["expires_in"]
|
|
|
|
return token
|
2019-01-29 22:34:28 +00:00
|
|
|
|
2019-02-08 15:34:04 +00:00
|
|
|
@utils.hook("received.command.sp", alias_of="spotify")
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.spotify", min_args=1)
|
2016-04-06 12:56:12 +00:00
|
|
|
def spotify(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Search for a track on spotify
|
|
|
|
:usage: <term>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-01-29 22:34:28 +00:00
|
|
|
token = self._get_token()
|
2019-01-29 22:39:58 +00:00
|
|
|
page = utils.http.request(URL_SEARCH,
|
2019-01-29 22:34:28 +00:00
|
|
|
get_params={"type": "track", "limit": 1, "q": event["args"]},
|
2019-01-29 22:40:44 +00:00
|
|
|
headers={"Authorization": "Bearer %s" % token},
|
2018-10-03 12:22:37 +00:00
|
|
|
json=True)
|
2016-04-06 12:56:12 +00:00
|
|
|
if page:
|
2018-12-11 22:26:38 +00:00
|
|
|
if len(page.data["tracks"]["items"]):
|
|
|
|
item = page.data["tracks"]["items"][0]
|
2016-04-06 12:56:12 +00:00
|
|
|
title = item["name"]
|
|
|
|
artist_name = item["artists"][0]["name"]
|
|
|
|
url = item["external_urls"]["spotify"]
|
|
|
|
event["stdout"].write("%s (by %s) %s" % (title, artist_name,
|
|
|
|
url))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No results found")
|
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|