Beispiel #1
0
def update_artist(artist_name, artist_conn):
    """
    Update artist in database
    
    :param artist_name: Artist to add
    :type artist_name: str
    :param artist_conn: Updated artist's connections
    :type artist_conn: dict | bytes

    :rtype: None
    """

    try:
        if connexion.request.is_json:
            artist_conn = ArtistList.from_dict(connexion.request.get_json())
        else:
            raise ValueError()
    except ValueError:
        return InvalidSyntax('400', 'invalid_JSON'), 400

    upd_count = collec.replace_one({
        '_id': artist_name
    }, artist_conn.to_dict()).matched_count

    if upd_count:
        return 200
    else:
        return ArtistNotFound('404', 'artist_name'), 404
Beispiel #2
0
def add_artist(artist_name, artist_conn=None):
    """
    Add artist to database
    
    :param artist_name: Artist to add
    :type artist_name: str
    :param artist_conn: Added artist's connections
    :type artist_conn: dict | bytes

    :rtype: None
    """
    try:
        if connexion.request.is_json:
            artist_conn = ArtistList.from_dict(connexion.request.get_json())
        else:
            raise ValueError()

    except ValueError as e:
        return InvalidSyntax('400', 'invalid_JSON'), 400

    db_data = artist_conn.to_dict()
    db_data['_id'] = artist_name

    try:
        collec.insert_one(db_data)
    except DuplicateKeyError as e:
        return 303

    return 200
    def test_update_artist(self):
        """
        Test case for update_artist

        Update artist in database
        """
        artist_conn = ArtistList()
        response = self.client.open('/v1/{artist_name}'.format(artist_name='artist_name_example'),
                                    method='PUT',
                                    data=json.dumps(artist_conn),
                                    content_type='application/json')
        self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def get_suggestion(artist_name):
    """
    Get similar artists list

    :param artist_name: Artist to be searched
    :type artist_name: str

    :rtype: ArtistList
    """
    encoder = URLEncoder()
    temp = encoder.encode(artist_name)
    if temp == 'test':
        a1 = Artist('test artist1', -1)
        a2 = Artist('test artist2', 1.45)
        return ArtistList([a1, a2])
    else:
        music_map = MapGetter()
        artist_score_list = music_map.retrieve_artist_by_url(temp)
        if not artist_score_list:
            return ArtistNotFound('404', 'Artist '+artist_name+' not found')
        
        return ArtistList([Artist(entry['name'], entry['score'])
                          for entry in artist_score_list['artists']])
Beispiel #5
0
def get_artist(artist_name):
    """
    Get stored similar artists list
    
    :param artist_name: Requested artist
    :type artist_name: str

    :rtype: ArtistList
    """
    result = collec.find_one({'_id': artist_name})
    if result:
        return ArtistList.from_dict(result)
    else:
        return ArtistNotFound('404', 'artist_name'), 404
def get_artist(artist_name):
    """
    Get stored similar artists list
    
    :param artist_name: Requested artist
    :type artist_name: str

    :rtype: ArtistList
    """
    result = collec.find_one({'_id': artist_name})
    if result:
        return ArtistList.from_dict(result)
    else:
        return ArtistNotFound(
            'artist_name',
            'Unable to retrieve; this artist is not in the database'), 404
Beispiel #7
0
 def artists_json(data):
     return ArtistList([Artist(name, score)
                        for name, score in data]).to_dict()