refactored out share API methods in sharable module. applied the module to artist, event and track.

master
Abhinav Sarkar 2008-10-03 12:14:31 +00:00
parent fc831421b7
commit e1a9541f7d
6 changed files with 44 additions and 18 deletions

View File

@ -22,7 +22,7 @@ class Album(Taggable, LastfmBase):
topTags = None):
if not isinstance(api, Api):
raise LastfmInvalidParametersError("api reference must be supplied as an argument")
super(self.__class__, self).init(api)
Taggable.init(self, api)
self.__api = api
self.__name = name
self.__artist = artist

View File

@ -6,9 +6,10 @@ __license__ = "GNU Lesser General Public License"
from base import LastfmBase
from taggable import Taggable
from sharable import Sharable
from lazylist import lazylist
class Artist(Taggable, LastfmBase):
class Artist(LastfmBase, Taggable, Sharable):
"""A class representing an artist."""
def init(self,
api,
@ -23,7 +24,8 @@ class Artist(Taggable, LastfmBase):
bio = None):
if not isinstance(api, Api):
raise LastfmInvalidParametersError("api reference must be supplied as an argument")
super(self.__class__, self).init(api)
Taggable.init(self, api)
Sharable.init(self, api)
self.__api = api
self.__name = name
self.__mbid = mbid

View File

@ -5,8 +5,9 @@ __version__ = "0.2"
__license__ = "GNU Lesser General Public License"
from base import LastfmBase
from sharable import Sharable
class Event(LastfmBase):
class Event(LastfmBase, Sharable):
"""A class representing an event."""
def init(self,
api,
@ -24,6 +25,7 @@ class Event(LastfmBase):
tag = None):
if not isinstance(api, Api):
raise LastfmInvalidParametersError("api reference must be supplied as an argument")
Sharable.init(self, api)
self.__api = api
self.__id = id
self.__title = title
@ -95,6 +97,14 @@ class Event(LastfmBase):
def tag(self):
"""tags for the event"""
return self.__tag
def _defaultParams(self, extraParams = None):
if not self.id:
raise LastfmInvalidParametersError("id has to be provided.")
params = {'event': self.id}
if extraParams is not None:
params.update(extraParams)
return params
@staticmethod
def getInfo(api, event):

24
src/sharable.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
class Sharable(object):
def init(self, api):
self.__api = api
def share(self, recipient, message = None):
from user import User
params = self._defaultParams({'method': '%s.share' % self.__class__.__name__.lower()})
if message is not None:
params['message'] = message
if not isinstance(recipient, list):
recipient = [recipient]
for i in xrange(len(recipient)):
if isinstance(recipient[i], User):
recipient[i] = recipient[i].name
params['recipient'] = ",".join(recipient)
self.__api._postData(params)

View File

@ -14,7 +14,6 @@ class Taggable(object):
@LastfmBase.cachedProperty
def tags(self):
from tag import Tag
params = self._defaultParams({'method': '%s.getTags' % self.__class__.__name__.lower()})
data = self.__api._fetchData(params, sign = True, session = True, no_cache = True).find('tags')
return SafeList([

View File

@ -6,9 +6,10 @@ __license__ = "GNU Lesser General Public License"
from base import LastfmBase
from taggable import Taggable
from sharable import Sharable
from lazylist import lazylist
class Track(LastfmBase, Taggable):
class Track(LastfmBase, Taggable, Sharable):
"""A class representing a track."""
def init(self,
api,
@ -28,7 +29,8 @@ class Track(LastfmBase, Taggable):
wiki = None):
if not isinstance(api, Api):
raise LastfmInvalidParametersError("api reference must be supplied as an argument")
super(self.__class__, self).init(api)
Taggable.init(self, api)
Sharable.init(self, api)
self.__api = api
self.__id = id
self.__name = name
@ -257,17 +259,6 @@ class Track(LastfmBase, Taggable):
def ban(self):
params = self._defaultParams({'method': 'track.ban'})
self.__api._postData(params)
def share(self, recipient, message = None):
params = self._defaultParams({'method': 'track.share'})
if message is not None:
params['message'] = message
for i in xrange(len(recipient)):
if isinstance(recipient[i], User):
recipient[i] = recipient[i].name
params['recipient'] = ",".join(recipient)
self.__api._postData(params)
@staticmethod
def search(api,