# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/10_Download-demo-data.ipynb (unless otherwise specified).

__all__ = ['download']

# Cell

import b2sdk.v2 as b2

def download():
    '''Download all datafiles to try the demo. '''

    # Backblaze cloud storage credentials
    bucket_name='crono-maxrf-data-demo'
    application_key_id='002c0259a1c18960000000005'
    application_key='K0021ZxgvJXexIPG6cpTRHOBOQ64ceo'

    # open connection to bucket
    info = b2.InMemoryAccountInfo()
    b2_api = b2.B2Api(info)
    b2_api.authorize_account('production',  application_key_id, application_key)

    # get bucket info
    bucket = b2_api.get_bucket_by_name(bucket_name)
    bucket_dict_list = [f[0].as_dict() for f in bucket.ls()]
    bucket_filenames = [d['fileName'] for d in bucket_dict_list]
    bucket_filesizes = [d['size'] for d in bucket_dict_list]

    print(f'Backblaze cloud storage bucket {bucket_name} contains the following files: ')
    for i, [fname, fsize] in enumerate(zip(bucket_filenames, bucket_filesizes)):
        print(f'({i+1}) {fname} ({fsize / 1e6:0.1f}Mb)')

    print('\nPlease wait while downloading all files from bucket into your current working directory...')

    for i, fname in enumerate(bucket_filenames):
        fh = bucket.download_file_by_name(fname)
        fh.save_to(fname)
    print('Ready!')