added shout and shoutable modules to implement getShout method in artist, user and event classes, moved the mixin modules to a separate directory, changed all imports to absolute imports

master
Abhinav Sarkar 2008-12-31 05:30:23 +00:00
parent 4e22b3c546
commit 0cec26c46a
30 changed files with 275 additions and 161 deletions

27
lastfm/__init__.py Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from lastfm.album import Album
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.error import LastfmError
from lastfm.event import Event
from lastfm.geo import Location, Country
from lastfm.group import Group
from lastfm.playlist import Playlist
from lastfm.objectcache import ObjectCache
from lastfm.tag import Tag
from lastfm.tasteometer import Tasteometer
from lastfm.track import Track
from lastfm.user import User
__all__ = ['LastfmError', 'Api', 'Album', 'Artist', 'Event',
'Location', 'Country', 'Group', 'Playlist', 'Tag',
'Tasteometer', 'Track', 'User', 'ObjectCache']

View File

@ -4,9 +4,8 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from taggable import Taggable
from cacheable import Cacheable
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable, Taggable
class Album(LastfmBase, Cacheable, Taggable):
"""A class representing an album."""
@ -214,9 +213,9 @@ class Album(LastfmBase, Cacheable, Taggable):
from datetime import datetime
import time
from api import Api
from artist import Artist
from error import InvalidParametersError
from playlist import Playlist
from stats import Stats
from tag import Tag
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.error import InvalidParametersError
from lastfm.playlist import Playlist
from lastfm.stats import Stats
from lastfm.tag import Tag

View File

@ -4,7 +4,7 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from lastfm.base import LastfmBase
class Api(object):
"""The class representing the last.fm web services API."""
@ -378,18 +378,18 @@ import urllib
import urllib2
import urlparse
from album import Album
from artist import Artist
from error import error_map, LastfmError, OperationFailedError, AuthenticationFailedError
from event import Event
from filecache import FileCache
from geo import Location, Country
from group import Group
from playlist import Playlist
from tag import Tag
from tasteometer import Tasteometer
from track import Track
from user import User
from lastfm.album import Album
from lastfm.artist import Artist
from lastfm.error import error_map, LastfmError, OperationFailedError, AuthenticationFailedError
from lastfm.event import Event
from lastfm.filecache import FileCache
from lastfm.geo import Location, Country
from lastfm.group import Group
from lastfm.playlist import Playlist
from lastfm.tag import Tag
from lastfm.tasteometer import Tasteometer
from lastfm.track import Track
from lastfm.user import User
if sys.version_info >= (2, 5):
import xml.etree.cElementTree as ElementTree

View File

@ -4,14 +4,11 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from taggable import Taggable
from sharable import Sharable
from searchable import Searchable
from lazylist import lazylist
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable, Searchable, Sharable, Shoutable, Taggable
from lastfm.lazylist import lazylist
class Artist(LastfmBase, Cacheable, Sharable, Searchable, Taggable):
class Artist(LastfmBase, Cacheable, Sharable, Shoutable, Searchable, Taggable):
"""A class representing an artist."""
def init(self,
api,
@ -26,8 +23,10 @@ class Artist(LastfmBase, Cacheable, Sharable, Searchable, Taggable):
bio = None):
if not isinstance(api, Api):
raise InvalidParametersError("api reference must be supplied as an argument")
Taggable.init(self, api)
Sharable.init(self, api)
Shoutable.init(self, api)
Taggable.init(self, api)
self._api = api
self._name = name
self._mbid = mbid
@ -358,12 +357,12 @@ class Artist(LastfmBase, Cacheable, Sharable, Searchable, Taggable):
from datetime import datetime
import time
from album import Album
from api import Api
from error import InvalidParametersError
from event import Event
from stats import Stats
from tag import Tag
from track import Track
from user import User
from wiki import Wiki
from lastfm.album import Album
from lastfm.api import Api
from lastfm.error import InvalidParametersError
from lastfm.event import Event
from lastfm.stats import Stats
from lastfm.tag import Tag
from lastfm.track import Track
from lastfm.user import User
from lastfm.wiki import Wiki

View File

