#!/usr/bin/env python3
"""Fix crawler_admin.py and search_app.py to handle list-format daily_crawl_config.json"""
import re

def fix_file(filepath):
    with open(filepath, 'r') as f:
        content = f.read()
    
    # Find all `cfg = json.load(...)` patterns and add normalization after them
    # Pattern: cfg = json.load(...) followed by newline
    count = 0
    
    # Fix 1: Add import for types near the top if not present
    # Fix 2: After each `cfg = json.load(open(...))`, add normalization
    
    # Replace each `cfg = json.load` line with cfg = json.load line + normalization
    pattern = r'(cfg\s*=\s*(?:json|_json)\.(?:load|open)[^;]*?\))'
    
    def add_normalize(m):
        return m.group(0) + '\nif isinstance(cfg, list):\n    cfg = {"crawlers": cfg}'
    
    new_content = re.sub(pattern, add_normalize, content)
    
    if new_content != content:
        with open(filepath, 'w') as f:
            f.write(new_content)
        print(f"{filepath}: patched {len(re.findall(pattern, content))} occurrences")
    else:
        print(f"{filepath}: no changes needed")

fix_file("/root/crawler_admin.py")
fix_file("/root/search_app.py")
