Update sozo_gen.py
Browse files- sozo_gen.py +69 -0
sozo_gen.py
CHANGED
|
@@ -283,6 +283,75 @@ def safe_json_dumps(obj, indent=2, **kwargs):
|
|
| 283 |
# Monkey patch json.dumps to use our safe version
|
| 284 |
json.dumps = safe_json_dumps
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
# --- Main Business Logic Functions for Flask ---
|
| 287 |
|
| 288 |
# ADD THIS NEW HELPER FUNCTION SOMEWHERE NEAR THE TOP OF THE FILE
|
|
|
|
| 283 |
# Monkey patch json.dumps to use our safe version
|
| 284 |
json.dumps = safe_json_dumps
|
| 285 |
|
| 286 |
+
def safe_firebase_data(obj):
|
| 287 |
+
"""
|
| 288 |
+
Safely prepare data for Firebase by ensuring all values are JSON-serializable
|
| 289 |
+
and Firebase-compatible.
|
| 290 |
+
"""
|
| 291 |
+
def clean_for_firebase(obj):
|
| 292 |
+
if obj is None:
|
| 293 |
+
return None
|
| 294 |
+
elif isinstance(obj, bool):
|
| 295 |
+
return obj
|
| 296 |
+
elif isinstance(obj, (int, float)):
|
| 297 |
+
return obj
|
| 298 |
+
elif isinstance(obj, str):
|
| 299 |
+
# Clean string for Firebase - remove null bytes and control characters
|
| 300 |
+
return ''.join(char for char in obj if ord(char) >= 32 or char in '\n\r\t')
|
| 301 |
+
elif isinstance(obj, (list, tuple)):
|
| 302 |
+
return [clean_for_firebase(item) for item in obj]
|
| 303 |
+
elif isinstance(obj, dict):
|
| 304 |
+
cleaned = {}
|
| 305 |
+
for key, value in obj.items():
|
| 306 |
+
# Firebase keys must be strings and can't contain certain characters
|
| 307 |
+
clean_key = str(key).replace('.', '_').replace('$', '_').replace('#', '_').replace('[', '_').replace(']', '_').replace('/', '_')
|
| 308 |
+
cleaned[clean_key] = clean_for_firebase(value)
|
| 309 |
+
return cleaned
|
| 310 |
+
else:
|
| 311 |
+
# Convert to string and clean
|
| 312 |
+
str_repr = str(obj)
|
| 313 |
+
return ''.join(char for char in str_repr if ord(char) >= 32 or char in '\n\r\t')
|
| 314 |
+
|
| 315 |
+
return clean_for_firebase(obj)
|
| 316 |
+
|
| 317 |
+
def concat_media(file_paths: List[str], output_path: Path):
|
| 318 |
+
valid_paths = [p for p in file_paths if Path(p).exists() and Path(p).stat().st_size > 100]
|
| 319 |
+
if not valid_paths: raise ValueError("No valid media files to concatenate.")
|
| 320 |
+
if len(valid_paths) == 1: import shutil; shutil.copy2(valid_paths[0], str(output_path)); return
|
| 321 |
+
list_file = output_path.with_suffix(".txt")
|
| 322 |
+
with open(list_file, 'w') as f:
|
| 323 |
+
for path in valid_paths: f.write(f"file '{Path(path).resolve()}'\n")
|
| 324 |
+
cmd = ["ffmpeg", "-y", "-f", "concat", "-safe", "0", "-i", str(list_file), "-c", "copy", str(output_path)]
|
| 325 |
+
try:
|
| 326 |
+
subprocess.run(cmd, check=True, capture_output=True, text=True)
|
| 327 |
+
finally:
|
| 328 |
+
list_file.unlink(missing_ok=True)
|
| 329 |
+
|
| 330 |
+
# Backward-compatible fix: Override json.dumps to handle non-serializable types
|
| 331 |
+
import json as _json
|
| 332 |
+
|
| 333 |
+
def safe_json_dumps(obj, indent=2, **kwargs):
|
| 334 |
+
"""Safely serialize object to JSON, handling non-serializable types."""
|
| 335 |
+
def json_serializer(obj):
|
| 336 |
+
if isinstance(obj, (bool, int, float, str, type(None))):
|
| 337 |
+
return obj
|
| 338 |
+
elif isinstance(obj, (list, tuple)):
|
| 339 |
+
return [json_serializer(item) for item in obj]
|
| 340 |
+
elif isinstance(obj, dict):
|
| 341 |
+
return {key: json_serializer(value) for key, value in obj.items()}
|
| 342 |
+
else:
|
| 343 |
+
# Convert non-serializable objects to string representation
|
| 344 |
+
return str(obj)
|
| 345 |
+
|
| 346 |
+
try:
|
| 347 |
+
return _json.dumps(json_serializer(obj), indent=indent, **kwargs)
|
| 348 |
+
except Exception as e:
|
| 349 |
+
logging.warning(f"JSON serialization failed: {e}")
|
| 350 |
+
return str(obj)
|
| 351 |
+
|
| 352 |
+
# Monkey patch json.dumps to use our safe version
|
| 353 |
+
json.dumps = safe_json_dumps
|
| 354 |
+
|
| 355 |
# --- Main Business Logic Functions for Flask ---
|
| 356 |
|
| 357 |
# ADD THIS NEW HELPER FUNCTION SOMEWHERE NEAR THE TOP OF THE FILE
|