摘要
為了監控搗亂者、騷擾者、同行等人羣加入微信羣,我寫了一個監控,實時監控這個人有沒有偷偷混進羣,如果檢測到,就給你手機發送通知。
代碼
import uiautomation as automation
import requests
import time
def send_results(results, url):
payload = {"results": results}
try:
response = requests.post(url, json=payload)
if response.status_code == 200:
print("---檢測結果已發送至服務器---")
else:
print(f"發送失敗,狀態碼: {response.status_code}")
except requests.RequestException as e:
print(f"發送請求時發生錯誤: {e}")
def get_filtered_controls_at_depth(control, target_depth, filter_strings, bypass_strings, current_depth=0, detected_controls=None):
if detected_controls is None:
detected_controls = set()
results = []
try:
if current_depth == target_depth:
# 如果控件名稱包含任何繞過字符串,則跳過該控件
if any(bypass_string in control.Name for bypass_string in bypass_strings):
return results
# 如果控件名稱包含任何過濾字符串且該控件未被檢測過,則添加到結果中
if any(filter_string in control.Name for filter_string in filter_strings) and control.Name not in detected_controls:
# 只返回控件內容
results.append(control.Name)
detected_controls.add(control.Name)
except Exception as e:
print(f"處理控件信息時發生錯誤: {e}")
try:
children = control.GetChildren()
for child in children:
results.extend(get_filtered_controls_at_depth(child, target_depth, filter_strings, bypass_strings, current_depth + 1, detected_controls))
except Exception as e:
print(f"獲取子控件時發生錯誤: {e}")
return results
def monitor_chat_window(target_depth, filter_strings, bypass_strings, interval, url):
detected_controls = set()
while True:
try:
# 獲取名為 "ChatWnd" 的窗口
window = automation.WindowControl(ClassName="ChatWnd")
if window.Exists(0, 0):
results = get_filtered_controls_at_depth(window, target_depth, filter_strings, bypass_strings, detected_controls=detected_controls)
# 打印結果
if results:
for result in results:
print(result)
send_results(result, url)
else:
# 移除每次循環中輸出的 "持續為您監控中..."
pass
except Exception as e:
print(f"主循環中發生錯誤: {e}")
send_results(f"主循環中發生錯誤: {e}", url)
# 等待指定的時間間隔
time.sleep(interval)
if __name__ == "__main__":
# 配置參數
target_depth = 12 # 檢測的控件的深度(這裏是固定的檢測進羣記錄就是深度12別修改)
filter_strings = ["蔡徐坤"] # 要檢測的字符串
bypass_strings = ["移出了羣聊"] # 要繞過檢測的字符串
interval = 1 # 檢測的頻率(秒)
url = "https://xxxxxxxxx" # 接收監控結果的URL
try:
print("持續為您監控中...") # 只輸出一次
# 開始監控
monitor_chat_window(target_depth, filter_strings, bypass_strings, interval, url)
except KeyboardInterrupt:
print("檢測程序被中斷,正在退出...")
# 發送中斷信息到服務器
send_results("檢測程序被中斷", url)
print("程序已退出")
這段代碼的主要功能是通過監控名為 “ChatWnd” 的窗口,檢查控件的名稱是否包含特定的過濾字符串,並將檢測結果通過 HTTP POST 請求發送到指定的URL,其背後的原理是通過 Python uiautomation 這個庫去讀取微信電腦版界面的控件去解析到具體的信息。
如何使用
- 打開微信電腦版你要監控的羣單獨拉出來
- 在代碼中的【要檢測的字符串】中配置你要檢測的微信用户暱稱
- 配置接收通知的URL
- CMD運行python程序即可開始檢測
- 整個過程請保持你要監控的羣是不關閉的狀態(最小化是可以的)
👇打開微信電腦版你要監控的羣單獨拉出來
👇CMD運行python程序即可開始檢測
👇接收到通知
本文作者
TANKING