changed search related methods to return lazylist. removed the search module.

master
Abhinav Sarkar 2008-08-28 07:01:30 +00:00
parent 9fdfaaf936
commit 10d0d76d6d
8 changed files with 115 additions and 165 deletions

View File

@ -14,7 +14,6 @@ from geo import Location, Country
from group import Group from group import Group
from playlist import Playlist from playlist import Playlist
from registry import Registry from registry import Registry
from search import SearchResult
from tag import Tag from tag import Tag
from tasteometer import Tasteometer from tasteometer import Tasteometer
from track import Track from track import Track
@ -22,4 +21,4 @@ from user import User
__all__ = ['LastfmError', 'Api', 'Album', 'Artist', 'Event', __all__ = ['LastfmError', 'Api', 'Album', 'Artist', 'Event',
'Location', 'Country', 'Group', 'Playlist', 'Tag', 'Location', 'Country', 'Group', 'Playlist', 'Tag',
'Tasteometer', 'Track', 'User', 'Registry', 'SearchResult'] 'Tasteometer', 'Track', 'User', 'Registry']

View File

@ -10,6 +10,7 @@ class Api(object):
DEFAULT_CACHE_TIMEOUT = 3600 # cache for 1 hour DEFAULT_CACHE_TIMEOUT = 3600 # cache for 1 hour
API_ROOT_URL = "http://ws.audioscrobbler.com/2.0/" API_ROOT_URL = "http://ws.audioscrobbler.com/2.0/"
FETCH_INTERVAL = 1 FETCH_INTERVAL = 1
SEARCH_XMLNS = "http://a9.com/-/spec/opensearch/1.1/"
def __init__(self, def __init__(self,
apiKey = '23caa86333d2cb2055fa82129802780a', apiKey = '23caa86333d2cb2055fa82129802780a',
@ -142,9 +143,8 @@ class Api(object):
def searchArtist(self, def searchArtist(self,
artist, artist,
limit = None, limit = None):
page = None): return Artist.search(self, artist, limit)
return Artist.search(self, artist, limit, page)
def getEvent(self, event): def getEvent(self, event):
return Event.getInfo(self, event) return Event.getInfo(self, event)
@ -169,9 +169,8 @@ class Api(object):
def searchTag(self, def searchTag(self,
tag, tag,
limit = None, limit = None):
page = None): return Tag.search(self, tag, limit)
return Tag.search(self, tag, limit, page)
def compareTaste(self, def compareTaste(self,
type1, type2, type1, type2,
@ -190,11 +189,10 @@ class Api(object):
def searchTrack(self, def searchTrack(self,
track, track,
artist = None, artist = None,
limit = None, limit = None):
page = None):
if isinstance(artist, Artist): if isinstance(artist, Artist):
artist = artist.name artist = artist.name
return Track.search(self, track, artist, limit, page) return Track.search(self, track, artist, limit)
def getUser(self, name): def getUser(self, name):
user = None user = None

View File

@ -5,6 +5,7 @@ __version__ = "0.1"
__license__ = "GNU Lesser General Public License" __license__ = "GNU Lesser General Public License"
from base import LastfmBase from base import LastfmBase
from lazylist import lazylist
class Artist(LastfmBase): class Artist(LastfmBase):
"""A class representing an artist.""" """A class representing an artist."""
@ -241,34 +242,39 @@ class Artist(LastfmBase):
@staticmethod @staticmethod
def search(api, def search(api,
artist, artist,
limit = None, limit = None):
page = None):
params = {'method': 'artist.search', 'artist': artist} params = {'method': 'artist.search', 'artist': artist}
if limit: if limit:
params.update({'limit': limit}) params.update({'limit': limit})
if page:
params.update({'page': page}) @lazylist
data = api._fetchData(params).find('results') def gen(lst):
return SearchResult( data = api._fetchData(params).find('results')
type = 'artist', totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'], int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)), @lazylist
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)), def gen2(lst, data):
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)), for a in data.findall('artistmatches/artist'):
matches = [ yield Artist(
Artist( api,
api, name = a.findtext('name'),
name = a.findtext('name'), mbid = a.findtext('mbid'),
mbid = a.findtext('mbid'), url = a.findtext('url'),
url = a.findtext('url'), image = dict([(i.get('size'), i.text) for i in a.findall('image')]),
image = dict([(i.get('size'), i.text) for i in a.findall('image')]), streamable = (a.findtext('streamable') == '1'),
streamable = (a.findtext('streamable') == '1'), )
)
for a in data.findall('artistmatches/artist') 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 @staticmethod
def _fetchData(api, def _fetchData(api,
artist = None, artist = None,
@ -400,7 +406,6 @@ from album import Album
from api import Api from api import Api
from error import LastfmError from error import LastfmError
from event import Event from event import Event
from search import SearchResult
from stats import Stats from stats import Stats
from tag import Tag from tag import Tag
from track import Track from track import Track

View File

@ -96,6 +96,9 @@ class LazyList(object):
class RecursiveLazyList(LazyList): class RecursiveLazyList(LazyList):
def __init__(self, prod, *args, **kwds): def __init__(self, prod, *args, **kwds):
super(RecursiveLazyList,self).__init__(prod(self,*args, **kwds)) super(RecursiveLazyList,self).__init__(prod(self,*args, **kwds))
def __repr__(self):
return "<lastfm.lazylist>"
class RecursiveLazyListFactory: class RecursiveLazyListFactory:
def __init__(self, producer): def __init__(self, producer):

View File

@ -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)

View File

@ -5,6 +5,7 @@ __version__ = "0.1"
__license__ = "GNU Lesser General Public License" __license__ = "GNU Lesser General Public License"
from base import LastfmBase from base import LastfmBase
from lazylist import lazylist
class Tag(LastfmBase): class Tag(LastfmBase):
""""A class representing a tag.""" """"A class representing a tag."""
@ -176,34 +177,39 @@ class Tag(LastfmBase):
@staticmethod @staticmethod
def search(api, def search(api,
tag, tag,
limit = None, limit = None):
page = None):
params = {'method': 'tag.search', 'tag': tag} params = {'method': 'tag.search', 'tag': tag}
if limit: if limit:
params.update({'limit': limit}) params.update({'limit': limit})
if page:
params.update({'page': page}) @lazylist
data = api._fetchData(params).find('results') def gen(lst):
return SearchResult( data = api._fetchData(params).find('results')
type = 'tag', totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'], int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)), @lazylist
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)), def gen2(lst, data):
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)), for t in data.findall('tagmatches/tag'):
matches = [ yield Tag(
Tag( api,
api, name = t.findtext('name'),
name = t.findtext('name'), url = t.findtext('url'),
url = t.findtext('url'), stats = Stats(
stats = Stats( subject = t.findtext('name'),
subject = t.findtext('name'), count = int(t.findtext('count')),
count = int(t.findtext('count')), )
) )
)
for t in data.findall('tagmatches/tag') 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 @staticmethod
def hashFunc(*args, **kwds): def hashFunc(*args, **kwds):
@ -228,6 +234,5 @@ from album import Album
from api import Api from api import Api
from artist import Artist from artist import Artist
from error import LastfmError from error import LastfmError
from search import SearchResult
from stats import Stats from stats import Stats
from track import Track from track import Track

View File

@ -5,6 +5,7 @@ __version__ = "0.1"
__license__ = "GNU Lesser General Public License" __license__ = "GNU Lesser General Public License"
from base import LastfmBase from base import LastfmBase
from lazylist import lazylist
class Track(LastfmBase): class Track(LastfmBase):
"""A class representing a track.""" """A class representing a track."""
@ -209,43 +210,48 @@ class Track(LastfmBase):
def search(api, def search(api,
track, track,
artist = None, artist = None,
limit = None, limit = None):
page = None):
params = {'method': 'track.search', 'track': track} params = {'method': 'track.search', 'track': track}
if artist is not None: if artist is not None:
params.update({'artist': artist}) params.update({'artist': artist})
if limit is not None: if limit is not None:
params.update({'limit': limit}) params.update({'limit': limit})
if page is not None:
params.update({'page': page}) @lazylist
data = api._fetchData(params).find('results') def gen(lst):
return SearchResult( data = api._fetchData(params).find('results')
type = 'track', totalPages = int(data.findtext("{%s}totalResults" % Api.SEARCH_XMLNS))/ \
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'], int(data.findtext("{%s}itemsPerPage" % Api.SEARCH_XMLNS)) + 1
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)), @lazylist
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)), def gen2(lst, data):
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)), for t in data.findall('trackmatches/track'):
matches = [ yield Track(
Track( api,
api, name = t.findtext('name'),
name = t.findtext('name'), artist = Artist(
artist = Artist( api,
api, name = t.findtext('artist')
name = t.findtext('artist') ),
), url = t.findtext('url'),
url = t.findtext('url'), stats = Stats(
stats = Stats( subject = t.findtext('name'),
subject = t.findtext('name'), listeners = int(t.findtext('listeners'))
listeners = int(t.findtext('listeners')) ),
), streamable = (t.findtext('streamable') == '1'),
streamable = (t.findtext('streamable') == '1'), fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'), image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
image = dict([(i.get('size'), i.text) for i in t.findall('image')]), )
)
for t in data.findall('trackmatches/track') 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 @staticmethod
def hashFunc(*args, **kwds): def hashFunc(*args, **kwds):
@ -275,7 +281,6 @@ class Track(LastfmBase):
from api import Api from api import Api
from artist import Artist from artist import Artist
from error import LastfmError from error import LastfmError
from search import SearchResult
from stats import Stats from stats import Stats
from tag import Tag from tag import Tag
from user import User from user import User

View File

@ -626,3 +626,7 @@ from weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, Weekly
#TODO #TODO
#write exceptions #write exceptions
#argument type checking #argument type checking
#extra methods in weeklycharts
#setup.py
#cross check with website
#registry issue