Permalink
Browse files

added lastfm.py for augmenting data with info from lastfm.py

  • Loading branch information...
1 parent 4155e0e commit ca6ce4f269f44bd31df3eb1c308080133b5e8f81 @edsu committed Apr 4, 2013
Showing with 47 additions and 0 deletions.
  1. +5 −0 README.md
  2. +42 −0 lastfm.py
View
@@ -13,6 +13,11 @@ The steps for reproducing the results can be done using the following steps:
# add rdio keys to config.py
./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
JSON dumps together like this. Could be more fun right? It would make it
easier to not repeat spotify and rdio API lookups.
View
@@ -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.