from datetime import datetime import json import hashlib from typing import Callable SALT = b"Lt2N5xgjJOqRsT5qVt7wWYw6SqOPZDI7" def salted_hash_userid(player: dict): uid = player["userId"] hash_uid = hashlib.sha256(f"{uid}".encode("utf-8") + SALT) player["userId"] = hash_uid.hexdigest()[:16] def clean_player(player: dict): player.pop("isLogin") player.pop("lastLoginDate") player.pop("lastPlayDate") player.pop("isNetMember") player.pop("dailyBonusDate") player.pop("banState") player.pop("nameplateId") player.pop("trophyId") def record_time(*, _: list[datetime] = []): last_time = _ if not last_time: last_time.append(datetime.now()) else: new = datetime.now() diff = (new - last_time.pop()).total_seconds() last_time.append(new) return diff def process( clean_fields: Callable[[dict], None], input_file: str, output_file: str, ): record_time() with open(input_file, "r", encoding="utf-8") as f: data = json.load(f) print(f"loaded, cost {record_time():.2f}s") record_time() for entry in data: salted_hash_userid(entry) clean_fields(entry) print(f"processed, cost {record_time():.2f}s") record_time() with open(output_file, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False) print(f"written out, cost {record_time():.2f}s") def main(): process( clean_player, "players.json", "players_pub.json", ) process( lambda d: d["userRating"].pop("rating"), "b50.json", "b50_pub.json", ) if __name__ == "__main__": main()