Skip to content

Commit

Permalink
added lastfm.py for augmenting data with info from lastfm.py
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Apr 4, 2013
1 parent 4155e0e commit ca6ce4f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -13,6 +13,11 @@ The steps for reproducing the results can be done using the following steps:
# add rdio keys to config.py # add rdio keys to config.py
./aoty.py | ./compare.py > aoty.json ./aoty.py | ./compare.py > aoty.json


If you would like to add LastFM information to the json you will need to get
an LastFM access key and add it to your config.py, then:

cat aoty.json | ./lastfm.py > aoty-lastfm.json

Maybe I should have dumped the crawled data into CouchDB instead of chaining Maybe I should have dumped the crawled data into CouchDB instead of chaining
JSON dumps together like this. Could be more fun right? It would make it JSON dumps together like this. Could be more fun right? It would make it
easier to not repeat spotify and rdio API lookups. easier to not repeat spotify and rdio API lookups.
Expand Down
42 changes: 42 additions & 0 deletions lastfm.py
@@ -0,0 +1,42 @@
#!/usr/bin/env python

"""
Reads the line oriented output of compare.py and layers in last.fm information
where available. You'll need to add your lastfm key to config.py
"""

import sys
import json
import time
import config
import logging
import requests
import fileinput

def main():
logging.basicConfig(filename="augment.log", level=logging.INFO)
for line in fileinput.input():
a = json.loads(line)
if a.has_key('rdio') and a['rdio']['can_stream']:
continue
info = lastfm(a["artist"], a["album"])
if not info:
logging.warn("no hit for %s/%s", artist, album)
else:
logging.info("found %s" % info["album"]["url"])
a["lastfm"] = info["album"]
print json.dumps(a)
# TODO: make this configurable
time.sleep(1)

def lastfm(artist, album):
url = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s&format=json'
r = requests.get(url % (config.LASTFM_KEY, artist, album))
if r.status_code == 200:
return json.loads(r.content)
else:
logging.warn("got %s when fetching info for %s/%s", (r.status_code, artist, album))
return None

if __name__ == "__main__":
main()

0 comments on commit ca6ce4f

Please sign in to comment.