def download_from_url(source, destination, progress=False, uncompress=True): """Download a file from an URL and place it somewhere. Like wget. Uses requests and tqdm to display progress if you want. By default it will uncompress files.""" # Modules # from tqdm import tqdm import requests from autopaths import FilePath # Check destination exists # destination = FilePath(destination) destination.directory.create_if_not_exists() # Over HTTP # response = requests.get(source, stream=True) total_size = int(response.headers.get('content-length')) block_size = total_size / 1024 # Do it # with open(destination, "wb") as handle: if progress: for data in tqdm(response.iter_content(chunk_size=block_size), total=1024): handle.write(data) else: for data in response.iter_content(chunk_size=block_size): handle.write(data) # Uncompress # if uncompress: with open(destination) as f: header = f.read(4) if header == "PK\x03\x04": unzip(destination, inplace=True) # Add other compression formats here # Return # return destination
def __init__(self, parent=None, base_dir=None, short_name=None): # Save parent # self.parent = parent # Base dir # if base_dir is None: self.base_dir = self.parent.p.graphs_dir else: self.base_dir = base_dir # Short name # if short_name: self.short_name = short_name if not hasattr(self, 'short_name'): self.short_name = 'graph' # Paths # self.path = FilePath(self.base_dir + self.short_name + '.pdf')