@ -47,4 +47,4 @@ class LastfmBase(object):
return not self.__gt__(other)
import copy
from error import LastfmError
from lastfm.error import LastfmError

View File

@ -4,11 +4,10 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from sharable import Sharable
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable, Sharable, Shoutable
class Event(LastfmBase, Cacheable, Sharable):
class Event(LastfmBase, Cacheable, Sharable, Shoutable):
"""A class representing an event."""
STATUS_ATTENDING = 0
STATUS_MAYBE = 1
@ -30,6 +29,8 @@ class Event(LastfmBase, Cacheable, Sharable):
if not isinstance(api, Api):
raise InvalidParametersError("api reference must be supplied as an argument")
Sharable.init(self, api)
Shoutable.init(self, api)
self._api = api
self._id = id
self._title = title
@ -216,8 +217,8 @@ class Event(LastfmBase, Cacheable, Sharable):
from datetime import datetime
import time
from api import Api
from artist import Artist
from error import InvalidParametersError
from geo import Venue, Location, Country
from stats import Stats
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.error import InvalidParametersError
from lastfm.geo import Venue, Location, Country
from lastfm.stats import Stats

View File

@ -4,9 +4,9 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from lazylist import lazylist
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable
from lastfm.lazylist import lazylist
class Geo(object):
"""A class representing an geographic location."""
@ -317,9 +317,9 @@ class Country(LastfmBase, Cacheable):
def __repr__(self):
return "<lastfm.geo.Country: %s>" % self.name
from api import Api
from artist import Artist
from error import InvalidParametersError
from event import Event
from stats import Stats
from track import Track
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.error import InvalidParametersError
from lastfm.event import Event
from lastfm.stats import Stats
from lastfm.track import Track

View File

@ -4,9 +4,9 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from lazylist import lazylist
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable
from lastfm.lazylist import lazylist
class Group(LastfmBase, Cacheable):
"""A class representing a group on last.fm."""
@ -123,6 +123,6 @@ class Group(LastfmBase, Cacheable):
def __repr__(self):
return "<lastfm.Group: %s>" % self.name
from api import Api
from error import InvalidParametersError
from weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart
from lastfm.api import Api
from lastfm.error import InvalidParametersError
from lastfm.weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart

13
lastfm/mixins/__init__.py Normal file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from lastfm.mixins.cacheable import Cacheable
from lastfm.mixins.searchable import Searchable
from lastfm.mixins.sharable import Sharable
from lastfm.mixins.shoutable import Shoutable
from lastfm.mixins.taggable import Taggable
__all__ = ['Cacheable', 'Searchable', 'Sharable', 'Shoutable', 'Taggable']

View File

