import json
import re

def recover_users_data(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8', errors='ignore') as f:
        content = f.read()

    pattern = r'"(\d+)":\s*\{\s*"points":\s*(\d+).*?\}'
    matches = re.finditer(pattern, content, re.DOTALL)
    
    recovered_dict = {}
    
    for match in matches:
        try:
            full_block = match.group(0)
            if not full_block.strip().endswith('}'):
                full_block += '}'
            
            user_json_str = "{" + full_block + "}"
            user_data = json.loads(user_json_str)
            
            for user_id, details in user_data.items():
                if user_id not in recovered_dict:
                    recovered_dict[user_id] = details
                else:
                    current_points = details.get('points', 0)
                    old_points = recovered_dict[user_id].get('points', 0)
                    if current_points >= old_points:
                        recovered_dict[user_id] = details
        except:
            continue

    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(recovered_dict, f, ensure_ascii=False, indent=4)

    return len(recovered_dict)

count = recover_users_data('full_raw_data.txt', 'users_fixed_v2.json')
print(f"Total Unique Users Recovered: {count}")
