Skip to content

Instantly share code, notes, and snippets.

@shweppsie
Created December 24, 2010 05:19
Show Gist options
  • Save shweppsie/753926 to your computer and use it in GitHub Desktop.
Save shweppsie/753926 to your computer and use it in GitHub Desktop.
import wave
import lastfp
import urllib
import urllib2
URL_FINGERPRINT_ID = 'http://www.last.fm/fingerprint/query/'
URL_METADATA = 'http://ws.audioscrobbler.com/2.0/'
""" firstly fingerprint the file """
frames = []
wav = wave.open('test.wav', 'rb')
while wav.getnframes() > wav.tell():
frames.append(wav.readframes(16))
srate = wav.getframerate()
channels = wav.getnchannels()
sec = (wav.getnframes()/2)/srate
wav.close
frame = iter(frames)
fpdata = lastfp.extract(frame, srate, channels)
""" now we need the fingerprint id from the fingerprint """
params = {
'artist': None,
'album': None,
'track': None,
'duration': sec,
}
url = '%s?%s' % (URL_FINGERPRINT_ID, urllib.urlencode(params))
content_type, data = lastfp.formdata_encode({'fpdata':fpdata})
req = urllib2.Request(url, data)
req.add_header('Content-Type', content_type)
result = urllib2.urlopen(req).read().split()
fpid = result[0]
status = result[1]
print "%s:%s" % (fpid, status)
""" let's look up this fingerprint id on lastfm """
params = {
'method': 'track.getFingerprintMetadata',
'fingerprintid': fpid,
'api_key': '65085e011105adbea86d1fdb8f3fe7a5',
}
url = '%s?%s' % (URL_METADATA, urllib.urlencode(params))
print urllib2.urlopen(url).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment