receive function splits files automatically into destination

This commit is contained in:
jpk 2019-08-31 11:56:57 +02:00
parent 4ef0cdede9
commit c875addfbf
1 changed files with 38 additions and 1 deletions

View File

@ -62,10 +62,47 @@ def superlist(host):
return response.text.split('\n') return response.text.split('\n')
@cli.command()
@click.argument('host', default='127.0.0.1')
@click.argument('destination', type=click.Path(exists=False))
def receive(host, destination):
"""Download a file stream from HOST and save at DESTINATION.
Receive a file stream from HOST and save all files listed in the superlist
in DESTINATION.
"""
def get_superlist(host):
with requests.get(f'http://{host}:8080/superlist',
headers={'User-Agent': 'sbeam'}) as response:
assert response.status_code == 200
return response.text.split('\n')
filelist = get_superlist(host=host)
with requests.get(f'http://{host}:8080/getstream', stream=True,
headers={'User-Agent': 'sbeam'}) as response:
assert response.status_code == 200
with click.progressbar(filelist, label='Receiving',
bar_template='%(label)s %(info)s:') as bar:
for rawinfo in bar:
if not rawinfo:
break
filename, filesize, filepath, timestamp = rawinfo.split('||')
# TODO fix when filename to long
bar.bar_template = f'%(label)s %(info)s: {filename}'
fullpath = os.path.join(destination, filepath[1:])
os.makedirs(fullpath, exist_ok=True)
with open(os.path.join(fullpath, filename), 'wb') as fd:
# TODO split filesize in chunks with remainder
fd.write(response.raw.read(int(filesize)))
@cli.command() @cli.command()
@click.argument('host', default='127.0.0.1') @click.argument('host', default='127.0.0.1')
@click.argument('filename', default='received.bin') @click.argument('filename', default='received.bin')
def receive(host, filename): def raw_receive(host, filename):
"""Download a SuperBeam stream from HOST and save as FILENAME. """Download a SuperBeam stream from HOST and save as FILENAME.
HOST is the IP of the SuperBeam server. The SuperBeam stream will be saved HOST is the IP of the SuperBeam server. The SuperBeam stream will be saved