def unvote_song(room_number): """ :param room_number: room id :param url: url of the song to unvote :authorization_headers: { "Authorization": "USER_ID FROM COOKIE" } :body: { "url": "SONG URL" } :return: """ #make sure user is unvoting his own song. data = request.json if 'url' not in data: msg = 'URL has not been found!' return Response.responseFailure(msg) url = data['url'] cookie = request.headers.get('Authorization') result, queue, msg = Router.unvote_song(room_number, url, cookie) if result: return Response.responseSuccess({'message': '', 'queue': queue}) else: return Response.responseFailure({'message': msg, 'queue': queue})
def remove_song(room_number): """ Song is removed from the queue\n Look at router.remove_song for more detail\n :param room_number: party room identifier\n :param songId: song to be removed id\n :returns: json success/failure """ data = request.json if 'url' not in data: msg = 'URL has not been found!' return Response.responseFailure(msg) url = data['url'] result = True is_successful, updated_queue, msg = Router.remove_song(room_number, url) if is_successful: return Response.responseSuccess({ 'queue': updated_queue, 'message': 'Song has been removed successfully' }) else: return Response.responseFailure({ 'queue': updated_queue, 'message': msg })
def dequeue_song(room_number): """ Song is dequeued (removed from the queue) if it is in the queue list and places in history\n Look at router.dequeue_song for more detail\n :param room_number: party room identifier\n :returns: json success/failure """ result, history, queue, song, message = Router.dequeue_song(room_number) if result: return Response.responseSuccess({ 'history': history, 'queue': queue, 'song': song, 'message': 'Song has been dequeued successfully' }) else: return Response.responseFailure({ 'history': history, 'queue': queue, 'song': song, 'message': message })
def enqueue_song(room_number): """ Adds a song to the queue\n Look at router.enqueue_song for more detail\n :param room_number: party room identifier\n :bodyparam url: url of the song (Spotify/Youtube), will act as a primary key in MongoDB\n bodyparam name: name of the song (together with author?)\n :bodyparam time: time of the song\n :returns: Response.responseSuccess if added successfully, Response.responseFailure if unable to add. """ data = request.json print(request.json) param = {'url', 'name', 'time'} if data is not None and param.issubset(set(data.keys())): #make sure userId is the same as in the cookie cookie = request.headers.get('Authorization') result, queue = Router.enqueue_song(room_number, data['url'], data['name'], data['time'], MiddlewareUtils.get_userId(cookie)) if result: return Response.responseSuccess({ 'queue': queue, 'msg': 'Song has been enqueued', }) else: return Response.responseFailure({ 'queue': queue, 'msg': 'Song was already enqueued', }) return Response.responseFailure( 'Song was not enqueued! Please enter url and name of the song!')
def get_blocked_members(room_number): """ Gets the list of blocked party members\n :param room_number: party room identifier\n :returns: json{Status, users:{}} """ return Router.get_blocked_members(room_number)
def create_room(): """ Create a room with a given room ID (if this ID is not used yet)\n Look at router.create_room for more detail\n :returns: response message, either success or failure which holds a room object """ result, room, message = Router.create_room() if result: return Response.responseSuccess({'room': room}) else: return Response.responseFailure({'room': room, 'message': message})
def generate_nickname(room_number): """ Generates a random nickname using third-party API :param room_number: :return: json {Status, [msg, nickname]} """ nickname = Router.generate_nickname(room_number) if nickname != "": return Response.responseSuccess({'nickname': nickname}) return Response.responseFailure( {'msg': 'Failed to generate unique nickname'})
def unblock(room_number): """ Unblock a user\n Look at router.unblock for more detail\n :param room_number: party room identifier\n :bodyParam userId: member id to be unblocked\n :returns: 'status' - success/failure """ data = request.json if 'userId' in data: return Router.unblock(room_number, data['userId']) return Response.responseFailure({'msg': 'Failed to unblock user.'})
def join_room(room_number): """ Joins an existing party room\n Look at router.join_room for more detail\n :param room_number: party room identifier\n :bodyParam nickname: \n :bodyParam IP: \n :returns: json{Status, [UserCookie]} """ # print("in main.py") data = request.json print(str(data)) if data is not None and 'nickname' in data and 'IP' in data: return Router.join_room(room_number, data['nickname'], data['IP']) return Response.responseFailure({'msg': 'Failed to join the room.'})
def delete_room(room_number): ''' Completely deletes a party room and all associated information with it\n :param room_number: room id to be destroyed\n :bodyparam masterCookie: cookie to authenticate master\n :returns: JSON object holding a single key ("success" or "failure") ''' status = Router.delete_room(room_number) if status == True: return Response.responseSuccess({ 'message': "Room " + room_number + " has been successfully destroyed." }) else: return Response.responseFailure({ 'message': "Failed to destroy room " + room_number + ". " + str(status) })
def get_pending_songs(room_number): """ retrieves the queue of songs that have not been played yet, SORTED by upvotes\n :param room_number: party room identifier\n :returns: Response.responseSuccess if retrieved successfully, Response.responseFailure if unable to get list """ # print("in Pending songs") result, queue = Router.pending_songs(room_number) if result: return Response.responseSuccess({ 'queue': queue, 'message': 'sorted queue of not yet played songs' }) else: return Response.responseFailure( {'message': 'failed to retrieve song queue'})
def kick(room_number): """ Kicks a party member out of the room\n Look at router.kick for more detail\n :param room_number: party room identifier\n :bodyParam userId: member id to kick\n :returns: 'status' - success/failure """ data = request.json if 'userId' in data: result = Router.kick(room_number, data['userId']) if result: return Response.responseSuccess( {'msg': 'User kicked successfully'}) else: return Response.responseFailure({'msg': 'Failed to kick user'}) return Response.responseFailure({'msg': 'No userId field posted'})
def get_currently_playing(room_number): """ :param room_number: room id :authorization_headers: { "Authorization": "USER_ID FROM COOKIE" } :return: the song that is playing at the moment """ result, currently_playing = Router.get_currently_playing(room_number) if result and currently_playing is not None: return Response.responseSuccess({ 'message': 'Currently playing song successfully retrieved', 'name': currently_playing['name'] }) else: return Response.responseFailure({ 'message': 'Unable to retrieve currently playing song', })
from flask import Flask, request, jsonify from routes.Router import Router from models.RoomKeeper import RoomKeeper from utils.Response import Response app = Flask(__name__) room_keeper = RoomKeeper() router = Router(room_keeper) # TODO - Home page @app.route('/', methods=['GET']) def home(): return Response.responseSuccess( 'Welcome to NQMe! Please enter your room number') # TODO - user joins a room and is returned a cookie @app.route('/<room_number>', methods=['GET']) def join_room(room_number): return router.join_room(room_number) # Song is enqueued if it has not been yet # params: room_number - room id # body params: # url: url of the song (Spotify/Youtube), will act as a primary key in MongoDB # name: name of the song (together with author?)
def get_played_songs(room_number): history = Router.played_songs(room_number) return Response.responseSuccess({ 'played_songs': history, 'message': 'List with played songs' })