2016-04-06 12:56:12 +00:00
|
|
|
import json
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-04-06 12:56:12 +00:00
|
|
|
|
|
|
|
URL_SPOTIFY = "https://api.spotify.com/v1/search"
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
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
|
|
|
"""
|
2018-10-03 12:22:37 +00:00
|
|
|
page = utils.http.get_url(URL_SPOTIFY, get_params=
|
|
|
|
{"type": "track", "limit": 1, "q": event["args"]},
|
|
|
|
json=True)
|
2016-04-06 12:56:12 +00:00
|
|
|
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")
|