Skip to content

Instantly share code, notes, and snippets.

@gshimansky
Created January 24, 2016 16:07
Show Gist options
  • Save gshimansky/ddef0e85c068742f09ec to your computer and use it in GitHub Desktop.
Save gshimansky/ddef0e85c068742f09ec to your computer and use it in GitHub Desktop.
Download loved tracks from last.fm for local database. Get album name and date for collections where tracks are grouped by albums
import urllib2
import xml.etree.ElementTree as ET
import pickle
import time
def main():
# Gather required info.
lastfm_username = raw_input("Lastfm username: ").strip()
lastfm_key = raw_input("Lastfm API key: ").strip()
loved = []
unknown = []
page = 1
while True:
url = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=%s&api_key=%s&page=%d" % \
(lastfm_username, lastfm_key, page)
print("Fetching: " + url)
tree = ET.parse(urllib2.urlopen(url)).getroot()
tracks = tree.findall('lovedtracks/track')
for track in tracks:
title = track.find('name').text
artist = track.find('artist/name').text
track_mbid = track.find('mbid').text
if track_mbid == None:
unknown.append((artist, title))
print("UNKNOWN!" + artist + ", " + title)
continue
url = "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=%s&mbid=%s" % \
(lastfm_key, track_mbid)
print("Fetching: " + url)
track_tree = ET.parse(urllib2.urlopen(url)).getroot()
album = track_tree.find('track/album')
if album == None:
unknown.append((artist, title))
print("UNKNOWN!" + artist + ", " + title)
continue
track_number = album.get('position')
album_title = album.find('title').text
album_mbid = album.find('mbid').text
url = "http://musicbrainz.org/ws/2/release/%s" % \
(album_mbid)
repeat = True
while repeat:
print("Fetching: " + url)
try:
album_tree = ET.parse(urllib2.urlopen(url)).getroot()
repeat = False
except urllib2.HTTPError as e:
if e.code == 503:
time.sleep(2)
else:
print ("Unknown code: " + str(e))
exit(1)
try:
album_date = album_tree.find('{http://musicbrainz.org/ns/mmd-2.0#}release/{http://musicbrainz.org/ns/mmd-2.0#}date').text
dash_index = album_date.find("-")
if dash_index > 0:
album_date = album_date[0: dash_index]
except AttributeError:
album_date = "NoDate"
track_hash = {
'artist': artist,
'album_name': album_title,
'album_date': album_date,
'album_hash': album_mbid,
'track_position': track_number,
'track_title': title
}
print("Got track info: " + str(track_hash))
loved.append(track_hash)
if len(tracks) < 50:
break
page += 1
print("Got " + str(len(loved)) + " loved tracks")
output = open('loved.pkl', 'wb')
pickle.dump(loved, output)
output.close()
print("Unknown:")
for u in unknown:
print(str(u))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment