示例#1
0
def download(url, target=None, force=False):
    """
    Download and storage file from a url
    """
    if not is_url(url):
        raise TypeError("Str '%s' is not a valid url." % url)

    storage_folder = target or settings.get('STORAGE_TEMPLATES')
    auth = str(settings.get('REPOSITORY_AUTH')).replace("'", '')
    file_name = path.basename(urlsplit(url).path)
    file_path = storage_folder + file_name

    if force or not path.isfile(file_path):
        Storage.create_folders(storage_folder)
        try:
            request = Request(url)
            if auth:
                request.add_header('Authorization', auth)
            with urlopen(request) as response:
                Storage.file(file_path, response.read(), 'wb')
            logger.log(url, 'download DONE!')
        except HTTPError as e:
            e.msg += ": URL '%s' cannot be downloaded" % url
            raise e
    else:
        logger.log(url, 'from CACHE!')

    return file_path
示例#2
0
def download(url, target=None, force=False):
    """
    Download and storage file from a url
    """
    if not is_url(url):
        raise TypeError("Str '%s' is not a valid url." % url)

    storage_folder = target or settings.get('STORAGE_TEMPLATES')
    file_name = path.basename(urlsplit(url).path)
    file_path = storage_folder + file_name

    if force or not path.isfile(file_path):
        Storage.create_folders(storage_folder)
        try:
            urlretrieve(url, file_path)
            logger.log(url, 'download DONE!')
        except HTTPError as e:
            e.msg += ": URL '%s' cannot be downloaded" % url
            raise e
    else:
        logger.log(url, 'from CACHE!')

    return file_path