diff --git a/examples/test_client.py b/examples/test_client.py index 308f0ad..2bee2a1 100644 --- a/examples/test_client.py +++ b/examples/test_client.py @@ -47,13 +47,13 @@ async def test_client(): handler.send(True, end=True) break - """ - # TODO: save this blob, first. - async for data in api.call('blobs.get', ['&/6q7JOKythgnnzoBI5xxvotCr5HeFkAIZSAuqHiZfLw=.sha256'], 'source'): - if data.type.name == 'BUFFER': - with open('./funny_img.png', 'wb') as f: - f.write(data.data) - """ + img_data = b'' + async for msg in api.call('blobs.get', ['&H4MFPEWh7gAbZG/4TaSsaJI6Hi8lh7PItrHTnaxoSfg=.sha256'], 'source'): + if msg.type.name == 'BUFFER': + img_data += msg.data + if msg.type.name == 'JSON' and msg.data == b'true': + with open('./Autonomouscar1956.jpg', 'wb') as f: + f.write(img_data) async def main(): diff --git a/ssb/packet_stream.py b/ssb/packet_stream.py index d13e339..b8f1f36 100644 --- a/ssb/packet_stream.py +++ b/ssb/packet_stream.py @@ -3,6 +3,7 @@ import struct from asyncio import Event, Queue from enum import Enum from time import time +from math import ceil import simplejson from async_generator import async_generator, yield_ @@ -134,8 +135,9 @@ class PacketStream(object): if not header or header == b'\x00' * 9: return flags, length, req = struct.unpack('>BIi', header) + logger.debug('flags=%s length=%s req=%s', flags, length, req) - n_packets = length // 4096 + 1 + n_packets = ceil(length / 4096) body = b'' for n in range(n_packets):