2016-04-06 12:56:12 +00:00
|
|
|
import json
|
2018-09-24 14:13:27 +00:00
|
|
|
from src import Utils
|
2016-04-06 12:56:12 +00:00
|
|
|
|
|
|
|
URL_SPOTIFY = "https://api.spotify.com/v1/search"
|
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command.spotify").hook(self.spotify, min_args=1,
|
|
|
|
help="Search for a track on spotify")
|
2016-04-06 12:56:12 +00:00
|
|
|
|
|
|
|
def spotify(self, event):
|
|
|
|
page = Utils.get_url(URL_SPOTIFY, get_params={"type": "track",
|
|
|
|
"limit": 1, "q": event["args"]}, json=True)
|
|
|
|
if page:
|
|
|
|
if len(page["tracks"]["items"]):
|
|
|
|
item = page["tracks"]["items"][0]
|
|
|
|
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:
|
|
|
|
event["stderr"].write("Failed to load results")
|