def GET(self): rdio, currentUser = get_rdio_and_current_user() if rdio and currentUser: user_id = int(currentUser['key'][1:]) myPlaylists = rdio.call('getPlaylists')['result']['owned'] db = get_db() result = list(db.select('discoversong_user', what='address, playlist', where="rdio_user_id=%i" % user_id)) if len(result) == 0: access_token = web.cookies().get('at') access_token_secret = web.cookies().get('ats') db.insert('discoversong_user', rdio_user_id=user_id, address=make_unique_email(currentUser), token=access_token, secret=access_token_secret, playlist='new') result = list(db.select('discoversong_user', what='address, playlist', where="rdio_user_id=%i" % user_id))[0] else: result = result[0] message = '' if 'saved' in get_input(): message = ' Saved your selections.' return render.loggedin(name=currentUser['firstName'], message=message, to_address=result['address'], editform=editform(myPlaylists, result['playlist']) ) else: return render.loggedout()
def GET(self): input = get_input() action = input['button'] rdio, currentUser = get_rdio_and_current_user() user_id = int(currentUser['key'][1:]) db = get_db() if action == 'save': db.update('discoversong_user', where="rdio_user_id=%i" % user_id, playlist=input['playlist']) raise web.seeother('/?saved=True')
def GET(self): # get the state from cookies and the query string request_token = web.cookies().get('rt') request_token_secret = web.cookies().get('rts') verifier = get_input()['oauth_verifier'] # make sure we have everything we need if request_token and request_token_secret and verifier: # exchange the verifier and request token for an access token rdio = get_rdio_with_access(request_token, request_token_secret) rdio.complete_authentication(verifier) # save the access token in cookies (and discard the request token) web.setcookie('at', rdio.token[0], expires=60*60*24*14) # expires in two weeks web.setcookie('ats', rdio.token[1], expires=60*60*24*14) # expires in two weeks web.setcookie('rt', '', expires=-1) web.setcookie('rts', '', expires=-1) # go to the home page raise web.seeother('/') else: # we're missing something important raise web.seeother('/logout')
def POST(self): db = get_db() input = get_input() envelope = json.loads(input['envelope']) to_addresses = envelope['to'] print 'received email to', to_addresses for to_address in to_addresses: lookup = db.select('discoversong_user', what='rdio_user_id, playlist, token, secret', where="address='%s'" % to_address) if len(lookup) == 1: result = lookup[0] access_token = str(result['token']) access_token_secret = str(result['secret']) rdio, current_user = get_rdio_and_current_user(access_token=access_token, access_token_secret=access_token_secret) print 'found user', current_user['username'] subject = input['subject'] title, artist = parse(subject) print 'parsed artist', artist, 'title', title search_result = rdio.call('search', {'query': ' '.join([title, artist]), 'types': 'Track'}) track_keys = [] name_artist_pairs_found = {} for possible_hit in search_result['result']['results']: if possible_hit['canStream']: name = possible_hit['name'] artist_name = possible_hit['artist'] if name_artist_pairs_found.has_key((name, artist_name)): continue name_artist_pairs_found[(name, artist_name)] = True track_key = possible_hit['key'] track_keys.append(track_key) print 'found tracks', track_keys playlist_key = result['playlist'] if playlist_key in ['new', 'alwaysnew']: p_names = [playlist['name'] for playlist in rdio.call('getPlaylists')['result']['owned']] new_name = generate_playlist_name(p_names) print 'creating new playlist', new_name result = rdio.call('createPlaylist', {'name': new_name, 'description': 'Songs found by discoversong on %s.' % datetime.datetime.now().strftime('%A, %d %b %Y %H:%M'), 'tracks': ', '.join(track_keys)}) new_key = result['result']['key'] if playlist_key == 'new': print 'setting', new_key, 'as the playlist to use next time' user_id = int(current_user['key'][1:]) db.update('discoversong_user', where="rdio_user_id=%i" % user_id, playlist=new_key) # else leave 'alwaysnew' to repeat this behavior every time else: rdio.call('addToPlaylist', {'playlist': playlist_key, 'tracks': ', '.join(track_keys)}) return None