commit 3e7f1f4b14f65a958dd0f7b2b94d7bdad788d461 Author: JayPiKay Date: Sat Oct 27 12:14:56 2018 +0200 Sbeam POC diff --git a/README.md b/README.md new file mode 100644 index 0000000..27865b8 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +curl -A sbeam 192.168.178.50:8080/superlist +curl -A sbeam 192.168.178.50:8080/getstream | hexdump -C diff --git a/sbeam.py b/sbeam.py new file mode 100644 index 0000000..9b8c2e0 --- /dev/null +++ b/sbeam.py @@ -0,0 +1,59 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2018 jpk +# +# Distributed under terms of the MIT license. + +""" + +""" + +import os +import requests + + +def get_superlist(): + with requests.get('http://192.168.178.50:8080/superlist', + headers={'User-Agent': 'sbeam'}) as response: + assert response.status_code == 200 + return response.text.split('\n') + + +def save_stream(filename): + with requests.get('http://192.168.178.50:8080/getstream', stream=True, + headers={'User-Agent': 'sbeam'}) as response: + assert response.status_code == 200 + with open(filename, 'wb') as handle: + # length = response.headers.get('Content-length') + # print(length) + for chunk in response.iter_content(chunk_size=8196): + handle.write(chunk) + + +def split_stream(data, directory, superlist): + with open(data, 'rb') as blob: + for item in superlist: + if not item: + break + filename, filesize, filepath, timestamp = item.split('||') + fullpath = os.path.join(directory, filepath[1:]) + os.makedirs(fullpath, exist_ok=True) + print(f'Saving {filename} ({filesize} bytes) to disk...') + with open(os.path.join(fullpath, filename), 'wb') as handle: + handle.write(blob.read(int(filesize))) + + +if __name__ == '__main__': + to_download = 0 + superlist = get_superlist() + try: + for item in superlist: + to_download += int(item.split('||')[1]) + except IndexError: + print(f'Download stream contains {len(superlist) - 1} items.') + finally: + print(f'Target download size is {to_download} bytes.') + save_stream('data.gz') + split_stream('data.gz', 'received', superlist)