Skip to content

Commit

Permalink
完成基础框架
Browse files Browse the repository at this point in the history
  • Loading branch information
smallevilbeast committed Aug 23, 2022
0 parents commit 311a281
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# If you need to exclude files such as those generated by an IDE, use
# $GIT_DIR/info/exclude or the mm.excludesFile configuration variable as
# described in https://git-scm.com/docs/gitignore

*.egg-info
*.pot
*.py[co]
.tox/
__pycache__
MANIFEST
dist/
docs/_build/
docs/locale/
node_modules/
tests/coverage_html/
tests/.coverage
build/
tests/report/
venv
.idea
log/
*.c
main.spec
build/
config.ini
ntchat/wc/*.pyd
7 changes: 7 additions & 0 deletions ntchat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .conf import VERSION
from .core.wechat import WeChat
from .wc import wcprobe

__version__ = VERSION

exit_ = wcprobe.exit
1 change: 1 addition & 0 deletions ntchat/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION = '0.1.0'
Empty file added ntchat/const/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions ntchat/const/wx_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

MT_READY_MSG = 11024
MT_USER_LOGIN_MSG = 11025
MT_USER_LOGOUT_MSG = 11026
MT_GET_SELF_MSG = 11028
MT_GET_CONTACTS_MSG = 11030
MT_GET_ROOMS_MSG = 11031
MT_GET_ROOM_MEMBERS_MSG = 11032
MT_GET_CONTACT_DETAIL_MSG = 11034

# 发送消息
MT_SEND_TEXT_MSG = 11036
MT_SEND_ROOM_AT_MSG = 11037
MT_SEND_CARD_MSG = 11038
MT_SEND_LINK_MSG = 11039
MT_SEND_IMAGE_MSG = 11040
MT_SEND_FILE_MSG = 11041
MT_SEND_VIDEO_MSG = 11042
MT_SEND_GIF_MSG = 11043

# 接收消息类
MT_RECV_TEXT_MSG = 11046
MT_RECV_PICTURE_MSG = 11047
MT_RECV_VOICE_MSG = 11048
MT_RECV_FRIEND_MSG = 11049
MT_RECV_CARD_MSG = 11050
MT_RECV_VIDEO_MSG = 11051
MT_RECV_EMOJI_MSG = 11052
MT_RECV_LOCATION_MSG = 11053
MT_RECV_LINK_MSG = 11054
MT_RECV_FILE_MSG = 11055
MT_RECV_MINIAPP_MSG = 11056
MT_RECV_WCPAY_MSG = 11057
MT_RECV_SYSTEM_MSG = 11058
MT_RECV_REVOKE_MSG = 11059
MT_RECV_OTHER_MSG = 11060
MT_RECV_OTHER_APP_MSG = 11061
Empty file added ntchat/core/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions ntchat/core/mgr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import json
import os.path
from ntchat.wc import wcprobe
from ntchat.utils.xdg import get_helper_file
from ntchat.exception import WeChatVersionNotMatchError, WeChatBindError
from ntchat.utils.singleton import Singleton
from ntchat.const import wx_type
from ntchat.utils.logger import get_logger

log = get_logger("WeChatManager")


class WeChatMgr(metaclass=Singleton):
__instance_list = []
__instance_map = {}

def __init__(self, wechat_exe_path=None, wechat_version=None):
self.set_wechat_exe_path(wechat_exe_path, wechat_version)

# init callbacks
wcprobe.init_callback(self.__on_accept, self.__on_recv, self.__on_close)

def set_wechat_exe_path(self, wechat_exe_path=None, wechat_version=None):
exe_path = ''
if wechat_exe_path is not None:
exe_path = wechat_exe_path

if wechat_version is None:
version = wcprobe.get_install_wechat_version()
else:
version = wechat_version

helper_file = get_helper_file(version)
if not os.path.exists(helper_file):
raise WeChatVersionNotMatchError()

log.info("initialize wechat, version: %s", version)

# init env
wcprobe.init_env(helper_file, exe_path)

def append_instance(self, instance):
log.debug("new wechat instance")
self.__instance_list.append(instance)

def __bind_wechat(self, client_id, pid):
bind_instance = None
if client_id not in self.__instance_map:
for instance in self.__instance_list:
if instance.pid == pid:
instance.client_id = client_id
instance.status = True
self.__instance_map[client_id] = instance
bind_instance = instance
break
if bind_instance is None:
raise WeChatBindError()
self.__instance_list.remove(bind_instance)

def __on_accept(self, client_id):
log.debug("accept client_id: %d", client_id)

def __on_recv(self, client_id, data):
message = json.loads(data)
if message["type"] == wx_type.MT_READY_MSG:
self.__bind_wechat(client_id, message["data"]["pid"])
else:
self.__instance_map[client_id].on_recv(message)

def __on_close(self, client_id):
log.debug("close client_id: %d", client_id)
if client_id in self.__instance_map:
self.__instance_map[client_id].login_status = False
self.__instance_map[client_id].status = False
Loading

0 comments on commit 311a281

Please sign in to comment.