下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:2491
该实现包含三个核心模块:主监控程序、数据库记录模块和配置模块。主程序使用itchat监听微信消息,结合uiautomator2实现安卓设备控制数据库模块记录抢红包历史数据,配置模块提供灵活的参数调整
import itchat
import time
import random
import uiautomator2 as u2
from threading import Thread
class RedPacketHunter:
def init(self):
self.d = u2.connect()
self.msg_filter = ["收到红包", "红包"]
self.running = False
self.statistics = {
'total': 0,
'success': 0,
'failed': 0,
'amount': 0.0
}
def _click_redpacket(self):
try:
if self.d(text='微信红包').exists:
self.d(text='微信红包').click()
time.sleep(0.5)
if self.d(description='开').exists:
self.d(description='开').click()
return True
except Exception as e:
print(f"点击红包异常: {str(e)}")
return False
def _monitor_chat(self):
while self.running:
try:
if self._click_redpacket():
self.statistics['success'] += 1
print(f"成功抢到红包! 累计: {self.statistics['success']}")
time.sleep(0.3)
except Exception as e:
print(f"监控异常: {str(e)}")
time.sleep(1)
def start(self):
self.running = True
Thread(target=self._monitor_chat, daemon=True).start()
print("红包监控已启动...")
def stop(self):
self.running = False
print("红包监控已停止")
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
if any(keyword in msg.text for keyword in hunter.msg_filter):
print(f"检测到红包消息: {msg.text}")
hunter._click_redpacket()
if name == 'main':
hunter = RedPacketHunter()
itchat.auto_login(hotReload=True)
hunter.start()
itchat.run()