python-lastfm/lastfm/playlist.py

69 lines
1.9 KiB
Python
Raw Normal View History

2008-07-10 22:25:05 +05:30
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
2008-07-10 22:25:05 +05:30
__license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
2008-07-10 22:25:05 +05:30
from lastfm.base import LastfmBase
from lastfm.mixin import mixin
from lastfm.decorators import cached_property
2008-07-15 00:38:39 +05:30
@mixin("cacheable", "property_adder")
class Playlist(LastfmBase):
2008-07-10 22:25:05 +05:30
"""A class representing an XPSF playlist."""
class Meta(object):
properties = ["url"]
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
@cached_property
def data(self):
"""playlist's data"""
2008-12-30 19:21:04 +05:30
params = {'method': 'playlist.fetch', 'playlistURL': self._url}
tmp = StringIO.StringIO()
2008-12-30 19:21:04 +05:30
ElementTree.ElementTree(self._api._fetch_data(params)[0]).write(tmp)
return tmp.getvalue()
2008-07-10 22:25:05 +05:30
@staticmethod
def fetch(api, url):
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:
return hash(kwds['url'])
2008-07-15 00:38:39 +05:30
except KeyError:
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):
return self.url == other.url
2008-07-15 00:38:39 +05:30
def __lt__(self, other):
return self.url < other.url
2008-07-15 00:38:39 +05:30
def __repr__(self):
return "<lastfm.Playlist: %s>" % self.url
import StringIO
2008-12-30 19:21:04 +05:30
import sys
from lastfm.error import InvalidParametersError
2008-12-30 19:21:04 +05:30
if sys.version_info >= (2, 5):
import xml.etree.cElementTree as ElementTree
else:
try:
import cElementTree as ElementTree
except ImportError:
try:
import ElementTree
except ImportError:
from error import LastfmError
raise LastfmError("Install ElementTree package for using python-lastfm")