diff --git a/bot.json.example b/bot.json.example index 0f3f525f..5ac30a5b 100644 --- a/bot.json.example +++ b/bot.json.example @@ -11,5 +11,6 @@ "twitter-access-token" : "", "twitter-access-secret" : "", "trakt-api-key" : "", - "bitly-api-key" : "" + "bitly-api-key" : "", + "soundcloud-api-key" : "" } diff --git a/modules/soundcloud.py b/modules/soundcloud.py new file mode 100644 index 00000000..9aa15508 --- /dev/null +++ b/modules/soundcloud.py @@ -0,0 +1,34 @@ +#--require-config soundcloud-api-key + +import json, time +import Utils + +URL_SOUNDCLOUD = "http://api.soundcloud.com/tracks" + +class Module(object): + _name = "SoundCloud" + def __init__(self, bot): + self.bot = bot + bot.events.on("received").on("command").on("soundcloud", "sc" + ).hook(self.soundcloud, min_args=1, help="Search SoundCloud") + + def soundcloud(self, event): + page = Utils.get_url(URL_SOUNDCLOUD, get_params={ + "client_id": self.bot.config["soundcloud-api-key"], + "limit": 1, "q": event["args"]}, json=True) + if page: + if page: + page = page[0] + title = page["title"] + user = page["user"]["username"] + duration = time.strftime("%H:%M:%S", time.gmtime(page[ + "duration"]/1000)) + if duration.startswith("00:"): + duration = duration[3:] + link = page["permalink_url"] + event["stdout"].write("%s [%s] (posted by %s) %s" % (title, + duration, user, link)) + else: + event["stderr"].write("No results found") + else: + event["stderr"].write("Failed to load results") diff --git a/modules/spotify.py b/modules/spotify.py new file mode 100644 index 00000000..64ba2c2f --- /dev/null +++ b/modules/spotify.py @@ -0,0 +1,26 @@ +import json +import Utils + +URL_SPOTIFY = "https://api.spotify.com/v1/search" + +class Module(object): + def __init__(self, bot): + bot.events.on("received").on("command").on("spotify").hook( + self.spotify, help="Search for a track on spotify", + min_args=1) + + 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")