Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script to get the top 100 songs scrobbled on in all the metros in U.S and associated stats and track info.
#Fetches the top 100 songs scrobbled in all the metros in U.S and associated stats and track info.
import urllib
import urllib2
from lxml import etree
import os
import cpickle as pickle
import pickle
lastfm_apikey = '' # use your own api key here
lastfm_url = ''
method_metros = 'geo.getMetros'
method_toptracks = 'geo.gettoptracks'
method_trackinfo = 'track.getInfo'
def call_lastfm(method, **kwargs):
data = {}
data['method'] = method
data['api_key'] = lastfm_apikey
for key in kwargs:
data[key] = kwargs[key]
values = urllib.urlencode(data)
url = lastfm_url + '?' + values
print url
response = urllib2.urlopen(url);
return etree.fromstring(
def get_data():
with open('laststats.csv', 'w') as f:
us = 'united states'
metros = call_lastfm(method_metros, country=us)
musicd = None
pfile = 'musicd.p'
if os.path.isfile(pfile):
musicd = pickle.load(open(pfile, 'rb'))
musicd = {}
for metro in metros.iter('metro'):
city = metro.find('name').text
toptracks = call_lastfm(method_toptracks, country=us, location=city, limit=100)
for track in toptracks.iter('track'):
artist = track.find('artist').find('name').text.encode('utf-8')
name = track.find('name').text.encode('utf-8')
key = '%s-%s' % (artist, name)
listeners = None
playcount = None
if key in musicd:
listeners, playcount = musicd[key]
info = call_lastfm(method_trackinfo, artist=artist, track=name)
listeners = info.find('track').find('listeners').text
playcount = info.find('track').find('playcount').text
musicd[key] = (listeners, playcount)
f.write('\n%s,%s,%s,%s,%s' % (city, artist, name, listeners, playcount))
pickle.dump(musicd, open(pfile, 'wb'))
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.