wx-ocr 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wx_ocr/utils.py ADDED
@@ -0,0 +1,161 @@
1
+ """
2
+ Utility functions for WeChat OCR.
3
+ """
4
+ import os
5
+ import winreg
6
+ from pathlib import Path
7
+ from typing import Optional, List
8
+
9
+
10
+ def find_wechat_install_path() -> Optional[str]:
11
+ """
12
+ Try to find WeChat installation path from Windows registry.
13
+
14
+ Returns:
15
+ WeChat installation path or None if not found
16
+ """
17
+ try:
18
+ # Try to read from registry
19
+ key_paths = [
20
+ r"SOFTWARE\WOW6432Node\Tencent\WeChat",
21
+ r"SOFTWARE\Tencent\WeChat",
22
+ ]
23
+
24
+ for key_path in key_paths:
25
+ try:
26
+ with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path) as key:
27
+ install_path, _ = winreg.QueryValueEx(key, "InstallPath")
28
+ if install_path and os.path.exists(install_path):
29
+ return install_path
30
+ except (FileNotFoundError, OSError):
31
+ continue
32
+
33
+ except Exception as e:
34
+ print(f"Error finding WeChat path: {e}")
35
+
36
+ return None
37
+
38
+
39
+ def find_wechat_ocr_path() -> Optional[str]:
40
+ """
41
+ Try to find WeChatOCR.exe path.
42
+
43
+ Returns:
44
+ WeChatOCR.exe path or None if not found
45
+ """
46
+ # Common locations
47
+ appdata = os.getenv("APPDATA")
48
+ if not appdata:
49
+ return None
50
+
51
+ wechat_plugin_base = Path(appdata) / "Tencent" / "WeChat" / "XPlugin" / "Plugins" / "WeChatOCR"
52
+
53
+ if not wechat_plugin_base.exists():
54
+ return None
55
+
56
+ # Find all version directories
57
+ version_dirs = [d for d in wechat_plugin_base.iterdir() if d.is_dir()]
58
+
59
+ for version_dir in sorted(version_dirs, reverse=True): # Try newest first
60
+ ocr_exe = version_dir / "extracted" / "WeChatOCR.exe"
61
+ if ocr_exe.exists():
62
+ return str(ocr_exe)
63
+
64
+ return None
65
+
66
+
67
+ def find_mmmojo_dll(wechat_path: str) -> Optional[str]:
68
+ """
69
+ Find mmmojo.dll or mmmojo_64.dll in WeChat installation directory.
70
+
71
+ Args:
72
+ wechat_path: WeChat installation directory
73
+
74
+ Returns:
75
+ Path to mmmojo dll or None if not found
76
+ """
77
+ import platform
78
+
79
+ wechat_dir = Path(wechat_path)
80
+
81
+ # Determine which dll to look for based on Python architecture
82
+ python_bit = platform.architecture()[0]
83
+ dll_name = "mmmojo_64.dll" if python_bit == "64bit" else "mmmojo.dll"
84
+
85
+ dll_path = wechat_dir / dll_name
86
+ if dll_path.exists():
87
+ return str(dll_path)
88
+
89
+ return None
90
+
91
+
92
+ def auto_find_wechat() -> dict:
93
+ """
94
+ Automatically find WeChat and WeChatOCR paths.
95
+
96
+ Returns:
97
+ Dictionary with 'wechat_dir' and 'wechat_ocr_dir' keys
98
+ """
99
+ result = {
100
+ "wechat_dir": None,
101
+ "wechat_ocr_dir": None,
102
+ }
103
+
104
+ # Find WeChat installation
105
+ wechat_dir = find_wechat_install_path()
106
+ if wechat_dir:
107
+ result["wechat_dir"] = wechat_dir
108
+ print(f"✓ Found WeChat: {wechat_dir}")
109
+ else:
110
+ print("✗ WeChat installation not found")
111
+
112
+ # Find WeChatOCR
113
+ wechat_ocr_dir = find_wechat_ocr_path()
114
+ if wechat_ocr_dir:
115
+ result["wechat_ocr_dir"] = wechat_ocr_dir
116
+ print(f"✓ Found WeChatOCR: {wechat_ocr_dir}")
117
+ else:
118
+ print("✗ WeChatOCR not found")
119
+
120
+ return result
121
+
122
+
123
+ def list_available_ocr_versions() -> List[str]:
124
+ """
125
+ List all available WeChatOCR versions.
126
+
127
+ Returns:
128
+ List of WeChatOCR.exe paths
129
+ """
130
+ appdata = os.getenv("APPDATA")
131
+ if not appdata:
132
+ return []
133
+
134
+ wechat_plugin_base = Path(appdata) / "Tencent" / "WeChat" / "XPlugin" / "Plugins" / "WeChatOCR"
135
+
136
+ if not wechat_plugin_base.exists():
137
+ return []
138
+
139
+ ocr_paths = []
140
+ for version_dir in wechat_plugin_base.iterdir():
141
+ if version_dir.is_dir():
142
+ ocr_exe = version_dir / "extracted" / "WeChatOCR.exe"
143
+ if ocr_exe.exists():
144
+ ocr_paths.append(str(ocr_exe))
145
+
146
+ return sorted(ocr_paths, reverse=True)
147
+
148
+
149
+ if __name__ == "__main__":
150
+ # Test the utility functions
151
+ print("Searching for WeChat and WeChatOCR...\n")
152
+
153
+ paths = auto_find_wechat()
154
+
155
+ print("\nAvailable WeChatOCR versions:")
156
+ versions = list_available_ocr_versions()
157
+ if versions:
158
+ for i, version in enumerate(versions, 1):
159
+ print(f" {i}. {version}")
160
+ else:
161
+ print(" None found")
Binary file