import time import pylast import re import sqlite3 import getpass from md5 import md5 con = sqlite3.connect('dump.db') cur = con.cursor() user_name = raw_input("Enter last.fm username: ") user_password = raw_input("Enter last.fm password: ") api_key = 'd90a0d87f51434863f97038a24c7f859' api_secret = 'ffd90592c48c8f9e232af22e0e43def9' name = raw_input("Enter artist to generate playlist of top tracks: ") top_tracks_file = open(''+name+'+'"toptracks"'.m3u', 'w') # time_periods = ['PERIOD_12MONTHS'] OR ['PERIOD_6MONTHS'] OR ['PERIOD_3MONTHS'] OR ['PERIOD_OVERALL'] time_periods = ['PERIOD_3MONTHS'] md5_user_password = md5(user_password).hexdigest() sg = pylast.SessionKeyGenerator(api_key, api_secret) session_key = sg.get_session_key(user_name, md5_user_password) artist = pylast.Artist(name, api_key, api_secret, session_key) top_tracks = [] for time_period in time_periods: tracks = artist.get_top_tracks() p = re.compile('.*[\s]-[\s](.*), Weight: [\d]+') for i in range (0,5): m = p.match(str(tracks[i])) track = m.groups()[0] for result in con.execute('SELECT path FROM lastfm WHERE artist=? AND title=?',(name, track)): result = "\n".join(result) top_tracks_file.write(result+"\n") top_tracks_file.close() time.sleep(5)