changed search related methods to return lazylist. removed the search module.
parent
9fdfaaf936
commit
10d0d76d6d
|
@ -14,7 +14,6 @@ from geo import Location, Country
|
|||
from group import Group
|
||||
from playlist import Playlist
|
||||
from registry import Registry
|
||||
from search import SearchResult
|
||||
from tag import Tag
|
||||
from tasteometer import Tasteometer
|
||||
from track import Track
|
||||
|
@ -22,4 +21,4 @@ from user import User
|
|||
|
||||
__all__ = ['LastfmError', 'Api', 'Album', 'Artist', 'Event',
|
||||
'Location', 'Country', 'Group', 'Playlist', 'Tag',
|
||||
'Tasteometer', 'Track', 'User', 'Registry', 'SearchResult']
|
||||
'Tasteometer', 'Track', 'User', 'Registry']
|
16
src/api.py
16
src/api.py
|
@ -10,6 +10,7 @@ class Api(object):
|
|||
DEFAULT_CACHE_TIMEOUT = 3600 # cache for 1 hour
|
||||
API_ROOT_URL = "http://ws.audioscrobbler.com/2.0/"
|
||||
FETCH_INTERVAL = 1
|
||||
SEARCH_XMLNS = "http://a9.com/-/spec/opensearch/1.1/"
|
||||
|
||||
def __init__(self,
|
||||
apiKey = '23caa86333d2cb2055fa82129802780a',
|
||||
|
@ -142,9 +143,8 @@ class Api(object):
|
|||
|
||||
def searchArtist(self,
|
||||
artist,
|
||||
limit = None,
|
||||
page = None):
|
||||
return Artist.search(self, artist, limit, page)
|
||||
limit = None):
|
||||
return Artist.search(self, artist, limit)
|
||||
|
||||
def getEvent(self, event):
|
||||
return Event.getInfo(self, event)
|
||||
|
@ -169,9 +169,8 @@ class Api(object):
|
|||
|
||||
def searchTag(self,
|
||||
tag,
|
||||
limit = None,
|
||||
page = None):
|
||||
return Tag.search(self, tag, limit, page)
|
||||
limit = None):
|
||||
return Tag.search(self, tag, limit)
|
||||
|
||||
def compareTaste(self,
|
||||
type1, type2,
|
||||
|
@ -190,11 +189,10 @@ class Api(object):
|
|||
def searchTrack(self,
|
||||
track,
|
||||
artist = None,
|
||||
limit = None,
|
||||
page = None):
|
||||
limit = None):
|
||||
if isinstance(artist, Artist):
|
||||
artist = artist.name
|
||||
return Track.search(self, track, artist, limit, page)
|
||||
return Track.search(self, track, artist, limit)
|
||||
|
||||
def getUser(self, name):
|
||||
user = None
|
||||
|
|
|
@ -5,6 +5,7 @@ __version__ = "0.1"
|
|||
__license__ = "GNU Lesser General Public License"
|
||||
|
||||
from base import LastfmBase
|
||||
from lazylist import lazylist
|
||||
|
||||
class Artist(LastfmBase):
|
||||
"""A class representing an artist."""
|
||||
|
@ -241,34 +242,39 @@ class Artist(LastfmBase):
|
|||
@staticmethod
|
||||
def search(api,
|
||||
artist,
|
||||
limit = None,
|
||||
page = None):
|
||||
limit = None):
|
||||
params = {'method': 'artist.search', 'artist': artist}
|
||||
if limit:
|
||||
params.update({'limit': limit})
|
||||
if page:
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
return SearchResult(
|
||||
type = 'artist',
|
||||
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'],
|
||||
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
|
||||
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)),
|
||||
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)),
|
||||
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)),
|
||||
matches = [
|
||||
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 data.findall('artistmatches/artist')
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@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()
|
||||
|
||||
@staticmethod
|
||||
def _fetchData(api,
|
||||
artist = None,
|
||||
|
@ -400,7 +406,6 @@ from album import Album
|
|||
from api import Api
|
||||
from error import LastfmError
|
||||
from event import Event
|
||||
from search import SearchResult
|
||||
from stats import Stats
|
||||
from tag import Tag
|
||||
from track import Track
|
||||
|
|
|
@ -96,6 +96,9 @@ class LazyList(object):
|
|||
class RecursiveLazyList(LazyList):
|
||||
def __init__(self, prod, *args, **kwds):
|
||||
super(RecursiveLazyList,self).__init__(prod(self,*args, **kwds))
|
||||
|
||||
def __repr__(self):
|
||||
return "<lastfm.lazylist>"
|
||||
|
||||
class RecursiveLazyListFactory:
|
||||
def __init__(self, producer):
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
|
||||
__version__ = "0.1"
|
||||
__license__ = "GNU Lesser General Public License"
|
||||
|
||||
from base import LastfmBase
|
||||
|
||||
class SearchResult(object):
|
||||
"""A class to represent a search result"""
|
||||
xmlns = "http://a9.com/-/spec/opensearch/1.1/"
|
||||
def __init__(self,
|
||||
type = None,
|
||||
searchTerms = None,
|
||||
startPage = None,
|
||||
totalResults = None,
|
||||
startIndex = None,
|
||||
itemsPerPage = None,
|
||||
matches = None):
|
||||
self.__type = type
|
||||
self.__searchTerms = searchTerms
|
||||
self.__startPage = startPage
|
||||
self.__totalResults = totalResults
|
||||
self.__startIndex = startIndex
|
||||
self.__itemsPerPage = itemsPerPage
|
||||
self.__matches = matches
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
"""type of the search"""
|
||||
return self.__type
|
||||
|
||||
@property
|
||||
def searchTerms(self):
|
||||
"""terms searched for in the search"""
|
||||
return self.__searchTerms
|
||||
|
||||
@property
|
||||
def startPage(self):
|
||||
"""start page of the search"""
|
||||
return self.__startPage
|
||||
|
||||
@property
|
||||
def totalResults(self):
|
||||
"""number of total results for the search"""
|
||||
return self.__totalResults
|
||||
|
||||
@property
|
||||
def startIndex(self):
|
||||
"""start index of the search"""
|
||||
return self.__startIndex
|
||||
|
||||
@property
|
||||
def itemsPerPage(self):
|
||||
"""number of items per page for the search"""
|
||||
return self.__itemsPerPage
|
||||
|
||||
@property
|
||||
def matches(self):
|
||||
"""match result of the search"""
|
||||
return self.__matches
|
||||
|
||||
@LastfmBase.topProperty("matches")
|
||||
def topMatch(self):
|
||||
"""top match for the search"""
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return "<lastfm.SearchResult: %s results for %s '%s'>" % (self.totalResults, self.type, self.searchTerms)
|
57
src/tag.py
57
src/tag.py
|
@ -5,6 +5,7 @@ __version__ = "0.1"
|
|||
__license__ = "GNU Lesser General Public License"
|
||||
|
||||
from base import LastfmBase
|
||||
from lazylist import lazylist
|
||||
|
||||
class Tag(LastfmBase):
|
||||
""""A class representing a tag."""
|
||||
|
@ -176,34 +177,39 @@ class Tag(LastfmBase):
|
|||
@staticmethod
|
||||
def search(api,
|
||||
tag,
|
||||
limit = None,
|
||||
page = None):
|
||||
limit = None):
|
||||
params = {'method': 'tag.search', 'tag': tag}
|
||||
if limit:
|
||||
params.update({'limit': limit})
|
||||
if page:
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
return SearchResult(
|
||||
type = 'tag',
|
||||
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'],
|
||||
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
|
||||
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)),
|
||||
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)),
|
||||
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)),
|
||||
matches = [
|
||||
Tag(
|
||||
api,
|
||||
name = t.findtext('name'),
|
||||
url = t.findtext('url'),
|
||||
stats = Stats(
|
||||
subject = t.findtext('name'),
|
||||
count = int(t.findtext('count')),
|
||||
)
|
||||
)
|
||||
for t in data.findall('tagmatches/tag')
|
||||
]
|
||||
)
|
||||
|
||||
@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()
|
||||
|
||||
@staticmethod
|
||||
def hashFunc(*args, **kwds):
|
||||
|
@ -228,6 +234,5 @@ from album import Album
|
|||
from api import Api
|
||||
from artist import Artist
|
||||
from error import LastfmError
|
||||
from search import SearchResult
|
||||
from stats import Stats
|
||||
from track import Track
|
71
src/track.py
71
src/track.py
|
@ -5,6 +5,7 @@ __version__ = "0.1"
|
|||
__license__ = "GNU Lesser General Public License"
|
||||
|
||||
from base import LastfmBase
|
||||
from lazylist import lazylist
|
||||
|
||||
class Track(LastfmBase):
|
||||
"""A class representing a track."""
|
||||
|
@ -209,43 +210,48 @@ class Track(LastfmBase):
|
|||
def search(api,
|
||||
track,
|
||||
artist = None,
|
||||
limit = None,
|
||||
page = 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})
|
||||
if page is not None:
|
||||
params.update({'page': page})
|
||||
data = api._fetchData(params).find('results')
|
||||
return SearchResult(
|
||||
type = 'track',
|
||||
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'],
|
||||
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
|
||||
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)),
|
||||
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)),
|
||||
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)),
|
||||
matches = [
|
||||
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 data.findall('trackmatches/track')
|
||||
]
|
||||
)
|
||||
|
||||
@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()
|
||||
|
||||
@staticmethod
|
||||
def hashFunc(*args, **kwds):
|
||||
|
@ -275,7 +281,6 @@ class Track(LastfmBase):
|
|||
from api import Api
|
||||
from artist import Artist
|
||||
from error import LastfmError
|
||||
from search import SearchResult
|
||||
from stats import Stats
|
||||
from tag import Tag
|
||||
from user import User
|
||||
|
|
|
@ -626,3 +626,7 @@ from weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, Weekly
|
|||
#TODO
|
||||
#write exceptions
|
||||
#argument type checking
|
||||
#extra methods in weeklycharts
|
||||
#setup.py
|
||||
#cross check with website
|
||||
#registry issue
|
Loading…
Reference in New Issue