Esempio n. 1
0
 def top_tracks(self):
     """top tracks for the tag"""
     params = self._default_params({'method': 'tag.getTopTracks'})
     data = self._api._fetch_data(params).find('toptracks')
     return [
         Track(
             self._api,
             subject=self,
             name=t.findtext('name'),
             artist=Artist(
                 self._api,
                 subject=self,
                 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'),
                 rank=t.attrib['rank'].strip() and int(t.attrib['rank'])
                 or None,
                 tagcount=t.findtext('tagcount')
                 and int(t.findtext('tagcount')) or None),
             streamable=(t.findtext('streamable') == '1'),
             full_track=(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')
     ]
Esempio n. 2
0
 def get_top_tracks(self, period = None):
     params = self._default_params({'method': 'user.getTopTracks'})
     if period is not None:
         params.update({'period': period})
     data = self._api._fetch_data(params).find('toptracks')
     return [
             Track(
                   self._api,
                   subject = self,
                   name = t.findtext('name'),
                   artist = Artist(
                                   self._api,
                                   subject = self,
                                   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'),
                                 rank = t.attrib['rank'].strip() and int(t.attrib['rank']) or None,
                                 playcount = t.findtext('playcount') and int(t.findtext('playcount')) or None
                                 ),
                   streamable = (t.findtext('streamable') == '1'),
                   full_track = (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')
             ]
Esempio n. 3
0
 def create_from_data(api, subject, data):
     w = WeeklyChart(
         subject=subject,
         start=datetime.utcfromtimestamp(int(data.attrib['from'])),
         end=datetime.utcfromtimestamp(int(data.attrib['to'])),
     )
     return WeeklyTrackChart(
         subject=subject,
         start=datetime.utcfromtimestamp(int(data.attrib['from'])),
         end=datetime.utcfromtimestamp(int(data.attrib['to'])),
         stats=Stats(subject=subject,
                     playcount=reduce(
                         lambda x, y: (x + int(y.findtext('playcount'))),
                         data.findall('track'), 0)),
         tracks=[
             Track(
                 api,
                 subject=w,
                 name=t.findtext('name'),
                 mbid=t.findtext('mbid'),
                 artist=Artist(
                     api,
                     name=t.findtext('artist'),
                     mbid=t.find('artist').attrib['mbid'],
                 ),
                 stats=Stats(
                     subject=t.findtext('name'),
                     rank=int(t.attrib['rank']),
                     playcount=int(t.findtext('playcount')),
                 ),
                 url=t.findtext('url'),
             ) for t in data.findall('track')
         ])
Esempio n. 4
0
 def loved_tracks(self):
     params = self._default_params({'method': 'user.getLovedTracks'})
     data = self._api._fetch_data(params).find('lovedtracks')
     return [
             Track(
                 self._api,
                 subject = self,
                 name = t.findtext('name'),
                 artist = Artist(
                     self._api,
                     subject = self,
                     name = t.findtext('artist/name'),
                     mbid = t.findtext('artist/mbid'),
                     url = t.findtext('artist/url'),
                 ),
                 mbid = t.findtext('mbid'),
                 image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
                 loved_on = datetime(*(
                     time.strptime(
                         t.findtext('date').strip(),
                         '%d %b %Y, %H:%M'
                         )[0:6])
                     )
                 )
             for t in data.findall('track')
             ]
Esempio n. 5
0
 def get_recent_tracks(self,
                       limit=None,
                       timefrom=None,
                       timeto=None,
                       page=None):
     params = self._default_params({'method': 'user.getRecentTracks'})
     if limit is not None:
         params.update({'limit': limit})
     if timefrom is not None:
         params.update({'from': timefrom})
     if timeto is not None:
         params.update({'to': timeto})
     if page is not None:
         params.update({'page': page})
     data = self._api._fetch_data(params,
                                  no_cache=True).find('recenttracks')
     total_pages = int(data.attrib['totalPages'])
     yield total_pages
     for t in data.findall('track'):
         track = Track(
             self._api,
             subject=self,
             name=t.findtext('name'),
             artist=Artist(
                 self._api,
                 subject=self,
                 name=t.findtext('artist'),
                 mbid=t.find('artist').attrib['mbid'],
             ),
             album=Album(
                 self._api,
                 subject=self,
                 name=t.findtext('album'),
                 artist=Artist(
                     self._api,
                     subject=self,
                     name=t.findtext('artist'),
                     mbid=t.find('artist').attrib['mbid'],
                 ),
                 mbid=t.find('album').attrib['mbid'],
             ),
             mbid=t.findtext('mbid'),
             streamable=(t.findtext('streamable') == '1'),
             url=t.findtext('url'),
             image=dict([(i.get('size'), i.text)
                         for i in t.findall('image')]),
             played_on=datetime(*(time.strptime(
                 t.findtext('date').strip(), '%d %b %Y, %H:%M')[0:6]))
             if t.findtext('date') else datetime(
                 *datetime.now().timetuple()[0:6]),
             bypass_registry=True)
         if 'nowplaying' in t.attrib and t.attrib['nowplaying'] == 'true':
             self._now_playing = track
         yield track
Esempio n. 6
0
 def get_recent_tracks(self, limit=None, page=1, fr=None, to=None): #from 1/1/2012 to 1/1/2014 #  to = 1388534400, fr = 1325376000
     params = self._default_params({'method': 'user.getRecentTracks'})
     if limit is not None:
         params.update({'limit': limit})
         params.update({'page': page})
     if fr is not None:
         params.update({'from': fr}) # set time window
     if to is not None:
         params.update({'to': to}) # set time window
        # params.update({'extended': 1})
     data = self._api._fetch_data(params, no_cache=True).find('recenttracks')
     return [
             Track(
                   self._api,
                   subject = self,
                   name = t.findtext('name'),
                   artist = Artist(
                                   self._api,
                                   subject = self,
                                   name = t.findtext('artist'),
                                   mbid = t.find('artist').attrib['mbid'],
                                   ),
                   album = Album(
                                 self._api,
                                 subject = self,
                                 name = t.findtext('album'),
                                 artist = Artist(
                                                 self._api,
                                                 subject = self,
                                                 name = t.findtext('artist'),
                                                 mbid = t.find('artist').attrib['mbid'],
                                                 ),
                                 mbid = t.find('album').attrib['mbid'],
                                 ),
                   mbid = t.findtext('mbid'),
                   streamable = (t.findtext('streamable') == '1'),
                   url = t.findtext('url'),
                   image = dict([(i.get('size'), i.text) for i in t.findall('image')]),
                   played_on = datetime(*(
                                        time.strptime(
                                                      t.findtext('date').strip(),
                                                      '%d %b %Y, %H:%M'
                                                      )[0:6])
                                        ).replace(
                                        tzinfo = UTC
                                        ) if t.findtext('date') else datetime(*datetime.utcnow().timetuple()[0:6]).replace(tzinfo=UTC),
                   now_playing = True if 'nowplaying' in t.attrib and t.attrib['nowplaying'] == 'true' else False
                   )
                   for t in data.findall('track')
                   ]
Esempio n. 7
0
    def get_top_tracks(api, country, location=None):
        """
        Get the most popular tracks on Last.fm by country
        
        @param api:          an instance of L{Api}
        @type api:           L{Api}
        @param country:      a country name, as defined by 
                             the ISO 3166-1 country names standard
        @type country:       L{str}
        @param location:     a metro name, to fetch the charts for 
                            (must be within the country specified) (optional)
        
        @return:             most popular tracks of the country
        @rtype:              L{list} of L{Track}
        
        @note: Use L{Country.top_tracks} and L{Country.get_top_tracks}
               instead of using this method directly.
        """
        params = {'method': 'geo.getTopTracks', 'country': country}
        if location is not None:
            params.update({'location': location})

        data = api._fetch_data(params).find('toptracks')
        return [
            Track(api,
                  name=t.findtext('name'),
                  mbid=t.findtext('mbid'),
                  artist=Artist(api,
                                name=t.findtext('artist/name'),
                                mbid=t.findtext('artist/mbid'),
                                url=t.findtext('artist/url')),
                  stats=Stats(subject=t.findtext('name'),
                              rank=int(t.attrib['rank']),
                              playcount=int(t.findtext('playcount'))
                              if t.findtext('playcount') else None,
                              listeners=int(t.findtext('listeners'))
                              if t.findtext('listeners') else None),
                  streamable=(t.findtext('streamable') == '1'),
                  full_track=(t.find('streamable').attrib['fulltrack'] == '1'),
                  url='http://' + t.findtext('url'),
                  image={'large': t.findtext('image')})
            for t in data.findall('track')
        ]
Esempio n. 8
0
        def get_tracks(self, limit=None, page=None):
            params = self._default_params({'method': 'library.getTracks'})
            if limit is not None:
                params.update({'limit': limit})
            if page is not None:
                params.update({'page': page})

            try:
                data = self._api._fetch_data(params).find('tracks')
                total_pages = int(data.attrib['totalPages'])
                yield total_pages

                for t in data.findall('track'):
                    yield Track(
                        self._api,
                        subject=self,
                        name=t.findtext('name'),
                        artist=Artist(
                            self._api,
                            subject=self,
                            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'),
                                    playcount=t.findtext('playcount')
                                    and int(t.findtext('playcount')) or None,
                                    tagcount=t.findtext('tagcount')
                                    and int(t.findtext('tagcount')) or None),
                        streamable=(t.findtext('streamable') == '1'),
                        full_track=(
                            t.find('streamable').attrib['fulltrack'] == '1'),
                        image=dict([(i.get('size'), i.text)
                                    for i in t.findall('image')]),
                    )
            except LastfmError:
                yield None
Esempio n. 9
0
 def top_tracks(self):
     """
     top tracks of the artist
     @rtype: L{list} of L{Track}
     """
     params = self._default_params({'method': 'artist.getTopTracks'})
     data = self._api._fetch_data(params).find('toptracks')
     return [
         Track(
             self._api,
             subject=self,
             name=t.findtext('name'),
             artist=self,
             mbid=t.findtext('mbid'),
             stats=Stats(subject=t.findtext('name'),
                         playcount=int(t.findtext('playcount')),
                         rank=int(t.attrib['rank'])),
             streamable=(t.findtext('streamable') == '1'),
             full_track=(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')
     ]
Esempio n. 10
0
 def get_recent_tracks(self, limit=None):
     params = self._default_params({'method': 'user.getRecentTracks'})
     if limit is not None:
         params.update({'limit': limit})
     data = self._api._fetch_data(params,
                                  no_cache=True).find('recenttracks')
     return [
         Track(self._api,
               subject=self,
               name=t.findtext('name'),
               artist=Artist(
                   self._api,
                   subject=self,
                   name=t.findtext('artist'),
                   mbid=t.find('artist').attrib['mbid'],
               ),
               album=Album(
                   self._api,
                   subject=self,
                   name=t.findtext('album'),
                   artist=Artist(
                       self._api,
                       subject=self,
                       name=t.findtext('artist'),
                       mbid=t.find('artist').attrib['mbid'],
                   ),
                   mbid=t.find('album').attrib['mbid'],
               ),
               mbid=t.findtext('mbid'),
               streamable=(t.findtext('streamable') == '1'),
               url=t.findtext('url'),
               image=dict([(i.get('size'), i.text)
                           for i in t.findall('image')]),
               played_on=datetime(*(time.strptime(
                   t.findtext('date').strip(), '%d %b %Y, %H:%M')[0:6])))
         for t in data.findall('track')
     ]