以前使用过wcferry 库,初步就想使用wcferry 库接收朋友圈消息,然后使用回调函数转发朋友圈消息,解析后保存到本地,再使用Flask解析在浏览器,可以按时间时间线排序,也可以按WX查看指定WX朋友的朋友圈。代码如下,但是目前有问题是,使用wcferry库无法持续获取此时和四天前的朋友圈消息,原因还没找到。wcferry HOOK微信转发朋友圈信息代码如下:
1.首先安装python3.11(压缩包里有),使用 pip install wcferry==39.3.3
2.下载指定版本微信(附件)
[Python] 纯文本查看 复制代码import json
import logging
import os
from datetime import datetime
from threading import Thread
from typing import Any
from wcferry import Wcf, WxMsg
import xml.etree.ElementTree as ET
from pydantic import BaseModel
from queue import Empty
class Msg(BaseModel):
id: int
ts: int
sign: str
type: int
xml: str
sender: str
roomid: str
content: str
thumb: str
extra: str
is_at: bool
is_self: bool
is_group: bool
__version__ = "39.2.4.0"
class GetWxmsg:
def __init__(
self,
cb: str,
** extra: Any
) -> None:
super().__init__(**extra)
self.LOG = logging.getLogger(__name__)
self.LOG.info(f"wcfhttp version: {__version__}")
self.wcf = Wcf
self.wcf.keep_running(self)
self.contact = None
self.pyq_content = { }
self._set_cb(cb)
def parse_xml(self, xml_file):
tree = ET.parse(xml_file.encode('utf-8'))
root = tree.getroot()
# 处理和赋值变量 weappInfo 为视频
wxid = root.find('username').text # 由WXID 显示使用备注信息
creat_time = root.find('createTime').text
createTime = datetime.fromtimestamp(creat_time)
# 格式化输出为月日时格式
formatted_date = createTime.strftime("%m月%d日%H时")
timeline_data = {
'id': root.find('id').text,
'username': wxid,
'createTime': creat_time,
'date_time': formatted_date,
'contentDesc': root.find('contentDesc').text,
'nikename': self.contact.get(wxid),
'mediaList': []
}
for media in root.findall('ContentObject/mediaList/media'):
media_data = {
'id': media.find('id').text,
'type': media.find('type').text,
'title': media.find('title').text,
'description': media.find('description').text,
'url': media.find('url').text,
'thumb': media.find('thumb').text,
'videoDuration': media.find('videoDuration').text,
'size': media.find('size').text,
'enc': media.find('enc').text
}
timeline_data['mediaList'].append(media_data)
self.pyq_content[timeline_data.get('createTime')] = timeline_data
self.save_xml(self.pyq_content)
@staticmethod
def save_xml(pyq_content):
if os.path.exists('pyq_data.json'):
with open('pyq_data.json', 'w', encoding='utf-8') as f:
data = json.load(f)
else:
data = { }
merge_dict = {**data, **pyq_content}
try:
with open('pyq_data.json', 'w', encoding='utf-8') as f:
json.dump(merge_dict, f, indent=4)
except Exception as e:
print(e)
def _forward_msg(self, msg: WxMsg):
if not self.cb:
self.LOG.error("No callback URL set")
return
data = {
"id": msg.id,
"ts": msg.ts,
"sign": msg.sign,
"type": msg.type,
"xml": msg.xml,
"sender": msg.sender,
"roomid": msg.roomid,
"content": msg.content,
"thumb": msg.thumb,
"extra": msg.extra,
"is_at": msg.is_at(self.wcf.self_wxid),
"is_self": msg.from_self(),
"is_group": msg.from_group(),
}
try:
rsp = self.parse_xml(data.get('content'))
except Exception as e:
self.LOG.error(f"消息解析: {e}")
def _set_cb(self, cb: str):
self.cb = cb
self.LOG.info(f"消息转发: {cb}" if cb else "没有设置转发,打印消息")
self.wcf.enable_receiving_msg(pyq=True) # 同时允许接收朋友圈消息
# Start the message receiving thread only if not already started
if not hasattr(self, '_message_thread'):
def callback(wcf: Wcf):
while wcf.is_receiving_msg():
try:
msg = wcf.get_msg()
if self.cb:
self.LOG.info(f"收到消息,转发至回调:{msg}")
self._forward_msg(msg)
else:
print(f"收到消息:{msg}")
except Empty:
continue # Empty message
except Exception as e:
self.LOG.error(f"Receiving message error: {e}")
self._message_thread = Thread(target=callback, name="GetMessage", args=(self.wcf,), daemon=True)
self._message_thread.start()
代码比较垃圾,大佬见谅。
目前存在朋友圈消息不完整的问题,过几天准备在研究研究WechatMoments直接导出微信朋友圈的方法,直接解析生成HTML文件展示。
需要的附件如下https://share.weiyun.com/wy0HAUMg 解压密码6666