sbeam/sbeam.py

99 lines
3.0 KiB
Python
Executable File

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 jpk <jpk+dev@goatpr0n.de>
#
# Distributed under terms of the MIT license.
"""
> GET /getstream HTTP/1.1
> Host: 192.168.178.50:8080
> User-Agent: sbeam
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< ylfrettub: 1
< Content-Disposition: attachment; filename="RedHotChillyStream"
< Date: Fri, 09 Aug 2019 12:57:20 GMT
< Content-Length: 8137967
< Content-Type: application/octet-stream
Server QR Code = http://superbe.am/q?ATKyqMA=
Base64 = 01 32 b2 a8 c0 = 01 50 178 168 192
"""
import os
import click
import requests
import SuperBeam
@click.group()
def cli():
pass
@cli.command()
@click.option('--recursive', default=True, is_flag=True,
help='Recurse into sub directories')
@click.argument('filename', nargs=-1, type=click.Path(exists=True))
def serve(recursive, filename):
filelist = SuperBeam.build_filelist(filename, recursive)
SuperBeam.serve_forever(filelist)
@cli.command()
@click.argument('host', default='127.0.0.1')
def 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')
@cli.command()
@click.argument('host', default='127.0.0.1')
@click.argument('filename', default='received.bin', type=click.File('w'))
def receive(host, filename):
with requests.get(f'http://{host}:8080/getstream', stream=True,
headers={'User-Agent': 'sbeam'}) as response:
assert response.status_code == 200
with open(filename, 'wb') as handle:
for chunk in response.iter_content(chunk_size=8196):
handle.write(chunk)
@cli.command()
@click.argument('filename', default='received.bin', type=click.File('r'))
@click.argument('superlist', default='superlist', type=click.File('r'))
@click.argument('directory', default='received', type=click.File('w'))
def split_stream(data, superlist, directory):
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__':
cli()
# 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)