微信自动转发文章链接!

import pyautogui
import time
import numpy as np
from PIL import Image

# 等待时间(秒),可以根据需要调整
delay = 1.5  # 您提供的 delay 参数

# 模板图片路径(需要替换为您的实际模板图片路径)
PUBLIC_TEMPLATE_PATH = "public.png"  # 确保这个文件存在且路径正确
FORWARD_TEMPLATE_PATH = "forward_option.png"  # 右键菜单中“转发”选项
MULTI_SELECT_TEMPLATE_PATH = "multi_select.png"  # 转发界面中的“多选”按钮
GROUP_CIRCLE_TEMPLATE_PATH = "group_circle.png"  # 群组列表中白色空白圈
SEND_BUTTON_TEMPLATE_PATH = "send_button.png"  # “分别发送”按钮

# 全局变量,跟踪已发送的群组(使用坐标作为标识)
sent_groups = set()


def find_and_click_template(template_path, confidence=0.8):
    time.sleep(delay)
    try:
        location = pyautogui.locateOnScreen(template_path, confidence=confidence)
        if location:
            left, top, width, height = location
            point = pyautogui.center((left, top, width, height))
            pyautogui.click(point)  # 默认左键点击,如果需要右键可改为 rightClick
            print(f"找到并点击 {template_path} 在位置 {point}")
            return point  # 返回点击的位置,用于跟踪已发送群组
        else:
            print(f"未找到 {template_path}")
            return None
    except pyautogui.ImageNotFoundException:
        print(f"{template_path} 未在屏幕上找到")
        return None


def find_and_right_click_template(template_path, confidence=0.8):
    time.sleep(delay)
    try:
        location = pyautogui.locateOnScreen(template_path, confidence=confidence)
        if location:
            left, top, width, height = location
            point = pyautogui.center((left, top, width, height))
            pyautogui.rightClick(point)  # 右键点击
            print(f"找到并右键点击 {template_path} 在位置 {point}")
            return True
        else:
            print(f"未找到 {template_path}")
            return False
    except pyautogui.ImageNotFoundException:
        print(f"{template_path} 未在屏幕上找到")
        return False


def find_and_click_with_scroll(template_path, confidence=0.8, max_scrolls=5):
    """在模板未找到时尝试滚动的函数"""
    for attempt in range(max_scrolls):
        point = find_and_click_template(template_path, confidence)
        if point:
            return point
        else:
            print(f"尝试第 {attempt + 1} 次滚动...")
            pyautogui.scroll(-300)  # 向下滚动300像素(负数表示向下滚动)
            time.sleep(0.5)  # 等待滚动后界面更新
    print(f"经过 {max_scrolls} 次滚动仍未找到 {template_path}")
    return None


def select_groups(num_groups=9):
    """选择指定数量的群组(点击白色空白圈),如果未找到则滚动,且跳过已发送的群组"""
    selected_count = 0
    max_scrolls = 5  # 最大滚动次数,可以根据需要调整
    scroll_count = 0

    while selected_count < num_groups and scroll_count < max_scrolls:
        point = find_and_click_with_scroll(GROUP_CIRCLE_TEMPLATE_PATH, confidence=0.7)
        if point:
            # 检查是否已发送过这个群组(使用坐标作为标识)
            if point not in sent_groups:
                sent_groups.add(point)  # 记录已发送的群组
                selected_count += 1
                time.sleep(0.5)  # 等待选择后界面更新
            else:
                print(f"跳过已发送的群组,位置: {point}")
        else:
            scroll_count += 1
            if scroll_count >= max_scrolls:
                print("达到最大滚动次数,未能选择足够群组。")
                break
            pyautogui.scroll(-300)  # 向下滚动300像素
            time.sleep(0.5)  # 等待滚动后界面更新

    return selected_count == num_groups


def main():
    # 循环执行20次
    for iteration in range(20):
        print(f"执行第 {iteration + 1} 次操作...")

        # 1. 找到并右键点击 public.png
        success_public = find_and_right_click_template(PUBLIC_TEMPLATE_PATH)
        if not success_public:
            print("操作失败,请检查模板图片或屏幕区域。")
            continue  # 如果失败,跳到下一次循环

        # 2. 等待右键菜单弹出后,点击“转发”选项
        time.sleep(1)  # 等待右键菜单出现,调整此值可能需要根据实际延迟
        success_forward = find_and_click_template(FORWARD_TEMPLATE_PATH)
        if not success_forward:
            print("点击“转发”失败,请检查模板图片或屏幕区域。")
            continue

        # 3. 等待转发界面加载,点击“多选”按钮
        time.sleep(1)  # 等待转发界面出现
        success_multi = find_and_click_template(MULTI_SELECT_TEMPLATE_PATH)
        if not success_multi:
            print("点击“多选”失败,请检查模板图片或屏幕区域。")
            continue

        # 4. 选择9个未发送的群组(点击白色空白圈),如果未找到则滚动
        time.sleep(1)  # 等待多选界面加载
        success_groups = select_groups(num_groups=9)
        if not success_groups:
            print("选择群组失败,请检查模板图片或屏幕区域。")
            continue

        # 5. 点击“分别发送”按钮
        time.sleep(1)  # 等待群组选择完成后
        success_send = find_and_click_template(SEND_BUTTON_TEMPLATE_PATH)
        if success_send:
            print(f"第 {iteration + 1} 次操作成功完成!")
        else:
            print("点击“分别发送”失败,请检查模板图片或屏幕区域。")

    print("所有操作完成!")


if __name__ == "__main__":
    # 导入 numpy 和 OpenCV(cv2)(虽然这里未直接使用,但保留以防扩展)
    import cv2

    # 提示用户准备好
    print("请确保微信窗口已打开且在正确的位置,按 Enter 继续...")
    input()

    # 运行脚本
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值