diff --git a/Headphones.py b/Headphones.py index 0b7aca50..65c7d8fc 100644 --- a/Headphones.py +++ b/Headphones.py @@ -109,7 +109,6 @@ def main(): 'http_root': headphones.HTTP_ROOT, 'http_username': headphones.HTTP_USERNAME, 'http_password': headphones.HTTP_PASSWORD, - 'api': headphones.API_ENABLED, }) logger.info('Starting Headphones on port: %i' % http_port) diff --git a/apireference b/apireference new file mode 100644 index 00000000..a713c185 --- /dev/null +++ b/apireference @@ -0,0 +1,11 @@ + +General structure: +http://localhost:8181 + HTTP_ROOT + /api?apikey=$apikey&cmd=$command + +Optional parameters: +format : json, xml + + +Commands: + +findArtist?name=$artistname&type={album,artist}[&limit=$limit] diff --git a/headphones/api.py b/headphones/api.py new file mode 100644 index 00000000..ec08fcae --- /dev/null +++ b/headphones/api.py @@ -0,0 +1,100 @@ +import headphones + +from headphones import db, mb, logger + +import lib.simplejson as simplejson + +cmd_list = [ 'findArtist'] + +class Api(object): + + def __init__(self): + + self.apikey = None + self.cmd = None + self.format = 'json' + self.id = None + + self.kwargs = None + + self.rawdata = None + self.data = None + + def checkParams(self,*args,**kwargs): + + if not headphones.API_ENABLED: + self.data = 'API not enabled' + return + if not headphones.API_KEY: + self.data = 'API key not generated' + return + if len(headphones.API_KEY) != 32: + self.data = 'API key not generated correctly' + return + + if 'apikey' not in kwargs: + self.data = 'Missing api key' + return + + if kwargs['apikey'] != headphones.API_KEY: + self.data = 'Incorrect API key' + return + else: + self.apikey = kwargs.pop('apikey') + + if 'cmd' not in kwargs: + self.data = 'Missing parameter: cmd' + return + + if kwargs['cmd'] not in cmd_list: + self.data = 'Unknown command: %s' % kwargs['cmd'] + return + else: + self.cmd = kwargs.pop('cmd') + + if 'format' not in kwargs: + self.format = 'json' + else: + if kwargs['format'] not in ['json', 'xml']: + self.data = 'Unknown format: %s' % kwargs['format'] + return + else: + self.format = kwargs.pop('format') + + self.kwargs = kwargs + self.data = 'OK' + + def formatData(self): + + self.data = '%s' % self.data + + def fetchData(self): + + if self.cmd == 'findArtist': + self.findArtist(**self.kwargs) + + return simplejson.dumps(self.data) + + def findArtist(self, **kwargs): + if 'type' not in kwargs: + self.data = 'Missing parameter: type' + return + if 'name' not in kwargs: + self.data = 'Missing parameter: name' + return + if kwargs['type'] not in ['artist','album']: + self.data = 'Incorrect type: %s' % kwargs['type'] + return + if 'limit' in kwargs: + limit = kwargs['limit'] + else: + limit=50 + if kwargs['type'] == 'artist': + self.data = mb.findArtist(kwargs['name'], limit) + else: + self.data = mb.findRelease(kwargs['name'], limit) + + + + + \ No newline at end of file diff --git a/headphones/webserve.py b/headphones/webserve.py index d611a7d2..111bde3e 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -552,3 +552,17 @@ class WebInterface(object): raise cherrypy.HTTPRedirect("extras") updateCloud.exposed = True + + def api(self, *args, **kwargs): + + from headphones.api import Api + + a = Api() + + a.checkParams(*args, **kwargs) + + data = a.fetchData() + + return data + + api.exposed = True diff --git a/headphones/webstart.py b/headphones/webstart.py index 4726ff5f..2c462c1c 100644 --- a/headphones/webstart.py +++ b/headphones/webstart.py @@ -43,9 +43,6 @@ def initialize(options={}): 'tools.staticfile.filename': "images/favicon.ico" } } - - if options['api']: - conf['/api'] = {'tools.staticdir.on': True, 'tools.staticdir.filename': "api"} if options['http_password'] != "": conf['/'].update({