def register(): token = bottle.request.query.token if token == None or len(token) < 1: bottle.redirect('http://www.last.fm/api/auth/?cb=http://lastfmerge.appspot.com/register&api_key=' + config.lastfm['Key']) data = common.jsonfetch(common.appendsig('http://ws.audioscrobbler.com/2.0/?method=auth.getSession&format=json&api_key=' + config.lastfm['Key'] + '&token=' + token)) try: username = data['session']['name'] session = data['session']['key'] except KeyError: return '''<h3>Last.fmerge - Last.Fm Api Authentication</h3><p><span style="color: red;">ERROR</span> Authentication has failed, please retry.</p>''' servicetoken = common.Token(length=15).make() q = Users.all(keys_only = True) q.filter('username ='******'''<h3>Last.fmerge - Last.Fm Api Authentication</h3><p>Authentication with the Last.fm API has succeed. Now you can return to the desktop application and give it this code :</p><p><b>''' + servicetoken + '''</b></p>'''
def token_check(servicetoken): print 'Checking code validity...', check_r = common.jsonfetch(common.ws('/check/') + servicetoken, use_cache=False) if check_r.has_key('Error'): print check_r['Message'] return False else: username = check_r['Username'] print 'Valid for ' + username return True
servicetoken = auth_prompt() if token_check(servicetoken): f = open('token', 'w') f.write(servicetoken) f.close() else: servicetoken = None if servicetoken == None: exit() print '\nSelect an operation:\n1 : Scrobble tracks.\n2 : Remove previous scrobbled tracks (revert).\n3 : Unregister service token.' mode = raw_input('[1,2,3]> ') if mode == '3': print common.jsonfetch(common.ws('/unregister/' + servicetoken), use_cache=False)['Message'] rmfile('token') exit() if mode == '2': remove = True print 'Reverting operation selected.' if mode == '1': remove = False print 'Scrobbling operation selected.' print 'Parsing scrobbles file...' f = open('scrobbles.json', 'r') scrobbles = json.loads(f.read()) f.close() #Dropping unicode scrobbles
mbids = list() for track in data: mbid = track['Mbid'] if mbids.count(mbid) == 0: mbids.append(mbid) total = len(mbids) print str(total) print 'Getting durations, it might take a while...' i = 0 while i <= (total - 1): print str(i+1) + ' / ' + str(total) try: results = parser( common.jsonfetch( url(mbids[i]) ), mbid = True ) except: results = None if results != None: for track in data: for td in results: if track['Artist'] == td['Artist'] and track['Name'] == td['Name']: track['Duration'] = td['Duration'] print track['Artist'], track['Name'], track['Duration'] i = i + 1 missing = 0 for track in data: if track.has_key('Duration') == False: track['Duration'] = None
mbids = list() for track in data: mbid = track['Mbid'] if mbids.count(mbid) == 0: mbids.append(mbid) total = len(mbids) print str(total) print 'Getting durations, it might take a while...' i = 0 while i <= (total - 1): print str(i + 1) + ' / ' + str(total) try: results = parser(common.jsonfetch(url(mbids[i])), mbid=True) except: results = None if results != None: for track in data: for td in results: if track['Artist'] == td['Artist'] and track['Name'] == td[ 'Name']: track['Duration'] = td['Duration'] print track['Artist'], track['Name'], track['Duration'] i = i + 1 missing = 0 for track in data: if track.has_key('Duration') == False:
artist = track['artist']['#text'] name = track['name'] try: ts = int(track['date']['uts']) mbid = track['album']['mbid'] except: ts = None mbid = None result.append( {'Artist' : artist, 'Name' : name, 'Time' : ts, 'Mbid' : mbid} ) return {'Result' : result, 'Total' : total} username = raw_input('Last.fm Username: '******'Result'] total = first['Total'] page = 2 while page <= total: print str(page) + ' / ' + str(total) pageresult = parser( common.jsonfetch( url(username, page ), use_cache=False ) )['Result'] for i in pageresult: result.append(i) page = page + 1 f = open(username + '.json', 'w') f.write(json.dumps(result, f)) f.close()