From c875addfbfecc87b57899f9305b1285a48576cb5 Mon Sep 17 00:00:00 2001 From: JayPiKay Date: Sat, 31 Aug 2019 11:56:57 +0200 Subject: [PATCH] receive function splits files automatically into destination --- sbeam.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/sbeam.py b/sbeam.py index 54fd3fe..2797154 100755 --- a/sbeam.py +++ b/sbeam.py @@ -62,10 +62,47 @@ def superlist(host): 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() @click.argument('host', default='127.0.0.1') @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. HOST is the IP of the SuperBeam server. The SuperBeam stream will be saved