this might actually work

This commit is contained in:
Luke Rogers 2013-09-05 20:19:35 +12:00
parent f96a975b70
commit 74fc62139c

View file

@ -4,12 +4,9 @@ import re
from util import hook from util import hook
import oauth2 as oauth import oauth2 as oauth
CONSUMER_KEY = conn.conf['api_keys'].get("rdio_key", "KEY")
CONSUMER_SECRET = conn.conf['api_keys'].get("rdio_secret", "SECRET")
def getdata(inp, types, api_key, api_secret):
def getdata(inp, types): consumer = oauth.Consumer(api_key, api_secret)
consumer = oauth.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth.Client(consumer) client = oauth.Client(consumer)
response = client.request('http://api.rdio.com/1/', 'POST', response = client.request('http://api.rdio.com/1/', 'POST',
urllib.urlencode({'method': 'search', 'query': inp, 'types': types, 'count': '1'})) urllib.urlencode({'method': 'search', 'query': inp, 'types': types, 'count': '1'}))
@ -17,19 +14,14 @@ def getdata(inp, types):
return data return data
def checkkeys():
if CONSUMER_KEY == "KEY" or CONSUMER_SECRET == "SECRET":
return True
else:
return False
@hook.command @hook.command
def rdio(inp): def rdio(inp, bot=None):
""" rdio <search term> - alternatives: .rdiot (track), .rdioar (artist), .rdioal (album) """ """ rdio <search term> - alternatives: .rdiot (track), .rdioar (artist), .rdioal (album) """
if checkkeys(): api_key = bot.config.get("api_keys", {}).get("rdio_key")
return "This command requires an API key, please enter one in the config" api_secret = bot.config.get("api_keys", {}).get("rdio_secret")
data = getdata(inp, "Track,Album,Artist") if not api_key:
return "error: no api key set"
data = getdata(inp, "Track,Album,Artist", api_key, api_secret)
try: try:
info = data['result']['results'][0] info = data['result']['results'][0]
except IndexError: except IndexError:
@ -53,11 +45,13 @@ def rdio(inp):
@hook.command @hook.command
def rdiot(inp): def rdiot(inp, bot=None):
""" rdiot <search term> - Search for tracks on rdio """ """ rdiot <search term> - Search for tracks on rdio """
if checkkeys(): api_key = bot.config.get("api_keys", {}).get("rdio_key")
return "This command requires an API key, please enter one in the config" api_secret = bot.config.get("api_keys", {}).get("rdio_secret")
data = getdata(inp, "Track") if not api_key:
return "error: no api key set"
data = getdata(inp, "Track", api_key, api_secret)
try: try:
info = data['result']['results'][0] info = data['result']['results'][0]
except IndexError: except IndexError:
@ -70,11 +64,13 @@ def rdiot(inp):
@hook.command @hook.command
def rdioar(inp): def rdioar(inp, bot=None):
""" rdioar <search term> - Search for artists on rdio """ """ rdioar <search term> - Search for artists on rdio """
if checkkeys(): api_key = bot.config.get("api_keys", {}).get("rdio_key")
return "This command requires an API key, please enter one in the config" api_secret = bot.config.get("api_keys", {}).get("rdio_secret")
data = getdata(inp, "Artist") if not api_key:
return "error: no api key set"
data = getdata(inp, "Artist", api_key, api_secret)
try: try:
info = data['result']['results'][0] info = data['result']['results'][0]
except IndexError: except IndexError:
@ -85,11 +81,13 @@ def rdioar(inp):
@hook.command @hook.command
def rdioal(inp): def rdioal(inp, bot=None):
""" rdioal <search term> - Search for albums on rdio """ """ rdioal <search term> - Search for albums on rdio """
if checkkeys(): api_key = bot.config.get("api_keys", {}).get("rdio_key")
return "This command requires an API key, please enter one in the config" api_secret = bot.config.get("api_keys", {}).get("rdio_secret")
data = getdata(inp, "Album") if not api_key:
return "error: no api key set"
data = getdata(inp, "Album", api_key, api_secret)
try: try:
info = data['result']['results'][0] info = data['result']['results'][0]
except IndexError: except IndexError:
@ -104,11 +102,13 @@ rdio_re = (r'(.*:)//(rd.io|www.rdio.com|rdio.com)(:[0-9]+)?(.*)', re.I)
@hook.regex(*rdio_re) @hook.regex(*rdio_re)
def rdio_url(match): def rdio_url(match, bot=None):
if checkkeys(): api_key = bot.config.get("api_keys", {}).get("rdio_key")
api_secret = bot.config.get("api_keys", {}).get("rdio_secret")
if not api_key:
return None return None
url = match.group(1) + "//" + match.group(2) + match.group(4) url = match.group(1) + "//" + match.group(2) + match.group(4)
consumer = oauth.Consumer(CONSUMER_KEY, CONSUMER_SECRET) consumer = oauth.Consumer(api_key, api_secret)
client = oauth.Client(consumer) client = oauth.Client(consumer)
response = client.request('http://api.rdio.com/1/', 'POST', response = client.request('http://api.rdio.com/1/', 'POST',
urllib.urlencode({'method': 'getObjectFromUrl', 'url': url})) urllib.urlencode({'method': 'getObjectFromUrl', 'url': url}))