#! /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)