示例#1
0
文件: lastfm.py 项目: davej/Ojax-old
def _tags_for_track(artist_name, track_name):
    """
    Get the top tags for a track. Also fetches tags for the artist. Only
    includes tracks that break a certain threshold of usage, defined by
    settings.LASTFM_TAG_USAGE_THRESHOLD (which defaults to 15).
    """

    urls = [
        ARTIST_TAGS_URL % (urllib.quote(artist_name)),
        TRACK_TAGS_URL % (urllib.quote(artist_name), urllib.quote(track_name)),
    ]
    tags = set()
    for url in urls:
        log.debug("Fetching tags from %r", url)
        try:
            xml = utils.getxml(url)
        except HttpLib2Error, e:
            if e.code == 408:
                return ""
            else:
                raise
        for t in xml.getiterator("tag"):
            count = utils.safeint(t.find("count").text)
            if count >= getattr(settings, "LASTFM_TAG_USAGE_THRESHOLD", 15):
                tags.add(slugify(smart_unicode(t.find("name").text)))
示例#2
0
文件: lastfm.py 项目: davej/Ojax-old
def update():
    last_update_date = Item.objects.get_last_update_of_model(Track)
    log.debug("Last update date: %s", last_update_date)

    xml = utils.getxml(RECENT_TRACKS_URL % settings.LASTFM_USERNAME)
    for track in xml.getiterator("track"):
        artist = track.find("artist")
        artist_name = smart_unicode(artist.text)
        artist_mbid = artist.get("mbid")
        track_name = smart_unicode(track.find("name").text)
        track_mbid = smart_unicode(track.find("mbid").text)
        url = smart_unicode(track.find("url").text)
        timestamp = datetime.datetime.fromtimestamp(int(track.find("date").get("uts")))
        if timestamp > last_update_date:
            log.debug("Handling track: %r - %r", artist_name, track_name)
            tags = _tags_for_track(artist_name, track_name)
            _handle_track(artist_name, artist_mbid, track_name, track_mbid, url, timestamp, tags)