def get_user_collection(user_info): rdio_manager = rdio.Api(user_info[0], user_info[1], user_info[2], user_info[3]) current_user = rdio_manager.current_user() collection = [] user_collection = rdio_manager.get_albums_in_collection( user=current_user.key) # print '**************number of albums: ', len(user_collection) for i in range(0, len(user_collection)): album = {} album['artist'] = user_collection[i].artist_name album['name'] = user_collection[i].name album['tracks'] = [] tracks = rdio_manager.get_tracks_for_album_in_collection( user_collection[i].key, user=current_user.key) # print '******album %d: %s' %(i + 1, user_collection[i].name) # print 'number of tracks: ', len(tracks) for track in tracks: album['tracks'].append(get_track_info(track)) collection.append(album) # print collection return json.dumps({ 'user_name': current_user.name, 'collection': collection })
def get_playlist_type(list_type): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) if list_type not in ['owned', 'collab', 'favorites']: raise APIException('Invalid list type') data = { 'method': 'getUserPlaylists', 'user': current_user.external_id, 'kind': list_type, 'extras': 'radioKey', } try: response = rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve playlist: %s' % str(e)) playlists = [ playlist for playlist in response if playlist['key'] != current_user.queue ] return jsonify(success=True, data=playlists)
def get_queue(self): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], self.oauth_token, self.oauth_token_secret) queue_id = self.get_queue_id() if not queue_id: return [] try: playlists = rdio_manager.get([queue_id], ['tracks']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve your queue: %s' % str(e)) if not playlists: return [] playlist = playlists[0] queue = [] for track in playlist.tracks: data = track._data data['in_queue'] = 1 queue.append(data) return queue
def get_queue_id(self): if self.queue: return self.queue data = { 'method': 'getUserPlaylists', 'user': self.external_id, }; rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], self.oauth_token, self.oauth_token_secret) try: results = rdio_manager.call_api_authenticated(data); if results: for playlist in results: if playlist['name'] == u'Lstn to Rdio': self.queue = playlist['key'] break except Exception as e: current_app.logger.debug(e) raise APIException('Unable to create Lstn playlist: %s' % str(e)) if not self.queue: return None db.session.add(self) db.session.commit() return self.queue
def get_authorized_rdio_manager(user_email='*****@*****.**', api_key=None, api_secret=None): # USER_EMAIL is the email address of the user whose account you want access to. # you can get your own API_KEY and API_SECRET from # http://rdio.mashery.com/member/register # Setup the API manager. If you have an ACCESS_KEY and ACCESS_SECRET for a # particular user, you can pass that in as the third and forth arguments # to Api(). if ((api_key==None) | (api_secret==None)): api_key, api_secret = get_api_info() rdio_manager = rdio.Api(api_key, api_secret) user = rdio_manager.find_user(user_email) print '%s %s key is: %s.' % (user.first_name, user.last_name, user.key) # Set authorization: get authorization URL, then pass back the PIN. token_dict = rdio_manager.get_token_and_login_url() print 'Authorize this application at: %s?oauth_token=%s' % ( token_dict['login_url'], token_dict['oauth_token']) token_secret = token_dict['oauth_token_secret'] oauth_verifier = raw_input('Enter the PIN / oAuth verifier: ').strip() #token = raw_input('Enter oauth_token parameter from URL: ').strip() token = token_dict['oauth_token'] request_token = {"oauth_token":token, "oauth_token_secret":token_secret} authorization_dict = rdio_manager.authorize_with_verifier(oauth_verifier, request_token) # Get back key and secret. rdio_manager is now authorized # on the user's behalf. print 'Access token key: %s' % authorization_dict['oauth_token'] print 'Access token secret: %s' % authorization_dict['oauth_token_secret'] return rdio_manager
def get_tracks(album_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) try: albums = rdio_manager.get([album_id], ['trackKeys']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve albums: %s' % str(e)) tracks = [] if len(albums) > 0: if hasattr(albums[0], 'track_keys') and len(albums[0].track_keys) > 0: try: response = rdio_manager.get(albums[0].track_keys, ['radioKey', 'streamRegions']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve album tracks: %s' % str(e)) trackDict = {} for track in response: trackDict[track.key] = track._data tracks = [] for trackKey in albums[0].track_keys: tracks.append(trackDict[trackKey]) return jsonify(success=True, data=tracks)
def search(): query = request.args.get('query', None) if not query: raise APIException('You must specify a search query') if len(query) < 3: return jsonify(success=True, results=[]) rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) data = { 'method': 'search', 'query': query, 'types': 'track', } try: results = rdio_manager.call_api(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to search music: %s' % str(e)) return jsonify(success=True, results=results['results'])
def get_tracks(playlist_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) try: playlists = rdio_manager.get([playlist_id], ['trackKeys']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve playlist tracks: %s' % str(e)) if not playlists: return [] playlist = playlists[0] if not hasattr(playlist, 'track_keys') or len(playlist.track_keys) == 0: return [] try: tracks = rdio_manager.get(playlist.track_keys, ['radioKey', 'streamRegions']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve playlist tracks: %s' % str(e)) tracks = [track._data for track in tracks] return jsonify(success=True, data=tracks)
def get_favorites(self): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], self.oauth_token, self.oauth_token_secret) keys = self.get_favorite_keys() keys = [key for key in keys if not key.startswith('tp')] if not keys: return [] try: response = rdio_manager.get( keys, ['radioKey', 'streamRegions', 'albumCount']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve favorites: %s' % str(e)) favoriteDict = {} for favorite in response: favoriteDict[favorite.key] = favorite._data favorites = [] for key in keys: favorites.append(favoriteDict[key]) return favorites
def remove_playlist(): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) try: playlists = rdio_manager.delete_playlist(playlist_id) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to remove the track from the playlist: %s' % str(e)) return jsonify(success=True)
def get_station_type(station_type): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) methods = { 'you': { 'method': 'getStations', 'user': current_user.external_id, 'v': '20140512', }, 'friends': { 'method': 'getFriendAndTastemakerStations', 'v': '20140512', }, 'recent': { 'method': 'getRecentStationsHistoryForUser', 'user': current_user.external_id, 'v': '20140512', }, 'genre': { 'method': 'getGenreStations', 'v': '20140512', 'genre': request.args.get('genre') }, 'top': { 'method': 'getCuratedContent', 'curationType': 'top_stations', 'v': '20140512' }, 'new': { 'method': 'getCuratedContent', 'curationType': 'new_releases_weekly_station', }, 'spotlight': { 'method': 'getCuratedContent', 'curationType': 'spotlight', } } if station_type not in methods: raise APIException('Invalid station type') try: response = rdio_manager.call_api_authenticated(methods[station_type]) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve station: %s' % str(e)) return jsonify(success=True, data=response['items'])
def get_collection_tracks(album_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) try: response = rdio_manager.get_tracks_for_album_in_collection( album_id, current_user.external_id, ['radioKey', 'streamRegions']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve tracks: %s' % str(e)) tracks = [track._data for track in response] return jsonify(success=True, data=tracks)
def get_albums(artist_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) try: response = rdio_manager.get_albums_for_artist(artist_id, False, ['radioKey']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve albums: %s' % str(e)) albums = [] if len(response) > 0: albums = [album._data for album in response] return jsonify(success=True, data=albums)
def room_id_action(room_id): if request.method == 'PUT': return room_update_action(room_id) if request.method == 'DELETE': return room_delete_action(room_id) room = Room.query.filter(Room.slug == room_id).first() if not room: room = Room.query.get(int(room_id)) if not room: raise APIException('Room not found', 404) rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) hostname = request.headers['Host'] if ':' in hostname: components = string.split(hostname, ':') hostname = components[0] try: playback = rdio_manager.get_playback_token(hostname) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve playback credentials: %s' % str(e)) if not playback: raise APIException('Unable to retrieve playback token', 500) queue = current_user.get_queue() favorites = current_user.get_favorite_keys() response = { 'success': 1, 'room': room, 'playback': playback, 'queue': queue, 'favorites': favorites, } return jsonify(response)
def bulk_add_queue(): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) queue_playlist = current_user.get_queue_id() data = request.get_json() if not data or 'tracks' not in data: raise APIException('Unable to add the tracks to your queue: %s' % str(e)) if queue_playlist: try: rdio_manager.add_to_playlist(queue_playlist, data['tracks']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to add the tracks to your queue: %s' % str(e)) else: try: playlist = rdio_manager.create_playlist('Lstn to Rdio', 'User Queue for Lstn', data['tracks']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to add the track to your queue: %s' % str(e)) # Throw an error if we couldn't create it if not playlist: raise APIException('Unable to create Rdio playlist', 500) queue_playlist = playlist.key # Update the user's queue field current_user.queue = queue_playlist db.session.add(current_user) db.session.flush() # Get the user's queue queue = current_user.get_queue() return jsonify(success=True, queue=queue)
def get_tracks(playlist_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) tracks = [] playlists = rdio_manager.get([playlist_id], ['tracks']) if len(playlists) > 0: tracks = [track._data for track in playlists[0].tracks] response = { 'success': 1, 'tracks': tracks, } return jsonify(response)
def get_queue(self): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], self.oauth_token, self.oauth_token_secret) queue_id = self.get_queue_id() if not queue_id: return [] try: playlists = rdio_manager.get([queue_id], ['trackKeys']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve your queue: %s' % str(e)) if not playlists: return [] playlist = playlists[0] queue = [] if not hasattr(playlist, 'track_keys') or len( playlist.track_keys) == 0: return [] try: tracks = rdio_manager.get(playlist.track_keys, ['radioKey', 'streamRegions']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve your queue: %s' % str(e)) trackDict = {} for track in tracks: data = track._data data['in_queue'] = 1 trackDict[track.key] = data queue = [] for trackKey in playlist.track_keys: queue.append(trackDict[trackKey]) return queue
def get_playlists(): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) try: playlist_set = rdio_manager.get_playlists() except Exception as e: current_app.logger.debug(e) raise APIException('Unable to get playlists: %s' % str(e)) playlists = { 'owned': [], 'collab': [], 'subscribed': [], 'favorites': [], } if hasattr(playlist_set, 'owned_playlists'): playlists['owned'] = [ playlist._data for playlist in playlist_set.owned_playlists if hasattr(playlist, '_data') and playlist.name != u'Lstn to Rdio' ] if hasattr(playlist_set, 'collab_playlists'): playlists['collab'] = [ playlist._data for playlist in playlist_set.collab_playlists if hasattr(playlist, '_data') ] if hasattr(playlist_set, 'subscribed_playlists'): playlists['subscribed'] = [ playlist._data for playlist in playlist_set.subscribed_playlists if hasattr(playlist, '_data') ] if hasattr(playlist_set, 'favorites_playlists'): playlists['favorites'] = [ playlist._data for playlist in playlist_set.favorites_playlists if hasattr(playlist, '_data') ] return jsonify(success=True, playlists=playlists)
def delete_favorite(track_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) data = { 'method': 'removeFromFavorites', 'keys': track_id, } try: rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException( 'Unable to remove that track from your favorites: %s' % str(e)) return jsonify(success=True)
def get_user_playlists(user_info): rdio_manager = rdio.Api(user_info[0], user_info[1], user_info[2], user_info[3]) current_user = rdio_manager.current_user() playlists = [] user_playlists = rdio_manager.get_playlists( extras=['tracks']).owned_playlists for playlist in user_playlists: single_playlist = {} single_playlist['name'] = playlist.name single_playlist['tracks'] = [] for track in playlist.tracks: single_playlist['tracks'].append(get_track_info(track)) playlists.append(single_playlist) return json.dumps({'user_name': current_user.name, 'playlists': playlists})
def get_collection_albums(artist_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) data = { 'method': 'getAlbumsForArtistInCollection', 'artist': artist_id, 'extras': 'radioKey,-tracks', }; try: response = rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve albums: %s' % str(e)) return jsonify(success=True, data=response)
def delete_queue(track_id): if not current_user.queue: raise APIException('Unable to delete track from non-existant playlist') rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) index = request.args.get('index', 0) try: rdio_manager.remove_from_playlist(current_user.queue, [track_id], index) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to remove that song from your queue: %s' % str(e)) queue = current_user.get_queue() return jsonify(success=True, queue=queue)
def clear_queue(): if not current_user.queue: raise APIException('Unable to clear user queue', 500) rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) queue = current_user.get_queue() if queue: tracks = [track['key'] for track in queue] try: rdio_manager.remove_from_playlist(current_user.queue, tracks) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to reorder your queue: %s' % str(e)) return jsonify(success=True)
def get_favorite_keys(self): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], self.oauth_token, self.oauth_token_secret) data = { 'method': 'getKeysInFavorites', 'user': self.external_id, } try: response = rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve your favorites: %s' % str(e)) if not response: return [] return response['keys']
def update_queue(): if 'queue' not in request.json: raise APIException('You must specify queue data') if not current_user.queue: raise APIException('Unable to set user queue', 500) rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) tracks = [track['key'] for track in request.json['queue']] if tracks: try: rdio_manager.set_playlist_order(current_user.queue, tracks) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to reorder your queue: %s' % str(e)) return jsonify(success=True)
def get_tracks(station_id): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) data = { 'method': 'generateStation', 'station_key': station_id, 'extras': 'trackKeys,streamRegions', } try: station = rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve station tracks: %s' % str(e)) current_app.logger.debug(station) track_keys = [] if 'tracks' in station: track_keys = [track['key'] for track in station['tracks']] tracks = [] if track_keys: try: tracks = rdio_manager.get(track_keys, ['radioKey', 'streamRegions']) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve station tracks: %s' % str(e)) tracks = [track._data for track in tracks] return jsonify(success=True, data=tracks)
def get_playlist_type(list_type): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) if list_type not in ['owned', 'collab', 'subscribed', 'favorites']: raise APIException('Invalid list type') data = { 'method': 'getUserPlaylists', 'user': current_user.external_id, 'kind': list_type, } playlists = {} try: playlists[list_type] = rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to retrieve playlist: %s' % str(e)) return jsonify(success=True, playlists=playlists)
def login(): # TODO: Use CSRF here state = {} rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET']) try: auth = rdio_manager.get_token_and_login_url( url_for('site.auth', _external=True)) except Exception as e: raise WebException('Unable to login: %s' % str(e), 500) if not auth: raise WebException('Unable to login', 500) required = ['login_url', 'oauth_token', 'oauth_token_secret'] missing = [key for key in required if key not in auth] if missing: raise WebException('Unable to authenticate with Rdio', 500) session['oauth_token_secret'] = auth['oauth_token_secret'] auth_url = "%s?oauth_token=%s" % (auth['login_url'], auth['oauth_token']) return redirect(auth_url)
def user_collection(): rdio_manager = rdio.Api(current_app.config['RDIO_CONSUMER_KEY'], current_app.config['RDIO_CONSUMER_SECRET'], current_user.oauth_token, current_user.oauth_token_secret) data = { 'method': 'getArtistsInCollection', 'user': current_user.external_id, 'extras': 'albumCount,radioKey,count', } try: response = rdio_manager.call_api_authenticated(data) except Exception as e: current_app.logger.debug(e) raise APIException('Unable to get your collection: %s' % str(e)) artists = [] for artist in response: artist['length'] = artist['count'] artists.append(artist) return jsonify(success=True, data=artists)
def get_user_playback_token(user_info, domain): rdio_manager = rdio.Api(user_info[0], user_info[1], user_info[2], user_info[3]) playback_token = rdio_manager.get_playback_token(domain) return playback_token