added spotify.py and soundcloud.py, updated bot.json.example.

This commit is contained in:
jesopo 2016-04-06 13:56:12 +01:00
parent 9b2556a65a
commit 7f7a1f6d55
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3
3 changed files with 62 additions and 1 deletions

View file

@ -11,5 +11,6 @@
"twitter-access-token" : "",
"twitter-access-secret" : "",
"trakt-api-key" : "",
"bitly-api-key" : ""
"bitly-api-key" : "",
"soundcloud-api-key" : ""
}

34
modules/soundcloud.py Normal file
View file

@ -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")

26
modules/spotify.py Normal file
View file

@ -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")