def add_song(artist_name, song_name, lyrics, artist_url=None, song_url=None, index_id=None): """ Add a song row to the database given all the information. :param artist_name: Artist name. :param song_name: Song name. :param lyrics: Song lyrics. :param artist_url: Artist URL. :param song_url: Song URL. :param index_id: NMSLIB index identifier. :return: Song identifier if it was added as expected, None otherwise. """ try: song = Song(artist_name=artist_name, song_name=song_name, lyrics=lyrics, artist_url=artist_url, song_url=song_url, index_id=index_id) add_element(song) commit_session() return song.id except Exception as e: log.error(f'Error adding a song: [{e}]') log.exception(e) return None
def by_song(): """ Controller for searching similarity through song identifier. :return: JSON response. """ try: # Parameters retrieving song_id = request.args.get('song_id') if not song_id: return response.make( error=True, message='`song_id` missed as a query parameter.') # Cache processing method = by_song.__name__ key = '{}'.format(song_id) results_cached = cache.get(method, key) if results_cached is not None: return response.make(response=results_cached, cached=True) # Feature extraction features = searcher.extract_features_from_song(song_id) if features is None: return response.make(error=True, message='Song not found.', method=method, key=key) # Searching results = searcher.search(features, song_id=song_id) # Return results and refresh cache return response.make(error=False, response=dict(similarity_list=results), method=method, key=key) except Exception as e: log.error(f'Unexpected error: [{e}]') log.exception(e) return response.make(error=True, message='Unexpected error.')
def not_found(e): """ Controller for not found scenarios [404]. :param e: Exception raised due to the not found error. :return: JSON response. """ log.error(f'Endpoint not found: [{e}]') return response.make(error=True, message='Not found.', code=404)
def method_not_allowed(e): """ Controller for method not allowed scenarios [405]. :param e: Exception raised due to the method not allowed error. :return: JSON response. """ log.error(f'Method not allowed: [{e}]') return response.make(error=True, message='Method not allowed.', code=405)
def save_w2v_instance(output_file, w2v_instance): """ Save a word2vec instance to an output path. :param output_file: Output path where to save the instance. :param w2v_instance: Word2vec Instance to save. :return: Word2vec instance saved. """ try: w2v_instance.save(output_file) except IOError as e: log.error(f'Error saving the word2vec instance: [{e}]')
def by_content(): """ Controller for searching similarity through song lyrics content. :return: JSON response. """ try: # Parameters retrieving request_json = request.json content = response.get('content', request_json) if not content: return response.make( error=True, message='`content` missed as a request json parameter.') # Cache processing method = by_content.__name__ key = '{}'.format(content) results_cached = cache.get(method, key) if results_cached is not None: return response.make(response=results_cached, cached=True) # Feature extraction features = searcher.extract_features_from_content(content) if features is None: return response.make( error=True, message='Could not be possible to extract features from it.', method=method, key=key) # Searching results = searcher.search(features) # Return results and refresh cache return response.make(error=False, response=dict(similarity_list=results), method=method, key=key) except Exception as e: log.error(f'Unexpected error: [{e}]') log.exception(e) return response.make(error=True, message='Unexpected error.')
def search(): """ Controller for searching songs from the database. :return: JSON response. """ try: # Parameters retrieving query = request.args.get('query') if not query: return response.make( error=True, message='`query` missed as a query parameter.') query = query.strip() if len(query) <= 2: log.warn(f'Query is too short: [{query}]') return response.make(error=False, response=dict(results=[])) # Cache processing method = search.__name__ key = '{}'.format(query) results_cached = cache.get(method, key) if results_cached is not None: return response.make(response=results_cached, cached=True) # Searching results = song_service.get_song_by_query(query) results = [{ 'id': q.id, 'name': f'{q.artist_name} - {q.song_name}' } for q in results] results = sorted(results, key=lambda q: q['name']) # Return results and refresh cache return response.make(error=False, response=dict(results=results), method=method, key=key) except Exception as e: log.error(f'Unexpected error: [{e}]') log.exception(e) return response.make(error=True, message='Unexpected error.')