def download_file(cls, url, local_file_name=None, force=False, chunk_size=1024):
        """
        Download file from a given url
        """

        local_file_name = local_file_name if local_file_name else url.split('/')[-1]
        filepath = os.path.join(cls.data_path, local_file_name)

        if not os.path.exists(filepath) or force:
            try:
                headers = requests.head(url, allow_redirects=True).headers
                length = headers.get('Content-Length')

                logger.info("Starting download of {} file with {} bytes ...".format(url, length))

                widgets = [
                    'Downloading file please wait...', progressbar.Percentage(),
                    ' ', progressbar.Bar(),
                    ' ', progressbar.ETA(),
                    ' ', progressbar.FileTransferSpeed(),
                ]
                bar = progressbar.ProgressBar(widgets=widgets, max_value=int(length) + chunk_size).start()

                r = requests.get(url, stream=True)

                with open(filepath, 'wb') as f:
                    total_chunk = 0

                    for chunk in r.iter_content(chunk_size):
                        if chunk:
                            f.write(chunk)
                            total_chunk += chunk_size
                            bar.update(total_chunk)

                bar.finish()

            except:
                if os.path.exists(filepath):
                    os.remove(filepath)

                raise

        return filepath
def download_file ( cls , url , local_file_name = None , force = False , chunk_size = 1024 ) : local_file_name = local_file_name if local_file_name else url . split ( '/' ) [ - 1 ] filepath = os . path . join ( cls . data_path , local_file_name ) if not os . path . exists ( filepath ) or force : try : headers = requests . head ( url , allow_redirects = True ) . headers length = headers . get ( 'Content-Length' ) logger . info ( "Starting download of {} file with {} bytes ..." . format ( url , length ) ) widgets = [ 'Downloading file please wait...' , progressbar . Percentage ( ) , ' ' , progressbar . Bar ( ) , ' ' , progressbar . ETA ( ) , ' ' , progressbar . FileTransferSpeed ( ) , ] bar = progressbar . ProgressBar ( widgets = widgets , max_value = int ( length ) + chunk_size ) . start ( ) r = requests . get ( url , stream = True ) with open ( filepath , 'wb' ) as f : total_chunk = 0 for chunk in r . iter_content ( chunk_size ) : if chunk : f . write ( chunk ) total_chunk += chunk_size bar . update ( total_chunk ) bar . finish ( ) except : if os . path . exists ( filepath ) : os . remove ( filepath ) raise return filepath
Download file from a given url