<commit_before># This file is part of beets.
# Copyright 2012, <NAME>.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Fetches album art.
"""
import urllib
import re
import logging
import os

from beets.plugins import BeetsPlugin
from beets.util import artresizer
from beets import importer
from beets import ui

IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg']
COVER_NAMES = ['cover', 'front', 'art', 'album', 'folder']
CONTENT_TYPES = ('image/jpeg',)

log = logging.getLogger('beets')


# ART SOURCES ################################################################

def do_resize_url(func):
    def wrapper(url, maxwidth=None):
        """Returns url pointing to resized image instead of original one"""
        if maxwidth and artresizer.inst.method == artresizer.WEBPROXY :
            url = artresizer.resize_url(url, maxwidth)
        return func(url)
    return wrapper

@do_resize_url
def _fetch_image(url):
    """Downloads an image from a URL and checks whether it seems to
    actually be an image. If so, returns a path to the downloaded image.
    Otherwise, returns None.
    """
    log.debug('Downloading art: %s' % url)
    try:
        fn, headers = urllib.urlretrieve(url)
    except IOError:
        log.debug('error fetching art')
        return

    # Make sure it's actually an image.
    if headers.gettype() in CONTENT_TYPES:
        log.debug('Downloaded art to: %s' % fn)
        return fn
    else:
        log.debug('Not an image.')


# Cover Art Archive.

CAA_URL = 'http://coverartarchive.org/release/{mbid}/front-500.jpg'

def caa_art(release_id):
    """Return a Cover Art Archive url given a MusicBrain release ID."""
    return CAA_URL.format(mbid=release_id)


# Art from Amazon.

AMAZON_URL = 'http://images.amazon.com/images/P/%s.%02i.LZZZZZZZ.jpg'
AMAZON_INDICES = (1, 2)

def art_for_asin(asin):
    """Return url for an Amazon ID (ASIN) string."""
    for index in AMAZON_INDICES:
        url = AMAZON_URL % (asin, index)
        try:
            urlopen(url)
            return url
        except IOError:
            pass # does not exist
    


# AlbumArt.org scraper.

AAO_URL = 'http://www.albumart.org/index_detail.php'
AAO_PAT = r'href\s*=\s*"([^>"]*)"[^>]*title\s*=\s*"View larger image"'

def aao_art(asin):
    """Return art url from AlbumArt.org."""
    # Get the page from albumart.org.
    url = '%s?%s' % (AAO_URL, urllib.urlencode({'asin': asin}))
    try:
        log.debug('Scraping art URL: %s' % url)
        page = urllib.urlopen(url).read()
    except IOError:
        log.debug('Error scraping art page')
        return<commit_msg>fetchart: fix urlopen call<commit_after># This file is part of beets.
# Copyright 2012, <NAME>.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Fetches album art.
"""
import urllib
import re
import logging
import os

from beets.plugins import BeetsPlugin
from beets.util import artresizer
from beets import importer
from beets import ui

IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg']
COVER_NAMES = ['cover', 'front', 'art', 'album', 'folder']
CONTENT_TYPES = ('image/jpeg',)

log = logging.getLogger('beets')


# ART SOURCES ################################################################

def do_resize_url(func):
    def wrapper(url, maxwidth=None):
        """Returns url pointing to resized image instead of original one"""
        if maxwidth and artresizer.inst.method == artresizer.WEBPROXY :
            url = artresizer.resize_url(url, maxwidth)
        return func(url)
    return wrapper

@do_resize_url
def _fetch_image(url):
    """Downloads an image from a URL and checks whether it seems to
    actually be an image. If so, returns a path to the downloaded image.
    Otherwise, returns None.
    """
    log.debug('Downloading art: %s' % url)
    try:
        fn, headers = urllib.urlretrieve(url)
    except IOError:
        log.debug('error fetching art')
        return

    # Make sure it's actually an image.
    if headers.gettype() in CONTENT_TYPES:
        log.debug('Downloaded art to: %s' % fn)
        return fn
    else:
        log.debug('Not an image.')


# Cover Art Archive.

CAA_URL = 'http://coverartarchive.org/release/{mbid}/front-500.jpg'

def caa_art(release_id):
    """Return a Cover Art Archive url given a MusicBrain release ID."""
    return CAA_URL.format(mbid=release_id)


# Art from Amazon.

AMAZON_URL = 'http://images.amazon.com/images/P/%s.%02i.LZZZZZZZ.jpg'
AMAZON_INDICES = (1, 2)

def art_for_asin(asin):
    """Return url for an Amazon ID (ASIN) string."""
    for index in AMAZON_INDICES:
        url = AMAZON_URL % (asin, index)
        try:
            urllib.urlopen(url)
            return url
        except IOError:
            pass # does not exist
    


# AlbumArt.org scraper.

AAO_URL = 'http://www.albumart.org/index_detail.php'
AAO_PAT = r'href\s*=\s*"([^>"]*)"[^>]*title\s*=\s*"View larger image"'

def aao_art(asin):
    """Return art url from AlbumArt.org."""
    # Get the page from albumart.org.
    url = '%s?%s' % (AAO_URL, urllib.urlencode({'asin': asin}))
    try:
        log.debug('Scraping art URL: %s' % url)
        page = urllib.urlopen(url).read()
    except IOError:
        log.debug('Error scraping art page')
        return
{'fraction_non_alphanumeric': 0.13169642857142858, 'fraction_numerical': 0.00350765306122449, 'mean_word_length': 3.3172746042670336, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5177351', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1948, 'n_words': 658}