2008-07-10 22:25:05 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2008-08-25 17:14:48 +05:30
|
|
|
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
|
2008-09-02 22:06:16 +05:30
|
|
|
__version__ = "0.2"
|
2008-07-10 22:25:05 +05:30
|
|
|
__license__ = "GNU Lesser General Public License"
|
2009-03-17 20:51:40 +05:30
|
|
|
__package__ = "lastfm"
|
2008-07-10 22:25:05 +05:30
|
|
|
|
2008-12-31 11:00:23 +05:30
|
|
|
from lastfm.base import LastfmBase
|
2009-04-18 10:41:22 +05:30
|
|
|
from lastfm.mixin import mixin
|
2009-03-10 11:39:11 +05:30
|
|
|
from lastfm.decorators import cached_property
|
2008-07-15 00:38:39 +05:30
|
|
|
|
2009-04-18 10:41:22 +05:30
|
|
|
@mixin("cacheable", "property_adder")
|
2009-03-31 09:09:23 +05:30
|
|
|
class Playlist(LastfmBase):
|
2008-07-10 22:25:05 +05:30
|
|
|
"""A class representing an XPSF playlist."""
|
2009-04-18 10:41:22 +05:30
|
|
|
|
|
|
|
class Meta(object):
|
|
|
|
properties = ["url"]
|
|
|
|
|
2009-03-10 11:39:11 +05:30
|
|
|
def init(self, api, url, **kwargs):
|
2008-12-30 19:21:04 +05:30
|
|
|
self._api = api
|
|
|
|
self._data = None
|
|
|
|
self._url = url
|
2008-07-15 00:38:39 +05:30
|
|
|
|
2009-03-10 11:39:11 +05:30
|
|
|
@cached_property
|
2008-07-31 16:04:46 +05:30
|
|
|
def data(self):
|
|
|
|
"""playlist's data"""
|
2008-12-30 19:21:04 +05:30
|
|
|
params = {'method': 'playlist.fetch', 'playlistURL': self._url}
|
2008-09-02 19:49:03 +05:30
|
|
|
tmp = StringIO.StringIO()
|
2008-12-30 19:21:04 +05:30
|
|
|
ElementTree.ElementTree(self._api._fetch_data(params)[0]).write(tmp)
|
2008-09-02 19:49:03 +05:30
|
|
|
return tmp.getvalue()
|
2008-07-17 22:17:32 +05:30
|
|
|
|
2008-07-10 22:25:05 +05:30
|
|
|
@staticmethod
|
2008-07-17 22:17:32 +05:30
|
|
|
def fetch(api, url):
|
2008-09-02 19:49:03 +05:30
|
|
|
return Playlist(api, url = url)
|
2008-07-15 00:38:39 +05:30
|
|
|
|
|
|
|
@staticmethod
|
2008-12-30 19:21:04 +05:30
|
|
|
def _hash_func(*args, **kwds):
|
2008-07-15 00:38:39 +05:30
|
|
|
try:
|
2008-07-17 22:17:32 +05:30
|
|
|
return hash(kwds['url'])
|
2008-07-15 00:38:39 +05:30
|
|
|
except KeyError:
|
2008-12-30 15:57:13 +05:30
|
|
|
raise InvalidParametersError("url has to be provided for hashing")
|
2008-07-15 00:38:39 +05:30
|
|
|
|
|
|
|
def __hash__(self):
|
2008-12-30 19:21:04 +05:30
|
|
|
return self.__class__._hash_func(url = self.url)
|
2008-07-15 00:38:39 +05:30
|
|
|
|
|
|
|
def __eq__(self, other):
|
2008-07-17 22:17:32 +05:30
|
|
|
return self.url == other.url
|
2008-07-15 00:38:39 +05:30
|
|
|
|
|
|
|
def __lt__(self, other):
|
2008-07-17 22:17:32 +05:30
|
|
|
return self.url < other.url
|
2008-07-15 00:38:39 +05:30
|
|
|
|
|
|
|
def __repr__(self):
|
2008-07-17 22:17:32 +05:30
|
|
|
return "<lastfm.Playlist: %s>" % self.url
|
2008-09-02 19:49:03 +05:30
|
|
|
|
|
|
|
import StringIO
|
2008-12-30 19:21:04 +05:30
|
|
|
import sys
|
2008-12-31 11:00:23 +05:30
|
|
|
from lastfm.error import InvalidParametersError
|
2008-09-02 19:49:03 +05:30
|
|
|
|
2008-12-30 19:21:04 +05:30
|
|
|
if sys.version_info >= (2, 5):
|
2008-09-02 19:49:03 +05:30
|
|
|
import xml.etree.cElementTree as ElementTree
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
import cElementTree as ElementTree
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import ElementTree
|
|
|
|
except ImportError:
|
2008-12-30 19:44:00 +05:30
|
|
|
from error import LastfmError
|
|
|
|
raise LastfmError("Install ElementTree package for using python-lastfm")
|