<commit_before>import sys
from hashlib import md5
import urllib
import Image
import cStringIO

import pylast

API_key = "d4e4bcc1745369ea3319820b31f3f637"
API_secret = "397a9d0662394ee5aff40bc9beeb4882"

def fetch_cover(artist, album, save_url):
    """
    Tries to fetch an album cover for track `url`
    from Last.FM and save it to `save_url`

    Returns save_url on success, None on failure.
    """

    connection = None
    try:
        connection = pylast.LastFMNetwork(api_key = API_key,api_secret = API_secret)
    except pylast.NetworkError:
        return None

    album = None
    try:
        album = connection.get_album(artist, album)
    except pylast.NetworkError:
        return None

    image = None
    try:
        image = album.get_cover_image()
    except (pylast.NetworkError, pylast.WSError, pylast.MalformedResponseError):
        return None
    print 'got image', image

    if not image:
    	return None

    try:
        req = urllib.urlopen(image)
        im = cStringIO.StringIO(req.read())
        img = Image.open(im)
        print 'got image', img

        img.save(save_url)
        print 'saved image'
        return save_url
    except (IOError, IndexError):
        print 'got exception'
        return None

if __name__ == '__main__':
	print fetch_cover(sys.argv[1], '/tmp/test.png')<commit_msg>Fix variable shadow issue in cover fetch<commit_after>import sys
from hashlib import md5
import urllib
import Image
import cStringIO

import pylast

API_key = "d4e4bcc1745369ea3319820b31f3f637"
API_secret = "397a9d0662394ee5aff40bc9beeb4882"

def fetch_cover(artist, album, save_url):
    """
    Tries to fetch an album cover for track `url`
    from Last.FM and save it to `save_url`

    Returns save_url on success, None on failure.
    """

    connection = None
    try:
        connection = pylast.LastFMNetwork(api_key = API_key,api_secret = API_secret)
    except pylast.NetworkError:
        return None

    album_obj = None
    try:
        album_obj = connection.get_album(artist, album)
    except pylast.NetworkError:
        return None

    image = None
    try:
        image = album_obj.get_cover_image()
    except (pylast.NetworkError, pylast.WSError, pylast.MalformedResponseError):
        return None

    if not image:
    	return None

    try:
        req = urllib.urlopen(image)
        im = cStringIO.StringIO(req.read())
        img = Image.open(im)

        img.save(save_url)
        return save_url
    except (IOError, IndexError):
        return None

if __name__ == '__main__':
	print fetch_cover(sys.argv[1], sys.argv[2], '/tmp/test.png')
{'fraction_non_alphanumeric': 0.08256880733944955, 'fraction_numerical': 0.033256880733944956, 'mean_word_length': 2.809315866084425, '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': '952161', 'n_tokens_mistral': 919, 'n_tokens_neox': 852, 'n_words': 226}