musixmatch-master/build/lib/musixmatch/track.py
Go to the documentation of this file.
1 """
2 This module contains higher level classes to query Musixmatch API and build
3 simple dictionary-like objects representing a Track or a TracksCollection.
4 
5 >>> from musixmatch.track import Track, TracksCollection
6 >>> import musixmatch.api
7 >>>
8 >>> try:
9 ... track = Track(track_mbid=8976)
10 ... collection = TracksCollection.fromChart(country='us', page=1)
11 ... except musixmatch.api.Error, e:
12 ... pass
13 """
14 import musixmatch
15 __license__ = musixmatch.__license__
16 __author__ = musixmatch.__author__
17 
18 from musixmatch import api, lyrics, subtitle
19 from musixmatch.base import Item, ItemsCollection
20 from musixmatch.ws import track, matcher, album
21 
22 _marker=object()
23 
24 class Track(Item):
25  """
26  This class builds a :py:class:`dict` like object representing a track. It
27  can get track information through the :py:class:`musixmatch.api.Method`
28  **track.get** or from an already well-formed :py:class:`dict`. Create a
29  Track object based on a given keyword argument:
30 
31  :param track_id: musiXmatch track ID
32  :param musicbrainz_id: Musicbrainz track ID
33  :param track_echonest_id: Echonest track ID
34  :param track_data: an already well-formed :py:class:`dict` of track data
35  :raises: :py:exc:`musixmatch.api.Error` if :py:class:`musixmatch.api.ResponseStatusCode` is not 200
36 
37  Once information are collected, the following keys are available:
38 
39  :keyword track_id: musiXmatch track ID
40  :keyword track_mbid: Musicbrainz track ID
41  :keyword lyrics_id: musiXmatch lyrics ID
42  :keyword instrumental: wether the track is instrumental or not
43  :keyword subtitle_id: musiXmatch subtitle ID
44  :keyword track_name: track name
45  :keyword album_coverart_100x100: album cover URL
46  :keyword artist_id: musiXmatch artist ID
47  :keyword artist_mbid: Musicbrainz artist ID
48  :keyword artist_name: artist name
49 
50  Keyword access have been overloaded thanks to the :py:meth:`get` method
51  which will eventually fetch the matching lyrics or subtitle.
52  """
53  __api_method__ = track.get
54 
55  @classmethod
56  def fromMatcher(cls, **keywords):
57  """
58  Returns a :py:class:`Track` based on the result of the
59  :py:class:`musiXmatch.api.Method` **matcher.track.get**. Accepts the
60  following keywords:
61 
62  :param q_track: words to be searched among track titles
63  :param q_artist: words to be searched among artist names
64  """
65  return cls.fromResponseMessage(matcher.track.get(**keywords))
66 
67  def get(self, key, default=_marker):
68  """
69  If key is *lyrics* or *subtitle* try to query api for proper value,
70  and build an :py:class:`musixmatch.lyrics.Lyrics` or
71  :py:class:`musixmatch.subtitle.Subtitle`. Access to the above mentioned
72  keys may fail with :py:exc:`musixmatch.api.Error`. Once fetched, the
73  result is saved.
74  """
75  special = {
76  'lyrics': lyrics.Lyrics,
77  'subtitle': subtitle.Subtitle,
78  }
79  if key in special and not key in self:
80  self[key] = special[key](track_id=self['track_id'])
81  value = dict.get(self, key, default)
82  if value == _marker:
83  raise KeyError, key
84  return value
85 
86  def __getitem__(self, key):
87  return self.get(key)
88 
89  def postFeedback(self, feedback):
90  """
91  Post feedback about lyrics for this track. **feedback** can be one of:
92 
93  :keyword wrong_attribution: the lyrics shown are not by the artist that
94  I selected.
95  :keyword bad_characters: there are strange characters and/or words
96  that are partially scrambled.
97  :keyword lines_too_long: the text for each verse is too long!
98  :keyword wrong_verses: there are some verses missing from the
99  beginning or at the end.
100  :keyword wrong_formatting: the text looks horrible, please fix it!
101  """
102  accepted = [
103  'wrong_attribution', 'bad_characters', 'lines_too_long',
104  'wrong_verses', 'wrong_formatting' ]
105  if feedback in accepted:
106  message = track.lyrics.feedback.post(
107  track_id=self['track_id'],
108  lyrics_id=self['track_id']['lyrics']['lyrics_id'],
109  feedback=feedback
110  )
111  if not message.status_code:
112  raise api.Error(str(message.status_code))
113  else:
114  raise TypeError, '%r not in %r' % (feedback, accepted)
115 
117  """
118  This class build a :py:class:`list` like object representing a tracks
119  collection. It accepts :py:class:`dict` or :py:class:`Track` objects.
120  """
121  __allowedin__ = Track
122 
123  @classmethod
124  def fromAlbum(cls, **keywords):
125  """
126  This classmethod builds an :py:class:`TracksCollection` from a
127  **album.tracks.get** :py:class:`musixmatch.api.Method` call.
128 
129  :param album_id: musiXmatch album ID
130  """
131  return cls.fromResponseMessage(album.tracks.get(**keywords))
132 
133  @classmethod
134  def fromSearch(cls, **keywords):
135  """
136  This classmethod builds an :py:class:`TracksCollection` from a
137  **track.search** :py:class:`musixmatch.api.Method` call.
138 
139  :param q: a string that will be searched in every data field
140  (q_track, q_artist, q_lyrics)
141  :param q_track: words to be searched among track titles
142  :param q_artist: words to be searched among artist names
143  :param q_track_artist: words to be searched among track titles or
144  artist names
145  :param q_lyrics: words to be searched into the lyrics
146  :param page: requested page of results
147  :param page_size: desired number of items per result page
148  :param f_has_lyrics: exclude tracks without an available lyrics
149  (automatic if q_lyrics is set)
150  :param f_artist_id: filter the results by the artist_id
151  :param f_artist_mbid: filter the results by the artist_mbid
152  :param quorum_factor: only works together with q and q_track_artist
153  parameter. Possible values goes from 0.1 to
154  0.9. A value of 0.9 means: 'match at least 90
155  percent of the words'.
156  """
157  return cls.fromResponseMessage(track.search(**keywords))
158 
159  @classmethod
160  def fromChart(cls, **keywords):
161  """
162  This classmethod builds an :py:class:`TracksCollection` from a
163  **track.chart.get** :py:class:`musixmatch.api.Method` call.
164 
165  :param page: requested page of results
166  :param page_size: desired number of items per result page
167  :param country: the country code of the desired country chart
168  :param f_has_lyrics: exclude tracks without an available lyrics
169  (automatic if q_lyrics is set)
170  """
171  return cls.fromResponseMessage(track.chart.get(**keywords))