def select_artwork( details, media_type ):
    artwork = None
    selection = None
    if media_type in ( "cdart", "cover" ):
        if media_type == "cdart":
            artwork = remote_cdart_list( details )
        else:
            artwork = remote_coverart_list( details )
        if artwork:
            for art in artwork:
                if art["musicbrainz_albumid"] == details[ "musicbrainz_albumid" ]:
                    selection = art
            if not selection:
                dialog_msg( "okdialog", heading = __language__( 32033 ), line1 = __language__( 32030 ), line2 = __language__( 32031 ), background = False )
        else:
            dialog_msg( "okdialog", heading = __language__( 32033 ), line1 = __language__( 32030 ), line2 = __language__( 32031 ), background = False )
    else:
        if media_type == "fanart":
            artwork = remote_fanart_list( details )
        elif media_type == "clearlogo":
            artwork = remote_clearlogo_list( details )
        elif media_type == "hdlogo":
            artwork = remote_hdlogo_list( details )
        elif media_type == "artistthumb":
            artwork = remote_artistthumb_list( details )
        elif media_type == "musicbanner":
            artwork = remote_banner_list( details )
        if artwork:
            for art in artwork:
                print art
def auto_download( type, artist_list, background=False ):
    is_canceled = False
    log( "Autodownload", xbmc.LOGDEBUG )
    try:
        artist_count = 0
        download_count = 0
        cdart_existing = 0
        album_count = 0
        d_error=False
        percent = 1
        successfully_downloaded = []
        if type in ( "clearlogo_allartists", "artistthumb_allartists", "fanart_allartists", "musicbanner_allartists" ):
            if type == "clearlogo_allartists":
                type = "clearlogo"
            elif type == "artistthumb_allartists":
                type = "artistthumb"
            elif type == "musicbanner_allartists":
                type = "musicbanner"
            else:
                type = "fanart"
        count_artist_local = len( artist_list )
        dialog_msg( "create", heading = __language__(32046), background = background )
        #Onscreen Dialog - Automatic Downloading of Artwork
        key_label = type
        for artist in artist_list:
            low_res = True
            if dialog_msg( "iscanceled", background = background ) or is_canceled:
                is_canceled = True
                break
            artist_count += 1
            if not artist["has_art"] == "True":
            # If fanart.tv does not report that it has an artist match skip it.
                continue
            percent = int( (artist_count / float(count_artist_local) ) * 100)
            if percent < 1:
                percent = 1
            if percent > 100:
                percent = 100
            log( "Artist: %-40s Local ID: %-10s   Distant MBID: %s" % ( artist["name"], artist["local_id"], artist["musicbrainz_artistid"] ), xbmc.LOGNOTICE )
            if type in ( "fanart", "clearlogo", "artistthumb", "musicbanner" ) and artist[ "has_art" ]:
                dialog_msg( "update", percent = percent, line1 = "%s%s" % ( __language__(32038), get_unicode( artist["name"] ) ), background = background )
                auto_art = {}
                temp_art = {}
                temp_art["musicbrainz_artistid"] = artist["musicbrainz_artistid"]
                auto_art["musicbrainz_artistid"] = artist["musicbrainz_artistid"]
                temp_art["artist"] = artist["name"]
                auto_art["artist"] = artist["name"]
                path = os.path.join( music_path, change_characters( smart_unicode( artist["name"] ) ) )
                if type == "fanart":
                    art = remote_fanart_list( auto_art )
                elif type == "clearlogo":
                    art = remote_clearlogo_list( auto_art )
                    arthd = remote_hdlogo_list( auto_art )
                elif type == "musicbanner":
                    art = remote_banner_list( auto_art )
                else:
                    art = remote_artistthumb_list( auto_art )
                if art:
                    if type == "fanart":
                        temp_art["path"] = path
                        auto_art["path"] = os.path.join( path, "extrafanart" ).replace( "\\\\" , "\\" )
                        if not exists( auto_art["path"] ):
                            try:
                                if _makedirs( auto_art["path"] ):
                                    log( "extrafanart directory made", xbmc.LOGDEBUG )
                            except:
                                print_exc()
                                log( "unable to make extrafanart directory", xbmc.LOGDEBUG )
                                continue
                        else:
                            log( "extrafanart directory already exists", xbmc.LOGDEBUG )
                    else:
                        auto_art["path"] = path
                    if type == "fanart":
                        if enable_fanart_limit:
                            fanart_dir, fanart_files = listdir( auto_art["path"] )
                            fanart_number = len( fanart_files )
                            if fanart_number == fanart_limit:
                                continue
                        if not exists( os.path.join( path, "fanart.jpg" ).replace( "\\\\", "\\" ) ):
                            message, d_success, final_destination, is_canceled = download_art( art[0], temp_art, artist["local_id"], "fanart", "single", 0, background )
                        for artwork in art:
                            fanart = {}
                            if enable_fanart_limit and fanart_number == fanart_limit:
                                log( "Fanart Limit Reached", xbmc.LOGNOTICE )
                                continue
                            if exists( os.path.join( auto_art["path"], os.path.basename( artwork ) ) ):
                                log( "Fanart already exists, skipping", xbmc.LOGDEBUG )
                                continue
                            else:
                                message, d_success, final_destination, is_canceled = download_art( artwork, auto_art, artist["local_id"], "fanart", "auto", 0, background )
                            if d_success == 1:
                                if enable_fanart_limit:
                                    fanart_number += 1
                                download_count += 1
                                fanart["artist"] = auto_art["artist"]
                                fanart["path"] = final_destination
                                successfully_downloaded.append( fanart )
                            else:
                                log( "Download Error...  Check Path.", xbmc.LOGDEBUG )
                                log( "    Path: %s" % auto_art["path"], xbmc.LOGDEBUG )
                                d_error = True
                    else:
                        if type == "clearlogo":
                            if arthd and enable_hdlogos:
                                artwork = arthd[0]
                            else:
                                artwork = art[0]
                        else:
                            artwork = art[0]
                        if type == "artistthumb":
                            if resizeondownload:
                                low_res = check_size( auto_art["path"], key_label, 1000, 1000 )
                            # Fixed always redownloading Thumbs
                            else:
                                low_res = False
                            if exists( os.path.join( auto_art["path"], "folder.jpg" ) ) and not low_res:
                                log( "Artist Thumb already exists, skipping", xbmc.LOGDEBUG )
                                continue
                            else:
                                message, d_success, final_destination, is_canceled = download_art( artwork , auto_art, artist["local_id"], "artistthumb", "auto", 0, background )
                        elif type == "clearlogo":
                            if enable_hdlogos and resizeondownload and arthd:
                                low_res = check_size( auto_art["path"], key_label, 800, 310 )
                            else:
                                low_res = False
                            if exists( os.path.join( auto_art["path"], "logo.png" ) ) and not low_res:
                                log( "ClearLOGO already exists, skipping", xbmc.LOGDEBUG )
                                continue
                            else:
                                message, d_success, final_destination, is_canceled = download_art( artwork , auto_art, artist["local_id"], "clearlogo", "auto", 0, background )
                        elif type == "musicbanner":
                            if exists( os.path.join( auto_art["path"], "banner.jpg" ) ):
                                log( "Music Banner already exists, skipping", xbmc.LOGDEBUG )
                                continue
                            else:
                                message, d_success, final_destination, is_canceled = download_art( artwork , auto_art, artist["local_id"], "musicbanner", "auto", 0, background )
                        if d_success == 1:
                            download_count += 1
                            auto_art["path"] = final_destination
                            successfully_downloaded.append( auto_art )
                        else:
                            log( "Download Error...  Check Path.", xbmc.LOGDEBUG )
                            log( "    Path: %s" % auto_art["path"], xbmc.LOGDEBUG )
                            d_error = True
                else :
                        log( "Artist Match not found", xbmc.LOGDEBUG )
            elif type in ( "cdart", "cover" ) and artist[ "has_art" ]:
                local_album_list = get_local_albums_db( artist["name"], background )
                if type == "cdart":
                    remote_art_url = remote_cdart_list( artist )
                else:
                    remote_art_url = remote_coverart_list( artist )
                for album in local_album_list:
                    low_res = True
                    if dialog_msg( "iscanceled", background = background ):
                            break
                    if not remote_art_url:
                        log( "No artwork found", xbmc.LOGDEBUG )
                        break
                    album_count += 1
                    if not album["musicbrainz_albumid"]:
                        continue
                    dialog_msg( "update", percent = percent, line1 = "%s%s" % ( __language__(32038) , get_unicode( artist["name"] ) ), line2 = "%s%s" % (__language__(32039) , get_unicode( album["title"] ) ), background = background )
                    name = artist["name"]
                    title = album["title"]
                    log( "Album: %s" % album["title"], xbmc.LOGDEBUG )
                    if not album[key_label] or resizeondownload:
                        musicbrainz_albumid = album["musicbrainz_albumid"]
                        art = artwork_search( remote_art_url, musicbrainz_albumid, album["disc"], key_label )
                        if art:
                            if resizeondownload:
                                low_res = check_size( album["path"].replace( "\\\\", "\\" ), key_label, art["size"], art["size"] )
                            if art["picture"]: 
                                log( "ALBUM MATCH ON FANART.TV FOUND", xbmc.LOGDEBUG )
                                #log( "test_album[0]: %s" % test_album[0], xbmc.LOGDEBUG )
                                if low_res:
                                    message, d_success, final_destination, is_canceled = download_art( art["picture"], album, album["local_id"], key_label, "auto", 0, background )
                                    if d_success == 1:
                                        download_count += 1
                                        album[key_label] = True
                                        album["path"] = final_destination
                                        successfully_downloaded.append( album )
                                    else:
                                        log( "Download Error...  Check Path.", xbmc.LOGDEBUG )
                                        log( "    Path: %s" % repr( album["path"] ), xbmc.LOGDEBUG )
                                        d_error = True
                                else:
                                    pass
                            else:
                                log( "ALBUM NOT MATCHED ON FANART.TV", xbmc.LOGDEBUG )
                        else:
                            log( "ALBUM NOT MATCHED ON FANART.TV", xbmc.LOGDEBUG )
                    else:
                        log( "%s artwork file already exists, skipping..." % key_label, xbmc.LOGDEBUG )
        dialog_msg( "close", background = background )
        if d_error:
            dialog_msg( "ok", line1 = __language__(32026), line2 = "%s: %s" % ( __language__(32041), download_count ), background = background )
        else:
            dialog_msg( "ok", line1 = __language__(32040), line2 = "%s: %s" % ( __language__(32041), download_count ), background = background  )
        return download_count, successfully_downloaded
    except:
        print_exc()
        dialog_msg( "close", background = background )