python-lastfm/src/track.py

282 lines
10 KiB
Python
Raw Normal View History

2008-07-10 22:25:05 +05:30
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
2008-07-10 22:25:05 +05:30
__version__ = "0.1"
__license__ = "GNU Lesser General Public License"
2008-07-15 00:38:39 +05:30
from base import LastfmBase
class Track(LastfmBase):
2008-07-10 22:25:05 +05:30
"""A class representing a track."""
2008-07-15 00:38:39 +05:30
def init(self,
2008-07-14 15:50:18 +05:30
api,
2008-07-10 22:25:05 +05:30
name = None,
mbid = None,
url = None,
streamable = None,
artist = None,
album = None,
2008-07-10 22:25:05 +05:30
image = None,
stats = None,
2008-08-18 21:42:46 +05:30
fullTrack = None,
playedOn = None,
lovedOn = None):
2008-07-16 18:36:42 +05:30
if not isinstance(api, Api):
raise LastfmError("api reference must be supplied as an argument")
2008-07-14 15:50:18 +05:30
self.__api = api
2008-07-10 22:25:05 +05:30
self.__name = name
self.__mbid = mbid
self.__url = url
self.__streamable = streamable
self.__artist = artist
self.__album = album
2008-07-10 22:25:05 +05:30
self.__image = image
self.__stats = stats and Stats(
subject = self,
match = stats.match,
playcount = stats.playcount,
rank = stats.rank,
listeners = stats.listeners,
)
2008-07-17 02:18:16 +05:30
self.__fullTrack = fullTrack
2008-08-18 21:42:46 +05:30
self.__playedOn = playedOn
self.__lovedOn = lovedOn
@property
def name(self):
"""name of the track"""
2008-07-10 22:25:05 +05:30
return self.__name
@property
def mbid(self):
"""mbid of the track"""
2008-07-10 22:25:05 +05:30
return self.__mbid
@property
def url(self):
"""url of the tracks's page"""
2008-07-10 22:25:05 +05:30
return self.__url
@property
def streamable(self):
"""is the track streamable"""
2008-07-10 22:25:05 +05:30
return self.__streamable
@property
def artist(self):
"""artist of the track"""
2008-07-10 22:25:05 +05:30
return self.__artist
@property
def album(self):
"""artist of the track"""
return self.__album
@property
def image(self):
"""image of the track's album cover"""
2008-07-10 22:25:05 +05:30
return self.__image
@property
def stats(self):
"""stats of the track"""
return self.__stats
2008-08-18 21:42:46 +05:30
@property
def fullTrack(self):
"""is the full track streamable"""
2008-07-17 02:18:16 +05:30
return self.__fullTrack
2008-08-18 21:42:46 +05:30
@property
def playedOn(self):
"""datetime the track was last played"""
return self.__playedOn
@property
def lovedOn(self):
"""datetime the track was marked 'loved'"""
return self.__lovedOn
2008-07-14 15:50:18 +05:30
def __checkParams(self,
params,
artist = None,
track = None,
mbid = None):
if not ((artist and track) or mbid):
raise LastfmError("either (artist and track) or mbid has to be given as argument.")
2008-08-18 21:42:46 +05:30
2008-07-16 18:36:42 +05:30
if artist and track:
2008-07-14 15:50:18 +05:30
params.update({'artist': artist, 'track': track})
elif mbid:
params.update({'mbid': mbid})
return params
2008-08-18 21:42:46 +05:30
@LastfmBase.cachedProperty
def similar(self):
"""tracks similar to this track"""
params = self.__checkParams(
{'method': 'track.getsimilar'},
self.artist.name,
self.name,
self.mbid
)
data = self.__api._fetchData(params).find('similartracks')
return [
Track(
self.__api,
name = t.findtext('name'),
artist = Artist(
self.__api,
name = t.findtext('artist/name'),
mbid = t.findtext('artist/mbid'),
url = t.findtext('artist/url')
),
mbid = t.findtext('mbid'),
stats = Stats(
subject = t.findtext('name'),
match = float(t.findtext('match'))
),
streamable = (t.findtext('streamable') == '1'),
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
)
for t in data.findall('track')
]
2008-08-18 21:42:46 +05:30
@LastfmBase.topProperty("similar")
def mostSimilar(self):
"""track most similar to this track"""
pass
2008-08-18 21:42:46 +05:30
@LastfmBase.cachedProperty
def topFans(self):
"""top fans of the track"""
params = self.__checkParams(
{'method': 'track.gettopfans'},
self.artist.name,
self.name,
self.mbid
)
data = self.__api._fetchData(params).find('topfans')
return [
User(
self.__api,
name = u.findtext('name'),
url = u.findtext('url'),
image = dict([(i.get('size'), i.text) for i in u.findall('image')]),
stats = Stats(
subject = u.findtext('name'),
weight = int(u.findtext('weight'))
)
)
for u in data.findall('user')
]
2008-08-18 21:42:46 +05:30
@LastfmBase.topProperty("topFans")
def topFan(self):
"""topmost fan of the track"""
pass
2008-08-18 21:42:46 +05:30
@LastfmBase.cachedProperty
def topTags(self):
"""top tags for the track"""
params = self.__checkParams(
{'method': 'track.gettoptags'},
self.artist.name,
self.name,
self.mbid
)
data = self.__api._fetchData(params).find('toptags')
return [
Tag(
self.__api,
name = t.findtext('name'),
url = t.findtext('url'),
stats = Stats(
subject = t.findtext('name'),
count = int(t.findtext('count')),
)
)
for t in data.findall('tag')
]
2008-08-18 21:42:46 +05:30
@LastfmBase.topProperty("topTags")
def topTag(self):
"""topmost tag for the track"""
pass
2008-08-18 21:42:46 +05:30
2008-07-14 15:50:18 +05:30
@staticmethod
def search(api,
track,
artist = None,
limit = None,
page = None):
params = {'method': 'track.search', 'track': track}
if artist is not None:
params.update({'artist': artist})
if limit is not None:
params.update({'limit': limit})
if page is not None:
params.update({'page': page})
data = api._fetchData(params).find('results')
return SearchResult(
type = 'track',
searchTerms = data.find("{%s}Query" % SearchResult.xmlns).attrib['searchTerms'],
startPage = int(data.find("{%s}Query" % SearchResult.xmlns).attrib['startPage']),
totalResults = int(data.findtext("{%s}totalResults" % SearchResult.xmlns)),
startIndex = int(data.findtext("{%s}startIndex" % SearchResult.xmlns)),
itemsPerPage = int(data.findtext("{%s}itemsPerPage" % SearchResult.xmlns)),
matches = [
Track(
api,
name = t.findtext('name'),
artist = Artist(
api,
name = t.findtext('artist')
),
url = t.findtext('url'),
stats = Stats(
subject = t.findtext('name'),
listeners = int(t.findtext('listeners'))
),
streamable = (t.findtext('streamable') == '1'),
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
)
for t in data.findall('trackmatches/track')
]
)
2008-08-18 21:42:46 +05:30
2008-07-15 00:38:39 +05:30
@staticmethod
def hashFunc(*args, **kwds):
try:
return hash("%s%s" % (kwds['name'], hash(kwds['artist'])))
except KeyError:
raise LastfmError("name and artist have to be provided for hashing")
2008-08-18 21:42:46 +05:30
2008-07-15 00:38:39 +05:30
def __hash__(self):
return self.__class__.hashFunc(name = self.name, artist = self.artist)
2008-08-18 21:42:46 +05:30
2008-07-15 00:38:39 +05:30
def __eq__(self, other):
if self.mbid and other.mbid:
return self.mbid == other.mbid
if self.url and other.url:
return self.url == other.url
if (self.name and self.artist) and (other.name and other.artist):
return (self.name == other.name) and (self.artist == other.artist)
return super(Track, self).__eq__(other)
2008-08-18 21:42:46 +05:30
2008-07-15 00:38:39 +05:30
def __lt__(self, other):
return self.name < other.name
2008-08-18 21:42:46 +05:30
2008-07-15 00:38:39 +05:30
def __repr__(self):
2008-07-16 18:36:42 +05:30
return "<lastfm.Track: '%s' by %s>" % (self.name, self.artist.name)
from api import Api
from artist import Artist
2008-07-14 15:50:18 +05:30
from error import LastfmError
from search import SearchResult
from stats import Stats
from tag import Tag
2008-08-18 21:42:46 +05:30
from user import User