refactored out search API methods in searchable module. applied the module to artist, tag and track.
This commit is contained in:
parent
e1a9541f7d
commit
302f943805
|
@ -110,7 +110,7 @@ class Api(object):
|
|||
def searchArtist(self,
|
||||
artist,
|
||||
limit = None):
|
||||
return Artist.search(self, artist, limit)
|
||||
return Artist.search(self, searchItem = artist, limit = limit)
|
||||
|
||||
def getEvent(self, event):
|
||||
return Event.getInfo(self, event)
|
||||
|
@ -136,7 +136,7 @@ class Api(object):
|
|||
def searchTag(self,
|
||||
tag,
|
||||
limit = None):
|
||||
return Tag.search(self, tag, limit)
|
||||
return Tag.search(self, searchItem = tag, limit = limit)
|
||||
|
||||
def compareTaste(self,
|
||||
type1, type2,
|
||||
|
@ -155,7 +155,7 @@ class Api(object):
|
|||
limit = None):
|
||||
if isinstance(artist, Artist):
|
||||
artist = artist.name
|
||||
return Track.search(self, track, artist, limit)
|
||||
return Track.search(self, searchItem = track, limit = limit, artist = artist)
|
||||
|
||||
def getUser(self, name):
|
||||
user = None
|
||||
|
|
|
@ -7,9 +7,10 @@ __license__ = "GNU Lesser General Public License"
|
|||
from base import LastfmBase
|
||||
from taggable import Taggable
|
||||
from sharable import Sharable
|
||||
from searchable import Searchable
|
||||
from lazylist import lazylist
|
||||
|
||||
class Artist(LastfmBase, Taggable, Sharable):
|
||||
class Artist(LastfmBase, Taggable, Sharable, Searchable):
|
||||
"""A class representing an artist."""
|
||||
def init(self,
|
||||
api,
|
||||
|
@ -254,41 +255,15 @@ class Artist(LastfmBase, Taggable, Sharable):
|
|||
pass
|
||||
|
||||
@staticmethod
|
||||
def search(api,
|
||||
artist,
|
||||
limit = None):
|
||||
params = {'method': 'artist.search', 'artist': artist}
|
||||
if limit:
|
||||
params.update({'limit': limit})
|
||||
|
||||
@lazylist
|
||||
def gen(lst):
|
||||
data = api._fetchData(params).find('results')
|
||||
totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
|
||||
int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
|
||||
|
||||
@lazylist
|
||||
def gen2(lst, data):
|
||||
for a in data.findall('artistmatches/artist'):
|
||||
yield Artist(
|
||||
api,
|
||||
name = a.findtext('name'),
|
||||
mbid = a.findtext('mbid'),
|
||||
url = a.findtext('url'),
|
||||
image = dict([(i.get('size'), i.text) for i in a.findall('image')]),
|
||||
streamable = (a.findtext('streamable') == '1'),
|
||||
)
|
||||
|
||||
for a in gen2(data):
|
||||
yield a
|
||||
|
||||
for page in xrange(2, totalPages+1):
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
for a in gen2(data):
|
||||
yield a
|
||||
return gen()
|
||||
|
||||
def _searchYieldFunc(api, artist):
|
||||
return Artist(
|
||||
api,
|
||||
name = artist.findtext('name'),
|
||||
mbid = artist.findtext('mbid'),
|
||||
url = artist.findtext('url'),
|
||||
image = dict([(i.get('size'), i.text) for i in artist.findall('image')]),
|
||||
streamable = (artist.findtext('streamable') == '1'),
|
||||
)
|
||||
@staticmethod
|
||||
def _fetchData(api,
|
||||
artist = None,
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
|
||||
__version__ = "0.2"
|
||||
__license__ = "GNU Lesser General Public License"
|
||||
from lazylist import lazylist
|
||||
|
||||
class Searchable(object):
|
||||
@classmethod
|
||||
def search(cls,
|
||||
api,
|
||||
searchItem,
|
||||
limit = None,
|
||||
**kwds):
|
||||
from api import Api
|
||||
clsName = cls.__name__.lower()
|
||||
params = {
|
||||
'method': '%s.search'%clsName,
|
||||
clsName: searchItem
|
||||
}
|
||||
for kwd in kwds:
|
||||
if kwds[kwd] is not None:
|
||||
params[kwd] = kwds[kwd]
|
||||
|
||||
if limit:
|
||||
params.update({'limit': limit})
|
||||
|
||||
@lazylist
|
||||
def gen(lst):
|
||||
data = api._fetchData(params).find('results')
|
||||
totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
|
||||
int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
|
||||
|
||||
@lazylist
|
||||
def gen2(lst, data):
|
||||
for a in data.findall('%smatches/%s'%(clsName, clsName)):
|
||||
yield cls._searchYieldFunc(api, a)
|
||||
|
||||
for a in gen2(data):
|
||||
yield a
|
||||
|
||||
for page in xrange(2, totalPages+1):
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
for a in gen2(data):
|
||||
yield a
|
||||
return gen()
|
48
src/tag.py
48
src/tag.py
|
@ -5,9 +5,10 @@ __version__ = "0.2"
|
|||
__license__ = "GNU Lesser General Public License"
|
||||
|
||||
from base import LastfmBase
|
||||
from searchable import Searchable
|
||||
from lazylist import lazylist
|
||||
|
||||
class Tag(LastfmBase):
|
||||
class Tag(LastfmBase, Searchable):
|
||||
""""A class representing a tag."""
|
||||
def init(self,
|
||||
api,
|
||||
|
@ -186,41 +187,16 @@ class Tag(LastfmBase):
|
|||
]
|
||||
|
||||
@staticmethod
|
||||
def search(api,
|
||||
tag,
|
||||
limit = None):
|
||||
params = {'method': 'tag.search', 'tag': tag}
|
||||
if limit:
|
||||
params.update({'limit': limit})
|
||||
|
||||
@lazylist
|
||||
def gen(lst):
|
||||
data = api._fetchData(params).find('results')
|
||||
totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
|
||||
int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
|
||||
|
||||
@lazylist
|
||||
def gen2(lst, data):
|
||||
for t in data.findall('tagmatches/tag'):
|
||||
yield Tag(
|
||||
api,
|
||||
name = t.findtext('name'),
|
||||
url = t.findtext('url'),
|
||||
stats = Stats(
|
||||
subject = t.findtext('name'),
|
||||
count = int(t.findtext('count')),
|
||||
)
|
||||
)
|
||||
|
||||
for t in gen2(data):
|
||||
yield t
|
||||
|
||||
for page in xrange(2, totalPages+1):
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
for t in gen2(data):
|
||||
yield t
|
||||
return gen()
|
||||
def _searchYieldFunc(api, tag):
|
||||
return Tag(
|
||||
api,
|
||||
name = tag.findtext('name'),
|
||||
url = tag.findtext('url'),
|
||||
stats = Stats(
|
||||
subject = tag.findtext('name'),
|
||||
count = int(tag.findtext('count')),
|
||||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def hashFunc(*args, **kwds):
|
||||
|
|
67
src/track.py
67
src/track.py
|
@ -7,9 +7,10 @@ __license__ = "GNU Lesser General Public License"
|
|||
from base import LastfmBase
|
||||
from taggable import Taggable
|
||||
from sharable import Sharable
|
||||
from searchable import Searchable
|
||||
from lazylist import lazylist
|
||||
|
||||
class Track(LastfmBase, Taggable, Sharable):
|
||||
class Track(LastfmBase, Taggable, Sharable, Searchable):
|
||||
"""A class representing a track."""
|
||||
def init(self,
|
||||
api,
|
||||
|
@ -261,52 +262,24 @@ class Track(LastfmBase, Taggable, Sharable):
|
|||
self.__api._postData(params)
|
||||
|
||||
@staticmethod
|
||||
def search(api,
|
||||
track,
|
||||
artist = None,
|
||||
limit = None):
|
||||
params = {'method': 'track.search', 'track': track}
|
||||
if artist is not None:
|
||||
params.update({'artist': artist})
|
||||
if limit is not None:
|
||||
params.update({'limit': limit})
|
||||
|
||||
@lazylist
|
||||
def gen(lst):
|
||||
data = api._fetchData(params).find('results')
|
||||
totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
|
||||
int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
|
||||
|
||||
@lazylist
|
||||
def gen2(lst, data):
|
||||
for t in data.findall('trackmatches/track'):
|
||||
yield Track(
|
||||
api,
|
||||
name = t.findtext('name'),
|
||||
artist = Artist(
|
||||
api,
|
||||
name = t.findtext('artist')
|
||||
),
|
||||
url = t.findtext('url'),
|
||||
stats = Stats(
|
||||
subject = t.findtext('name'),
|
||||
listeners = int(t.findtext('listeners'))
|
||||
),
|
||||
streamable = (t.findtext('streamable') == '1'),
|
||||
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
|
||||
image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
|
||||
)
|
||||
|
||||
for t in gen2(data):
|
||||
yield t
|
||||
|
||||
for page in xrange(2, totalPages+1):
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
for t in gen2(data):
|
||||
yield t
|
||||
return gen()
|
||||
|
||||
def _searchYieldFunc(api, track):
|
||||
return Track(
|
||||
api,
|
||||
name = track.findtext('name'),
|
||||
artist = Artist(
|
||||
api,
|
||||
name=track.findtext('artist')
|
||||
),
|
||||
url = track.findtext('url'),
|
||||
stats = Stats(
|
||||
subject=track.findtext('name'),
|
||||
listeners=int(track.findtext('listeners'))
|
||||
),
|
||||
streamable = (track.findtext('streamable') == '1'),
|
||||
fullTrack = (track.find('streamable').attrib['fulltrack'] == '1'),
|
||||
image = dict([(i.get('size'), i.text) for i in track.findall('image')]),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _fetchData(api,
|
||||
artist = None,
|
||||
|
|
Loading…
Reference in New Issue