コード例 #1
0
def convert_song(id):
    """ Converts a song from .mp3 to .wav"""
    logging.debug("{songs_controller} BEGIN function convert_song()")

    time.sleep(random.expovariate(3 / 2))

    return RESP.response_200(message='Song converted with success')
コード例 #2
0
def update_user(id, body):
    """ Updates an active user matching a given id with given parameters such as name, email and password. When a
    parameter is empty it is not updated"""
    logging.debug("{users_controller} BEGIN function update_user()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        user = CRUD.read_user_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    try:
        if body['email'] is not '' and body['email'] != user.email:
            user_email = CRUD.read_user_by_email(body['email'])
            if user_email is not None:
                return RESP.response_409(
                    message='The given email is already in use!')
    except Exception:
        return RESP.response_500(message='Database is down!')

    try:
        CRUD.update_user(user, body['name'], body['email'],
                         UTILS.hash_password(body['password']))
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='User updated with success!')
コード例 #3
0
def delete_playlist(id):
    """ Deletes a playlist"""
    logging.debug("{users_controller} BEGIN function delete_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        CRUD.delete_object(playlist)

        entries = CRUD.read_songs_from_playlist(id)

        for entry in entries:
            CRUD.delete_object(entry)

        CRUD.commit()
    except Exception:
        CRUD.rollback()
        RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Playlist deleted with success')
コード例 #4
0
def update_playlist(id, body):
    """ Updates a playlist matching a given id with given the given name"""
    logging.debug("{users_controller} BEGIN function update_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    if body['name'] is '':
        return RESP.response_400(message='The name parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        CRUD.update_playlist(playlist, name=body['name'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Playlist updated with success!')
コード例 #5
0
def get_playlist_songs(id):
    """ Retrieves all playlist songs' ids"""
    logging.debug(
        "{users_controller} BEGIN function delete_song_from_playlist()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        songs = CRUD.read_songs_from_playlist(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for song in songs:
        array.append(song.dump())

    return RESP.response_200(message=array)
コード例 #6
0
def convert_song(id):
    """ Converts a song from .mp3 to .wav"""
    logging.debug("{songs_controller} BEGIN function convert_song()")
    """if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        sound = AudioSegment.from_mp3(song.path)
    except Exception:
        return RESP.response_404(message='Song not found in given path!')

    try:
        new_path = song.path.replace(".mp3", ".wav")
        sound.export(new_path, format="wav")
    except Exception:
        return RESP.response_500(message='Error converting song!')"""
    time.sleep(random.expovariate(3 / 2))

    return RESP.response_200(message='Song converted with success')
コード例 #7
0
def update_song(id, body):
    """ Updates an active song matching a given id with given parameters such as title, artist, album, release year and
    path. When a parameter is empty it is not updated"""
    logging.debug("{songs_controller} BEGIN function update_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        CRUD.update_song(song, body['title'], body['artist'], body['album'],
                         body['release_year'], body['path'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song updated with success!')
コード例 #8
0
def get_playlist_songs_info(id):
    """ Retrieves all playlist songs' information"""
    logging.debug("{aggregator_controller} BEGIN function get_playlist_songs_info()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    base_url = PLAYLISTS_MS + '/playlists/songs'
    url = '/'.join((base_url, str(id)))

    # Checks if song exists by sending a request into the Songs Microservice

    headers = {'Content-Type': 'application/json',
               'Authorization': request.headers['Authorization']}
    headers.update(create_http_headers_for_new_span())

    with zipkin_span(service_name='aggregator_ms', span_name='get_playlists_songs') as zipkin_context:
        r = requests.get(url, headers=headers)
        zipkin_context.update_binary_annotations({'http.method': 'GET', 'http.url': url,
                                                  'http.status_code': r.status_code})

    if r.status_code == 400:
        return RESP.response_400()
    if r.status_code == 404:
        return RESP.response_404(message='Playlist not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Playlists_MS is down!')

    response_data = json.loads(r.text)

    song_ids = []
    for dictionary in response_data:
        song_ids.append(dictionary['song_id'])

    return RESP.response_200(message="Playlist Songs Info Retrieved Successfully")
コード例 #9
0
def hello_world():
    # try:
    #     song = CRUD.create_song("titulo", "artista", "album", "2000", "/test",
    #                             "1")
    #     CRUD.commit()
    # except Exception:
    #     CRUD.rollback()
    #     return RESP.response_500(message='Database is down!')
    #
    # if song is None:
    #     return RESP.response_500(message='Error adding song into database!')
    #
    # time.sleep(random.expovariate(3))
    # return RESP.response_201(message='Song created with success!')
    return RESP.response_200(message='Songs_MS working!')
コード例 #10
0
def read_songs_criteria(expression):
    """ Returns a list of songs given an expression"""
    logging.debug("{songs_controller} BEGIN function read_song_criteria()")

    try:
        songs = CRUD.read_songs_by_criteria(expression)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for song in songs:
        array.append(song.dump())

    return RESP.response_200(message=array)
コード例 #11
0
def get_playlist(id):
    """ Gets a playlist given an id"""
    logging.debug("{users_controller} BEGIN function get_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    return RESP.response_200(message=playlist.dump())
コード例 #12
0
def read_song(id):
    """ Returns a song (if any) given an id"""
    logging.debug("{songs_controller} BEGIN function read_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    return RESP.response_200(message=song.dump())
コード例 #13
0
def read_user(email):
    """ Returns an active user (if any) given an email"""
    logging.debug("{users_controller} BEGIN function read_user()")

    if email is '':
        return RESP.response_400(message='The email parameter is empty!')

    try:
        user = CRUD.read_user_by_email_not_deleted(email)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    return RESP.response_200(message=user.dump())
コード例 #14
0
def get_user_playlists(user_id):
    """ Retrieves all user playlists"""
    logging.debug("{users_controller} BEGIN function get_user_playlists()")

    if user_id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlists = CRUD.read_all_user_playlists(user_id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for playlist in playlists:
        array.append(playlist.dump())

    return RESP.response_200(message=array)
コード例 #15
0
def check_login(body):
    """ Checks the login parameters"""
    logging.debug("{users_controller} BEGIN function check_login()")

    if body['email'] is '' or body['password'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    user = CRUD.read_user_by_email_not_deleted(body['email'])
    try:
        user = CRUD.read_user_by_email_not_deleted(body['email'])
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_400(message='Bad login!')

    if UTILS.hash_password(body['password']) != user.password:
        return RESP.response_400(message='Bad login!')

    return RESP.response_200(message=user.dump())
コード例 #16
0
def add_song_to_playlist(id, body):
    """ Adds a song into a playlist"""
    logging.debug("{users_controller} BEGIN function add_song_to_playlist()")

    if id is '' or body['song_id'] is '' or body['user_id'] is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    if playlist.user_id != body['user_id']:
        return RESP.response_400(
            message='This playlist belongs to another user')

    # Checks if song exists by sending a request into the Songs Microservice
    headers = {
        'Content-Type': 'application/json',
        'Authorization': request.headers['Authorization']
    }
    param = {'id': body['song_id']}
    r = requests.get(SONGS_MS + '/songs', params=param, headers=headers)
    if r.status_code == 404:
        return RESP.response_404(message='Song not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Songs_MS is down!')

    try:
        CRUD.create_song_in_playlist(id, body['song_id'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song added into playlist with success!')
コード例 #17
0
def create_token(body):
    logging.debug("{authentication_controller} BEGIN function create_token()")

    if body['email'] is '' or body['password'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    payload = {'email': body['email'], 'password': body['password']}

    logging.debug("{authentication_controller} %s", USERS_MS)
    r = requests.post(USERS_MS + '/login', json=payload)
    if r.status_code == requests.codes.ok:
        token_info = {
            'id': json.loads(r.content).get('id'),
            'name': json.loads(r.content).get('name'),
            'email': json.loads(r.content).get('email')
        }

        token = jwt.encode(token_info, TOKEN_SECRET, algorithm=ALGORITHM)
        return RESP.response_200(message={'token': token.decode('utf-8')})
    if r.status_code == 500:
        return RESP.response_500(message='Users_MS is down!')

    return RESP.response_400(message='Credentials are incorrect!')
コード例 #18
0
def delete_user(id):
    """ Deletes an active user given an id"""
    logging.debug("{users_controller} BEGIN function delete_user()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        user = CRUD.read_user_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    try:
        CRUD.delete_user(user)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='User deleted with success')
コード例 #19
0
def delete_song(id):
    """ Deletes an active song given an id"""
    logging.debug("{songs_controller} BEGIN function delete_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        CRUD.delete_song(song)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song deleted with success')
コード例 #20
0
def create_token(body):
    logging.debug("{authentication_controller} BEGIN function create_token()")

    if body['email'] is '' or body['password'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    payload = {'email': body['email'], 'password': body['password']}

    logging.debug("{authentication_controller} %s", USERS_MS)

    with zipkin_span(service_name='authentication_ms',
                     span_name='create_token') as zipkin_context:
        headers = {}
        headers.update(create_http_headers_for_new_span())
        r = requests.post(USERS_MS + '/login', json=payload, headers=headers)
        zipkin_context.update_binary_annotations({
            'http.method':
            'POST',
            'http.url':
            USERS_MS + '/login',
            'http.status_code':
            r.status_code
        })

    if r.status_code == requests.codes.ok:
        token_info = {
            'id': json.loads(r.content).get('id'),
            'name': json.loads(r.content).get('name'),
            'email': json.loads(r.content).get('email')
        }

        token = jwt.encode(token_info, TOKEN_SECRET, algorithm=ALGORITHM)
        return RESP.response_200(message={'token': token.decode('utf-8')})
    if r.status_code == 500:
        return RESP.response_500(message='Users_MS is down!')

    return RESP.response_400(message='Credentials are incorrect!')
コード例 #21
0
def get_playlist_songs_info(id):
    """ Retrieves all playlist songs' information"""
    logging.debug(
        "{aggregator_controller} BEGIN function get_playlist_songs_info()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    # Checks if song exists by sending a request into the Songs Microservice
    headers = {
        'Content-Type': 'application/json',
        'Authorization': request.headers['Authorization']
    }

    base_url = PLAYLISTS_MS + '/playlists/songs'
    url = '/'.join((base_url, str(id)))

    r = requests.get(url, headers=headers)
    if r.status_code == 400:
        return RESP.response_400()
    if r.status_code == 404:
        return RESP.response_404(message='Playlist not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Playlists_MS is down!')

    response_data = json.loads(r.text)

    song_ids = []
    for dictionary in response_data:
        song_ids.append(dictionary['song_id'])

    pool = multiprocessing.Pool(processes=len(song_ids))
    pool_outputs = pool.map(get_song, song_ids)
    pool.close()
    pool.join()

    return RESP.response_200(message=pool_outputs)
コード例 #22
0
def delete_song_from_playlist(id, song_id, user_id):
    """ Removes a song from a playlist"""
    logging.debug(
        "{users_controller} BEGIN function delete_song_from_playlist()")

    if id is '' or song_id is '' or user_id is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    if playlist.user_id != user_id:
        return RESP.response_400(
            message='This playlist belongs to another user')

    try:
        playlist_song = CRUD.read_song_from_playlist(id, song_id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist_song is None:
        return RESP.response_404(message='Song not found is playlist')

    try:
        CRUD.delete_object(playlist_song)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song removed from playlist with success')
コード例 #23
0
def delete_files():
    os.remove('EventSequence.txt')
    os.remove('Data.csv')
    return RESP.response_200()
コード例 #24
0
def hello_world():
    return RESP.response_200(message='Aggregator_MS working! -> Host: ' +
                             socket.gethostname())
コード例 #25
0
def create_log_entry(body):
    with open('EventSequence.txt', 'a') as file:
        file.write(body['message']+"\n")

    return RESP.response_200(message='Log written with success!')
コード例 #26
0
def hello_world():
    return RESP.response_200(message='Users_MS working!')
コード例 #27
0
def write_to_database():
    DB.run()
    return RESP.response_200()
コード例 #28
0
def hello_world():
    return RESP.response_200(message='Authentication_MS working! -> Host: ' +
                             socket.gethostname())
コード例 #29
0
def hello_world():
    return RESP.response_200(message='Log_Server working!')
コード例 #30
0
def write_to_csv():
    CSV.run()
    return RESP.response_200()