Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added lastfm.py for augmenting data with info from lastfm.py
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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() |