48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
import json
|
|
import struct
|
|
import io
|
|
import pika
|
|
|
|
class StreamParser:
|
|
def __init__(self):
|
|
self.buffer = io.BytesIO() # Buffer to accumulate chunks
|
|
self.metadata_length = None # Length of the metadata
|
|
self.metadata = None # Parsed JSON metadata
|
|
self.file_writer = None # File writer for the remaining data
|
|
|
|
def process_chunk(self, chunk):
|
|
"""
|
|
Process a chunk of data and handle it according to the rules.
|
|
"""
|
|
self.buffer.write(chunk)
|
|
|
|
# If metadata length is not yet known, try to read it
|
|
if self.metadata_length is None and self.buffer.tell() >= 4:
|
|
self.buffer.seek(0)
|
|
self.metadata_length = struct.unpack('>I', self.buffer.read(4))[0]
|
|
|
|
# If metadata length is known but metadata is not yet parsed, try to parse it
|
|
if self.metadata_length is not None and self.metadata is None:
|
|
if self.buffer.tell() >= 4 + self.metadata_length:
|
|
self.buffer.seek(4)
|
|
metadata_bytes = self.buffer.read(self.metadata_length)
|
|
self.metadata = json.loads(metadata_bytes.decode('utf-8'))
|
|
print("Metadata:", self.metadata)
|
|
|
|
# Prepare to write the remaining data to a file
|
|
self.file_writer = open(self.metadata.get("filename", "output_file"), "wb")
|
|
|
|
# If metadata is parsed, write the remaining data to the file
|
|
if self.metadata is not None:
|
|
remaining_data = self.buffer.read()
|
|
if remaining_data:
|
|
self.file_writer.write(remaining_data)
|
|
|
|
def close(self):
|
|
"""
|
|
Close the file writer and clean up.
|
|
"""
|
|
if self.file_writer:
|
|
self.file_writer.close()
|
|
self.buffer.close()
|