<commit_before>import logging
from email.mime.image import MIMEImage
from beets.plugins import BeetsPlugin
from beets import mediafile

log = logging.getLogger('beets')

class EmbedCoverArtPlugin(BeetsPlugin):
    """Allows albumart to be embedded into the actual files."""
    def configure(self, config):
        pass

@EmbedCoverArtPlugin.listen('album_imported')
def album_imported(lib, album):
    if album.artpath:
        data = open(album.artpath, 'rb').read()
        img = MIMEImage(data)
        mime_img = img.get_content_subtype()

        if mime_img == 'jpeg':
            kind = mediafile.imagekind.JPEG
        elif mime_img == 'png':
            kind = mediafile.imagekind.PNG
        else:
            log.error('A file of type %s is not allowed as coverart.'
                        % mime_img)
            return

        # Add art to each file.
        log.debug('Embedding album art.')
        for item in album.items():
            f = mediafile.MediaFile(item.path)
            f.art = (data, kind)
            f.save()<commit_msg>use imghdr directly instead of MIME lib (which uses imghdr)<commit_after>import logging
import imghdr
from beets.plugins import BeetsPlugin
from beets import mediafile

log = logging.getLogger('beets')

class EmbedCoverArtPlugin(BeetsPlugin):
    """Allows albumart to be embedded into the actual files."""
    def configure(self, config):
        pass

@EmbedCoverArtPlugin.listen('album_imported')
def album_imported(lib, album):
    if album.artpath:
        data = open(album.artpath, 'rb').read()
        kindstr = imghdr.what(None, data)

        if kindstr == 'jpeg':
            kind = mediafile.imagekind.JPEG
        elif kindstr == 'png':
            kind = mediafile.imagekind.PNG
        else:
            log.error('A file of type %s is not allowed as coverart.'
                        % kindstr)
            return

        # Add art to each file.
        log.debug('Embedding album art.')
        for item in album.items():
            f = mediafile.MediaFile(item.path)
            f.art = (data, kind)
            f.save()
{'fraction_non_alphanumeric': 0.09918931807343824, 'fraction_numerical': 0.0, 'mean_word_length': 2.2476780185758516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7421606', 'n_tokens_mistral': 625, 'n_tokens_neox': 605, 'n_words': 175}