import json, sys, os

path = sys.argv[1]
fixed = []
with open(path) as f:
    for line in f:
        item = json.loads(line.strip())
        new = {
            "title": item["title"],
            "page_url": item["url"],
            "content": item["content"],
            "publish_date": item["date"],
            "site_name": item["site_name"],
            "attachments": item["attachments"] if isinstance(item["attachments"], list) else json.loads(item["attachments"]) if item["attachments"] else []
        }
        fixed.append(new)

with open(path, "w") as f:
    for item in fixed:
        f.write(json.dumps(item, ensure_ascii=False) + "\n")

print(f"Fixed {len(fixed)} items -> saved to {path}")
