diff --git a/scripts/wechat_agent.py b/scripts/wechat_agent.py index 5f855a3..8499570 100644 --- a/scripts/wechat_agent.py +++ b/scripts/wechat_agent.py @@ -53,13 +53,27 @@ def get_db_handle(): r = wxpost("/api/getDBInfo", timeout=10) dbs = r.get("data") or [] # WeChat 3.9.5.81+: messages stored in MSG0.db, MSG1.db, etc. - # Older versions: messages in MicroMsg.db's MSG table + # Also check ChatMsg.db (has ChatMsg table with different schema). + # Prefer MSG*.db over MicroMsg.db (MicroMsg.db has "Msg" in name but no MSG table in new versions). + candidate = None for db in dbs: dbname = db.get("databaseName", "") - if "MSG" in dbname or "Msg" in dbname: - db_handle_cache = db.get("handle") - log(f"History DB: {dbname} handle={db_handle_cache}") - return db_handle_cache + # Prefer MSG0.db/MSG1.db over MicroMsg.db + if dbname.upper().startswith("MSG") and dbname.upper().endswith(".DB"): + candidate = db.get("handle") + log(f"History DB: {dbname} handle={candidate}") + break + # Fallback: check if any table is named MSG + for t in (db.get("tables") or []): + if t.get("tableName") == "MSG": + candidate = db.get("handle") + log(f"History DB: {dbname} handle={candidate}") + break + if candidate: + break + if candidate: + db_handle_cache = candidate + return candidate log("History DB handle: NOT FOUND") return None