Beispiel #1
0
def test_song_from_filename_errors():
    """
    Test the different errors that may be raised from calling
    Song.from_filename().
    """
    assert Song.from_filename('') is None

    with NamedTemporaryFile() as temp:
        assert Song.from_filename(temp.name) is None

    with TemporaryDirectory() as temp:
        assert Song.from_filename(temp) is None
Beispiel #2
0
def test_run_one_song(mp3file, monkeypatch):
    """
    Test the run() function when passing a single song object. It should call
    get_lyrics_threaded to search for lyrics in all the sources at the same
    time.
    """
    song_lyrics = 'some lyrics here'

    def fake_getlyricsthreaded(songs):
        song.lyrics = song_lyrics
        return Result(song=song, source='whatever', runtimes={})

    song = Song.from_filename(mp3file)
    monkeypatch.setattr(lyricfetch.run, 'get_lyrics_threaded',
                        fake_getlyricsthreaded)
    lyricfetch.run.run(song)
    assert Song.from_filename(mp3file).lyrics == song_lyrics
Beispiel #3
0
def test_getlyrics_from_song(mp3file):
    """
    Check that the main method can find the lyrics for a song and write them as
    ID3 metadata.
    """
    tag_mp3(mp3file, artist='YOB', title='Our raw heart')
    song = Song.from_filename(mp3file)
    result = get_lyrics(song)
    assert 'my restless ghost' in result.song.lyrics.lower()
Beispiel #4
0
def test_run_multiple_songs(mp3file, monkeypatch):
    """
    Test the run() function when passing multiple songs. This time it should
    call run_mp() on the entire collection.
    """
    def fake_runmp(songs):
        for i, song in enumerate(songs):
            tag_mp3(song.filename, lyrics=f'lyrics{i}')
        return Stats()

    other_mp3 = tempfile.mktemp()
    shutil.copy(mp3file, other_mp3)
    mp3files = [mp3file, other_mp3]
    songs = [Song.from_filename(f) for f in mp3files]
    CONFIG['print_stats'] = False
    monkeypatch.setattr(lyricfetch.run, 'run_mp', fake_runmp)
    lyricfetch.run.run(songs)
    for i, filename in enumerate(mp3files):
        assert Song.from_filename(filename).lyrics == f'lyrics{i}'
Beispiel #5
0
def test_process_result(mp3file):
    """
    Check that the `process_result()` function can write the lyrics to the
    corresponding mp3 and return wheter or not they were found.
    """
    artist = 'lör'
    title = 'requiem'
    song_lyrics = 'hello world'
    tag_mp3(mp3file, artist=artist, title=title)
    song = Song.from_filename(mp3file)
    song.lyrics = song_lyrics

    result_notfound = Result(song=song, source=None, runtimes={})
    assert not process_result(result_notfound)
    assert not Song.from_filename(mp3file).lyrics

    result_found = Result(song=song, source='whatever', runtimes={})
    assert process_result(result_found)
    assert Song.from_filename(mp3file).lyrics == song_lyrics
Beispiel #6
0
def test_getlyrics_dont_overwrite(mp3file):
    """
    Check that we skip a song if the mp3 file already has embedded lyrics.
    """
    placeholder = 'Some lyrics'
    tag_mp3(mp3file, lyrics=placeholder)

    song = Song.from_filename(mp3file)
    CONFIG['overwrite'] = False
    assert get_lyrics(song) is None
    assert song.lyrics == placeholder
Beispiel #7
0
def test_getlyrics_overwrite(mp3file):
    """
    Check that we can overwrite the lyrics of a song if it already has them.
    """
    placeholder = 'Some lyrics'
    tag_mp3(mp3file, artist='Baroness', title='Eula', lyrics=placeholder)

    song = Song.from_filename(mp3file)
    CONFIG['overwrite'] = True
    result = get_lyrics(song)
    assert result.song.lyrics != placeholder
    assert 'forget the taste of my own tongue' in result.song.lyrics.lower()
Beispiel #8
0
def test_song_from_filename(mp3file):
    """
    Create a song object from an mp3 file.
    """
    tag_mp3(mp3file,
            artist='Kataklysm',
            title='Born to kill and destined to die',
            album='Meditations')

    song = Song.from_filename(mp3file)
    assert song
    assert song.filename == mp3file
    assert song.artist == 'Kataklysm'
    assert song.title == 'Born to kill and destined to die'
    assert song.album == 'Meditations'
Beispiel #9
0
        artist = item['artists'][0]['name']
        url = item['external_urls']['spotify']
        curr_albums[(artist, name)] = url
    if len(query['items']) < page:
        break
    offset += page


# Get the local list of albums
if os.path.isfile('.cache-musiclib'):
    with open('.cache-musiclib', 'rb') as f:
        songs = pickle.load(f)
else:
    music_home = os.path.expanduser('~/Music')
    mp3files = glob.iglob(music_home + '/**/*.mp3', recursive=True)
    songs = set(Song.from_filename(f) for f in mp3files)
    with open('.cache-musiclib', 'wb') as f:
        pickle.dump(songs, f)
library = set((song.artist, song.album) for song in songs)


# Load the cached album url list
if os.path.isfile('.cache-album_urls'):
    with open('.cache-album_urls', 'rb') as f:
        album_urls = pickle.load(f)
else:
    album_urls = dict()


# Get the urls for all the albums we don't already have
for artist, album in library: