aboutsummaryrefslogtreecommitdiff
path: root/modules/spotify.py
blob: 6816cd504d743111ff997dc1e6b982669f4ac023 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import json
from src import ModuleManager, Utils

URL_SPOTIFY = "https://api.spotify.com/v1/search"

class Module(ModuleManager.BaseModule):
    @Utils.hook("received.command.spotify", min_args=1)
    def spotify(self, event):
        """
        Search for a track on spotify
        """
        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")