def get_lyrics(approximate, keep_brackets, artist, title, source, song_filepath): request_artist = artist request_title = title if approximate and source != 'lyricwiki' and source != 'metrolyrics': request_artist = '' if not keep_brackets: request_artist = re.sub(r'\((.+)\)', '', request_artist).strip() request_title = re.sub(r'\((.+)\)', '', request_title).strip() try: if source == 'azlyrics': time_to_sleep = random.randrange(10, 30) time.sleep(time_to_sleep) result = fetcher.azlyrics_search_and_scrape_url( request_artist, request_title) elif source == 'genius': result = fetcher.genius_search_and_scrape_url( request_artist, request_title) elif source == 'lyricsfreak': result = fetcher.lyricsfreak_search_and_scrape_url(request_title) elif source == 'lyricwiki': result = fetcher.lyricwiki_search_and_scrape_url( request_artist, request_title) elif source == 'metrolyrics': result = fetcher.metrolyrics_search_and_scrape_url( request_artist, request_title) elif source == 'musixmatch': time_to_sleep = random.randrange(10, 30) time.sleep(time_to_sleep) result = fetcher.musixmatch_search_and_scrape_url( request_artist, request_title) else: message = logger.create_message( logger.LOG_LEVEL_ERROR, 'Source not valid! (choose from \'azlyrics\', \'genius\', \'lyricsfreak\', \'lyricwiki\', \'metrolyrics\', \'musixmatch\')' ) return ERROR_TUPLE(False, message, song_filepath) if (result is not False): lyrics = result.lyrics url = result.url return LYRICS_TUPLE(True, artist, title, lyrics, url, song_filepath) else: message = logger.create_message( logger.LOG_LEVEL_INFO, 'No lyrics found for {file}!'.format(file=title)) return ERROR_TUPLE(False, message, song_filepath) except Exception as e: message = logger.create_message( logger.LOG_LEVEL_ERROR, 'Something went horribly wrong getting lyrics for {file}! Error: {error}' .format(file=title, error=e)) return ERROR_TUPLE(False, message, song_filepath)
def test_lyricwiki_incorrect_parameters(self): with self.assertRaises(TypeError): result = fetcher.lyricsfreak_search_and_scrape_url( 'Taylor Swift', 'Shake It Off')
def test_azlyrics_nonexistent_title(self): result = fetcher.lyricsfreak_search_and_scrape_url( 'thissongshouldn\'texist') self.assertEqual(result, False)
def test_lyricsfreak_get_lyrics(self): result = fetcher.lyricsfreak_search_and_scrape_url('Shake It Off') self.assertNotEqual(result, False)