@ -3,7 +3,8 @@
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from lazylist import lazylist
from lastfm.lazylist import lazylist
class Searchable(object):
@classmethod
@ -12,7 +13,7 @@ class Searchable(object):
search_item,
limit = None,
**kwds):
from api import Api
from lastfm.api import Api
cls_name = cls.__name__.lower()
params = {
'method': '%s.search'%cls_name,

View File

@ -9,7 +9,7 @@ class Sharable(object):
self._api = api
def share(self, recipient, message = None):
from user import User
from lastfm.user import User
params = self._default_params({'method': '%s.share' % self.__class__.__name__.lower()})
if message is not None:
params['message'] = message

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from lastfm.base import LastfmBase
class Shoutable(object):
def init(self, api):
self._api = api
@LastfmBase.cached_property
def shouts(self):
"""shouts for this ssubject"""
from lastfm.shout import Shout
from lastfm.user import User
params = self._default_params({'method': '%s.getShouts' % self.__class__.__name__.lower()})
data = self._api._fetch_data(params).find('shouts')
return [
Shout(
body = s.findtext('body'),
author = User(self._api, name = s.findtext('author')),
date = s.findtext('date') and s.findtext('date').strip() and \
datetime(*(time.strptime(s.findtext('date').strip(), '%a, %d %b %Y %H:%M:%S')[0:6]))
)
for s in data.findall('shout')
]
@LastfmBase.top_property("shouts")
def recent_shout(self):
"""recent shout for this subject"""
pass
def _default_params(self, extra_params = {}):
return extra_params
from datetime import datetime
import time

View File

@ -4,8 +4,8 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from safelist import SafeList
from lastfm.base import LastfmBase
from lastfm.safelist import SafeList
class Taggable(object):
def init(self, api):
@ -13,7 +13,7 @@ class Taggable(object):
@LastfmBase.cached_property
def tags(self):
from tag import Tag
from lastfm.tag import Tag
params = self._default_params({'method': '%s.getTags' % self.__class__.__name__.lower()})
data = self._api._fetch_data(params, sign = True, session = True, no_cache = True).find('tags')
return SafeList([
@ -27,7 +27,7 @@ class Taggable(object):
self.add_tags, self.remove_tag)
def add_tags(self, tags):
from tag import Tag
from lastfm.tag import Tag
while(len(tags) > 10):
section = tags[0:9]
tags = tags[9:]
@ -50,7 +50,7 @@ class Taggable(object):
self._tags = None
def remove_tag(self, tag):
from tag import Tag
from lastfm.tag import Tag
if isinstance(tag, Tag):
tag = tag.name

View File

@ -4,18 +4,18 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from album import Album
from artist import Artist
from cacheable import Cacheable
from error import InvalidParametersError
from event import Event
from geo import Location, Country
from group import Group
from playlist import Playlist
from tag import Tag
from track import Track
from user import User
from weeklychart import WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart
from lastfm.album import Album
from lastfm.artist import Artist
from lastfm.mixins import Cacheable
from lastfm.error import InvalidParametersError
from lastfm.event import Event
from lastfm.geo import Location, Country
from lastfm.group import Group
from lastfm.playlist import Playlist
from lastfm.tag import Tag
from lastfm.track import Track
from lastfm.user import User
from lastfm.weeklychart import WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart
class ObjectCache(object):
"""The registry to contain all the entities"""

View File

@ -4,8 +4,8 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable
class Playlist(LastfmBase, Cacheable):
"""A class representing an XPSF playlist."""
@ -52,7 +52,7 @@ class Playlist(LastfmBase, Cacheable):
import StringIO
import sys
from error import InvalidParametersError
from lastfm.error import InvalidParametersError
if sys.version_info >= (2, 5):
import xml.etree.cElementTree as ElementTree

62
lastfm/shout.py Normal file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable
class Shout(LastfmBase, Cacheable):
""""A class representing a shout."""
def init(self,
body = None,
author = None,
date = None):
self._body = body
self._author = author
self._date = date
@LastfmBase.cached_property
def body(self):
return self._body
@LastfmBase.cached_property
def author(self):
return self._author
@LastfmBase.cached_property
def date(self):
return self._date
@staticmethod
def _hash_func(*args, **kwds):
try:
return hash("%s%s" % (kwds['body'], kwds['author']))
except KeyError:
raise InvalidParametersError("body and author have to be provided for hashing")
def __hash__(self):
return self.__class__._hash_func(body = self.body, author = self.author)
def __eq__(self, other):
return (
self.body == other.body and
self.author == other.author
)
def __lt__(self, other):
if self.author != other.author:
return self.author < other.author
else:
if self.date != other.date:
return self.date < other.date
else:
return self.body < other.body
def __repr__(self):
return "<lastfm.Shout: '%s' by %s>" % (self.body, self.author.name)
from lastfm.error import InvalidParametersError

View File

@ -4,10 +4,9 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from searchable import Searchable
from cacheable import Cacheable
from lazylist import lazylist
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable, Searchable
from lastfm.lazylist import lazylist
class Tag(LastfmBase, Cacheable, Searchable):
""""A class representing a tag."""
@ -225,10 +224,10 @@ class Tag(LastfmBase, Cacheable, Searchable):
def __repr__(self):
return "<lastfm.Tag: %s>" % self.name
from album import Album
from api import Api
from artist import Artist
from error import InvalidParametersError
from playlist import Playlist
from stats import Stats
from track import Track
from lastfm.album import Album
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.error import InvalidParametersError
from lastfm.playlist import Playlist
from lastfm.stats import Stats
from lastfm.track import Track

View File

@ -63,4 +63,4 @@ class Tasteometer(object):
def __repr__(self):
return "<lastfm.Tasteometer: %s%% match>" % (self.score*100)
from artist import Artist
from lastfm.artist import Artist

View File

@ -4,12 +4,9 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from taggable import Taggable
from sharable import Sharable
from searchable import Searchable
from lazylist import lazylist
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable, Searchable, Sharable, Taggable
from lastfm.lazylist import lazylist
class Track(LastfmBase, Cacheable, Sharable, Searchable, Taggable):
"""A class representing a track."""
@ -390,11 +387,11 @@ class Track(LastfmBase, Cacheable, Sharable, Searchable, Taggable):
import time
from datetime import datetime
from api import Api
from artist import Artist
from album import Album
from error import InvalidParametersError
from stats import Stats
from tag import Tag
from user import User
from wiki import Wiki
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.album import Album
from lastfm.error import InvalidParametersError
from lastfm.stats import Stats
from lastfm.tag import Tag
from lastfm.user import User
from lastfm.wiki import Wiki

View File

@ -4,12 +4,12 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from lazylist import lazylist
import playlist
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable, Shoutable
from lastfm.lazylist import lazylist
import lastfm.playlist
class User(LastfmBase, Cacheable):
class User(LastfmBase, Cacheable, Shoutable):
"""A class representing an user."""
def init(self,
api,
@ -24,6 +24,7 @@ class User(LastfmBase, Cacheable):
subscriber = None):
if not isinstance(api, Api):
raise InvalidParametersError("api reference must be supplied as an argument")
Shoutable.init(self, api)
self._api = api
self._name = name
self._url = url
@ -559,7 +560,7 @@ class User(LastfmBase, Cacheable):
def __repr__(self):
return "<lastfm.User: %s>" % self.name
class Playlist(playlist.Playlist):
class Playlist(lastfm.playlist.Playlist):
"""A class representing a playlist belonging to the user."""
def init(self, api, id, title, date, size, creator):
super(User.Playlist, self).init(api, "lastfm://playlist/%s" % id)
@ -796,14 +797,14 @@ class User(LastfmBase, Cacheable):
from datetime import datetime
import time
from api import Api
from artist import Artist
from album import Album
from error import LastfmError, InvalidParametersError
from event import Event
from geo import Country
from stats import Stats
from tag import Tag
from tasteometer import Tasteometer
from track import Track
from weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.album import Album
from lastfm.error import LastfmError, InvalidParametersError
from lastfm.event import Event
from lastfm.geo import Country
from lastfm.stats import Stats
from lastfm.tag import Tag
from lastfm.tasteometer import Tasteometer
from lastfm.track import Track
from lastfm.weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart

View File

@ -4,8 +4,8 @@ __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from cacheable import Cacheable
from lastfm.base import LastfmBase
from lastfm.mixins import Cacheable
class WeeklyChart(LastfmBase, Cacheable):
"""A class for representing the weekly charts"""
@ -257,8 +257,8 @@ class WeeklyTrackChart(WeeklyChart):
from datetime import datetime
import calendar
from album import Album
from artist import Artist
from error import InvalidParametersError
from stats import Stats
from track import Track
from lastfm.album import Album
from lastfm.artist import Artist
from lastfm.error import InvalidParametersError
from lastfm.stats import Stats
from lastfm.track import Track

View File

@ -15,7 +15,6 @@ located at http://ws.audioscrobbler.com/2.0/ .""",
maintainer="Abhinav Sarkar",
maintainer_email="abhinav.sarkar@gmail.com",
url="http://python-lastfm.googlecode.com/svn/trunk/dist/",
package_dir = {'lastfm':'src'},
packages=['lastfm'],
license="GNU Lesser General Public License",
keywords="audioscrobbler webservice api last.fm",

View File

@ -1,23 +0,0 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from album import Album
from api import Api
from artist import Artist
from error import LastfmError
from event import Event
from geo import Location, Country
from group import Group
from playlist import Playlist
from objectcache import ObjectCache
from tag import Tag
from tasteometer import Tasteometer
from track import Track
from user import User
__all__ = ['LastfmError', 'Api', 'Album', 'Artist', 'Event',
'Location', 'Country', 'Group', 'Playlist', 'Tag',
'Tasteometer', 'Track', 'User', 'ObjectCache']