Exemple #1
0
 def test_remove_picture(self, datadir):
     """Test removing a picture."""
     file = FileInfo(datadir / 'tagged.flac')
     assert file.remove_picture(3) is True
     assert file.remove_picture(3) is False
     file.update()
     assert file.get_picture(3) is None
Exemple #2
0
    def test_update_tagged(self, datadir):
        """Test updating a tagged FLAC file."""
        file = FileInfo(datadir / 'tagged.flac')
        tags = file.tags

        album = tags.album_tags()
        album['ALBUM'] = 'Album'
        album['ARTIST'] = 'Artist'
        assert tags.update_album(album) is True

        track = tags.track_tags(1)
        track['TITLE'] = 'Track One'
        assert tags.update_track(1, track) is True

        track = tags.track_tags(2)
        assert tags.update_track(2, track) is False

        track = tags.track_tags(3)
        del track['TITLE']
        assert tags.update_track(3, track) is True

        file.update()
        file = FileInfo(datadir / 'tagged.flac')
        tags = file.tags
        self.assert_album(tags.album_tags(), 'Album', 'Artist', 'Test', '2018')
        self.assert_track(tags.track_tags(1), 'Track One', None, None)
        self.assert_track(tags.track_tags(2), 'Track 2', None, None)
        self.assert_track(tags.track_tags(3), None, 'true', 'Terrible')
Exemple #3
0
def cover(flac):
    """Add cover images to tagged FLAC files."""
    mb = MusicBrainz()
    for path in flac:
        info = FileInfo(path)
        if not info.parse_ok:
            continue
        album_tags = info.tags.album_tags()
        mbid = album_tags.get('RELEASE_MBID')
        if mbid is None:
            continue
        front = info.get_picture(FRONT_COVER_TYPE)
        if front is not None:
            continue
        click.echo('{} {}'.format(info.summary, path))
        try:
            release = mb.release_by_id(mbid)
            data = mb.front_cover(release)
            if data is not None:
                front = fc.parse_picture(data, FRONT_COVER_TYPE)
                if info.set_picture(front):
                    info.update()
                width = front.width
                height = front.height
                type_ = fc.picture_ext(front).upper()
                click.echo('- {} x {} {}'.format(width, height, type_))
            else:
                click.echo('- No image found')
        except MusicBrainzError:
            click.echo('- Error while querying MusicBrainz')
            continue
        except Exception as e:
            click.echo('- Error while processing image ({})'.format(e))
            continue
Exemple #4
0
 def test_set_picture(self, datadir):
     """Test setting a picture."""
     file = FileInfo(datadir / 'empty.flac')
     with open(str(datadir / 'cover.png'), 'rb') as cover:
         data = cover.read()
     picture = parse_picture(data, 3)
     assert file.set_picture(picture) is True
     assert file.set_picture(picture) is False
     file.update()
     self.assert_picture(file, 3, 'image/png', 128, 128, 24, data)
Exemple #5
0
def tag(flac, mbid, hide, unhide, hide_track, unhide_track):
    """Tag FLAC files.

    Files with a cue sheet but no album/track-level tags will be tagged using
    metadata from MusicBrainz.
    """
    mb = MusicBrainz()
    for path in flac:
        info = FileInfo(path)
        summary = info.summary
        if not summary.parse_ok or not summary.cuesheet:
            continue
        tagged = summary.album_tags or summary.track_tags
        album_edit = hide or unhide
        track_edit = hide_track or unhide_track
        if tagged and not (album_edit or track_edit):
            continue
        click.echo('{} {}'.format(summary, path))
        album_changed = False
        track_changed = False
        # Query MusicBrainz
        if not tagged:
            try:
                if mbid is None:
                    release = find_release(mb, info)
                else:
                    release = mb.release_by_id(mbid, info.cuesheet)
                if release is None:
                    continue
                original_date = mb.first_release_date(release['group-id'])
            except MusicBrainzError:
                click.echo('- Error while querying MusicBrainz')
                continue
            album_changed |= update_album_tags(info, release, original_date)
            track_changed |= update_track_tags(info, release)
        # Hide or unhide album
        tags = info.tags.album_tags()
        if hide:
            tags['HIDE'] = 'true'
        if unhide:
            tags.pop('HIDE', None)
        album_changed |= info.tags.update_album(tags)
        # Hide or unhide tracks
        for number in hide_track:
            tags = info.tags.track_tags(number)
            tags['HIDE'] = 'true'
            track_changed |= info.tags.update_track(number, tags)
        for number in unhide_track:
            tags = info.tags.track_tags(number)
            tags.pop('HIDE', None)
            track_changed |= info.tags.update_track(number, tags)
        # Save any changes
        if album_changed or track_changed:
            info.update()