From aeb09c50a0183c4447aa8d5ef34d1a7e3e3fb992 Mon Sep 17 00:00:00 2001 From: rembo10 Date: Tue, 27 Mar 2012 00:19:34 +0100 Subject: [PATCH] Added index fetching, separated findAlbum and findArtist, updated apireference --- apireference | 9 +++--- headphones/api.py | 82 +++++++++++++++++++++++++++++------------------ 2 files changed, 55 insertions(+), 36 deletions(-) diff --git a/apireference b/apireference index a713c185..1497a45f 100644 --- a/apireference +++ b/apireference @@ -2,10 +2,9 @@ General structure: http://localhost:8181 + HTTP_ROOT + /api?apikey=$apikey&cmd=$command -Optional parameters: -format : json, xml - +Data returned in json format Commands: - -findArtist?name=$artistname&type={album,artist}[&limit=$limit] +&getIndex +&findArtist?name=$artistname[&limit=$limit] +&findAlbum?name=$albumname[&limit=$limit] \ No newline at end of file diff --git a/headphones/api.py b/headphones/api.py index ec08fcae..9c05ae80 100644 --- a/headphones/api.py +++ b/headphones/api.py @@ -3,8 +3,10 @@ import headphones from headphones import db, mb, logger import lib.simplejson as simplejson +from xml.dom.minidom import Document +import copy -cmd_list = [ 'findArtist'] +cmd_list = [ 'getIndex', 'findArtist'] class Api(object): @@ -12,12 +14,10 @@ class Api(object): 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): @@ -52,49 +52,69 @@ class Api(object): 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') + #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 == 'getIndex': + self._getIndex() + if self.cmd == 'findArtist': - self.findArtist(**self.kwargs) + self._findArtist(**self.kwargs) return simplejson.dumps(self.data) - def findArtist(self, **kwargs): - if 'type' not in kwargs: - self.data = 'Missing parameter: type' - return + def _getIndex(self): + myDB = db.DBConnection() + artists = myDB.select('SELECT * from artists order by ArtistSortName COLLATE NOCASE') + + artists_as_dic = [] + for artist in artists: + artist_as_dic = { + 'ArtistID' : artist['ArtistID'], + 'ArtistName' : artist['ArtistName'], + 'ArtistSortName' : artist['ArtistSortName'], + 'DateAdded' : artist['DateAdded'], + 'Status' : artist['Status'], + 'IncludeExtras' : artist['IncludeExtras'], + 'LatestAlbum' : artist['LatestAlbum'], + 'ReleaseDate' : artist['ReleaseDate'], + 'AlbumID' : artist['AlbumID'], + 'HaveTracks' : artist['HaveTracks'], + 'TotalTracks' : artist['TotalTracks']} + artists_as_dic.append(artist_as_dic) + + self.data = artists_as_dic + + def _findArtist(self, **kwargs): 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) + + self.data = mb.findArtist(kwargs['name'], limit) + + def _findAlbum(self, **kwargs): + if 'name' not in kwargs: + self.data = 'Missing parameter: name' + return + if 'limit' in kwargs: + limit = kwargs['limit'] else: - self.data = mb.findRelease(kwargs['name'], limit) - - - + limit=50 + self.data = mb.findRelease(kwargs['name'], limit) \ No newline at end of file