예제 #1
0
def user_queue():
    if request.method == 'PUT':
        return update_queue()

    if request.method == 'DELETE':
        return clear_queue()

    if request.method == 'POST':
        return bulk_add_queue()

    queue = current_user.get_queue()
    return jsonify(success=True, queue=queue)
예제 #2
0
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)
예제 #3
0
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)
예제 #4
0
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)
예제 #5
0
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)
예제 #6
0
def user_queue():
    if request.method == 'PUT':
        return update_queue()

    queue = current_user.get_queue()
    return jsonify(success=True, queue=queue)