Ejemplo n.º 1
0
 def tags(self):
     from lastfm.tag import Tag
     params = self._default_params(
         {'method': '%s.getTags' % self.__class__.__name__.lower()})
     data = self._api._fetch_data(params,
                                  sign=True,
                                  session=True,
                                  no_cache=True).find('tags')
     return SafeList([
         Tag(self._api, name=t.findtext('name'), url=t.findtext('url'))
         for t in data.findall('tag')
     ], self.add_tags, self.remove_tag)
Ejemplo n.º 2
0
 def get_top_tags(self, limit=None):
     params = self._default_params({'method': 'user.getTopTags'})
     if limit is not None:
         params.update({'limit': limit})
     data = self._api._fetch_data(params).find('toptags')
     return [
         Tag(self._api,
             subject=self,
             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')
     ]
Ejemplo n.º 3
0
 def top_tags(self):
     """top tags for the track"""
     params = Track._check_params({'method': 'track.getTopTags'},
                                  self.artist.name, self.name, self.mbid)
     data = self._api._fetch_data(params).find('toptags')
     return [
         Tag(self._api,
             subject=self,
             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')
     ]
Ejemplo n.º 4
0
 def get_tag(self, name, callback = None):
     """
     Get a tag object.
     
     @param name:        the tag name
     @type name:         L{str}
     @param callback:    callback function for asynchronous invocation (optional)
     @type callback:     C{function}
     
     @return:        a tag object corresponding to the tag name provided
     @rtype:         L{Tag}
     
     @see:           L{async_callback}
     """
     return Tag(self, name = name)
Ejemplo n.º 5
0
 def top_tags(self):
     """
     top tags for the artist
     @rtype: L{list} of L{Tag}
     """
     if not hasattr(self, "_top_tags") or self._top_tags is None or len(
             self._top_tags) < 6:
         params = self._default_params({'method': 'artist.getTopTags'})
         data = self._api._fetch_data(params).find('toptags')
         self._top_tags = [
             Tag(self._api,
                 subject=self,
                 name=t.findtext('name'),
                 url=t.findtext('url')) for t in data.findall('tag')
         ]
     return self._top_tags[:]
Ejemplo n.º 6
0
 def top_tags(self):
     """
     top tags for the album
     @rtype: L{list} of L{Tag}
     """
     params = {'method': 'album.getInfo'}
     if self.artist and self.name:
         params.update({'artist': self.artist.name, 'album': self.name})
     elif self.mbid:
         params.update({'mbid': self.mbid})
     data = self._api._fetch_data(params).find('album')
     return [
         Tag(self._api,
             subject=self,
             name=t.findtext('name'),
             url=t.findtext('url')) for t in data.findall('toptags/tag')
     ]
Ejemplo n.º 7
0
 def _fill_info(self):
     data = Album._fetch_data(self._api, self.artist.name, self.name)
     self._id = int(data.findtext('id'))
     self._mbid = data.findtext('mbid')
     self._url = data.findtext('url')
     self._release_date = data.findtext('releasedate') and data.findtext('releasedate').strip() and \
                         datetime(*(time.strptime(data.findtext('releasedate').strip(), '%d %b %Y, 00:00')[0:6]))
     self._image = dict([(i.get('size'), i.text)
                         for i in data.findall('image')])
     if not self._stats:
         self._stats = Stats(
             subject=self,
             listeners=int(data.findtext('listeners')),
             playcount=int(data.findtext('playcount')),
         )
     self._top_tags = [
         Tag(self._api,
             subject=self,
             name=t.findtext('name'),
             url=t.findtext('url')) for t in data.findall('toptags/tag')
     ]
Ejemplo n.º 8
0
    def _fill_info(self):
        data = Artist._fetch_data(self._api, self.name)
        self._name = data.findtext('name')
        self._mbid = data.findtext('mbid')
        self._url = data.findtext('url')
        self._image = dict([(i.get('size'), i.text)
                            for i in data.findall('image')])
        self._streamable = (data.findtext('streamable') == 1)
        if not self._stats:
            self._stats = Stats(
                subject=self,
                listeners=safe_int(data.findtext('stats/listeners')),
                playcount=safe_int(data.findtext('stats/playcount')))


#        self._similar = [
#                          Artist(
#                                 self._api,
#                                 subject = self,
#                                 name = a.findtext('name'),
#                                 url = a.findtext('url'),
#                                 image = dict([(i.get('size'), i.text) for i in a.findall('image')])
#                                 )
#                          for a in data.findall('similar/artist')
#                          ]
        self._top_tags = [
            Tag(self._api,
                subject=self,
                name=t.findtext('name'),
                url=t.findtext('url')) for t in data.findall('tags/tag')
        ]
        self._bio = Wiki(
            self,
            published=data.findtext('bio/published').strip()
            and datetime(*(time.strptime(
                data.findtext('bio/published').strip(),
                '%a, %d %b %Y %H:%M:%S +0000')[0:6])).replace(tzinfo=UTC),
            summary=data.findtext('bio/summary'),
            content=data.findtext('bio/content'))
Ejemplo n.º 9
0
    def create_from_data(api, subject, start, end):
        w = WeeklyChart(
            subject=subject,
            start=start,
            end=end,
        )
        max_tag_count = 3
        global_top_tags = api.get_global_top_tags()
        from collections import defaultdict

        wac = subject.get_weekly_artist_chart(start, end)
        all_tags = defaultdict(lambda: 0)
        tag_weights = defaultdict(lambda: 0)
        total_playcount = 0
        artist_count = 0
        for artist in wac.artists:
            artist_count += 1
            total_playcount += artist.stats.playcount
            tag_count = 0
            for tag in artist.top_tags:
                if tag not in global_top_tags: continue
                if tag_count >= max_tag_count: break
                all_tags[tag] += 1
                tag_count += 1

            artist_pp = artist.stats.playcount / float(wac.stats.playcount)
            cumulative_pp = total_playcount / float(wac.stats.playcount)
            if (cumulative_pp > 0.75
                    or artist_pp < 0.01) and artist_count > 10:
                break

        for artist in wac.artists[:artist_count]:
            artist_pp = artist.stats.playcount / float(wac.stats.playcount)
            tf = 1 / float(max_tag_count)
            tag_count = 0
            weighted_tfidfs = {}
            for tag in artist.top_tags:
                if tag not in global_top_tags: continue
                if tag_count >= max_tag_count: break

                df = all_tags[tag] / float(artist_count)
                tfidf = tf / df
                weighted_tfidf = float(max_tag_count - tag_count) * tfidf
                weighted_tfidfs[tag.name] = weighted_tfidf
                tag_count += 1

            sum_weighted_tfidfs = sum(weighted_tfidfs.values())
            for tag in weighted_tfidfs:
                tag_weights[tag] += weighted_tfidfs[
                    tag] / sum_weighted_tfidfs * artist_pp

            artist_pp = artist.stats.playcount / float(wac.stats.playcount)

        tag_weights_sum = sum(tag_weights.values())
        tag_weights = tag_weights.items()
        tag_weights.sort(key=lambda x: x[1], reverse=True)
        for i in xrange(len(tag_weights)):
            tag, weight = tag_weights[i]
            tag_weights[i] = (tag, weight, i + 1)

        wtc = WeeklyTagChart(
            subject=subject,
            start=wac.start,
            end=wac.end,
            stats=Stats(subject=subject, playcount=1000),
            tags=[
                Tag(api,
                    subject=w,
                    name=tag,
                    stats=Stats(
                        subject=tag,
                        rank=rank,
                        count=int(round(1000 * weight / tag_weights_sum)),
                    )) for (tag, weight, rank) in tag_weights
            ])
        wtc._artist_spectrum_analyzed = 100 * total_playcount / float(
            wac.stats.playcount)
        return wtc