下载地址:http://m.pan38.com/download.php?code=ENROLN 提取码:2819
代码功能说明:
设备连接与初始化:通过uiautomator2库连接Android设备,设置随机操作延迟模拟人类行为
广告识别系统:采用OpenCV模板匹配技术识别广告播放按钮和关闭按钮
防检测机制:包含随机滑动、不规则操作间隔、异常处理等设计
奖励收集功能:自动检测并点击金币奖励领取按钮
运行监控:记录广告观看次数,控制最大运行时间防止过度使用
import os
import time
import random
import subprocess
from PIL import Image
import cv2
import numpy as np
import uiautomator2 as u2
class KuaishouAutoAd:
def init(self):
self.device = u2.connect() # 自动连接设备
self.ad_watch_count = 0
self.max_runtime = 3600 # 最大运行时间(秒)
self.screenshot_dir = "screenshots"
self.setup_environment()
def setup_environment(self):
"""初始化运行环境"""
if not os.path.exists(self.screenshot_dir):
os.makedirs(self.screenshot_dir)
self.device.settings['operation_delay'] = (0, 1) # 随机操作延迟
self.device.implicitly_wait(10.0)
def start_kuaishou(self):
"""启动快手极速版"""
print("启动快手极速版...")
self.device.app_start("com.kuaishou.nebula")
time.sleep(random.uniform(5, 8))
def random_swipe(self):
"""随机滑动屏幕"""
width, height = self.device.window_size()
start_x = random.randint(width//4, width*3//4)
start_y = random.randint(height//4, height*3//4)
end_x = random.randint(width//4, width*3//4)
end_y = random.randint(height//4, height*3//4)
duration = random.uniform(0.3, 1.5)
self.device.swipe(start_x, start_y, end_x, end_y, duration)
def take_screenshot(self, filename):
"""截图并保存"""
screenshot = self.device.screenshot()
screenshot.save(f"{self.screenshot_dir}/{filename}.png")
return cv2.imread(f"{self.screenshot_dir}/{filename}.png")
def match_template(self, main_img, template_img, threshold=0.8):
"""模板匹配"""
result = cv2.matchTemplate(main_img, template_img, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= threshold:
return max_loc
return None
def watch_advertisement(self):
"""观看广告主逻辑"""
print(f"开始第{self.ad_watch_count+1}次广告观看...")
# 随机滑动进入广告
self.random_swipe()
time.sleep(random.uniform(3, 6))
# 尝试查找广告播放按钮
play_btn = self.device(resourceId="com.kuaishou.nebula:id/play_btn")
if play_btn.exists:
play_btn.click()
print("点击播放广告")
time.sleep(random.uniform(2, 4))
# 模拟观看广告30-40秒
watch_time = random.randint(30, 40)
print(f"广告观看中,预计耗时{watch_time}秒...")
# 随机微操作防止检测
for _ in range(watch_time//10):
self.random_swipe()
time.sleep(random.uniform(8, 12))
# 尝试关闭广告
close_btn = self.device(textContains="关闭|跳过|结束")
if close_btn.exists:
close_btn.click()
print("关闭广告")
else:
self.device.press("back")
self.ad_watch_count += 1
time.sleep(random.uniform(3, 7))
def collect_reward(self):
"""收集金币奖励"""
print("尝试收集金币奖励...")
reward_btn = self.device(textContains="领取奖励|金币奖励")
if reward_btn.exists:
reward_btn.click()
time.sleep(random.uniform(1, 3))
close_btn = self.device(description="关闭")
if close_btn.exists:
close_btn.click()
return True
return False
def run(self):
"""主运行循环"""
self.start_kuaishou()
start_time = time.time()
while time.time() - start_time < self.max_runtime:
try:
self.watch_advertisement()
if random.random() > 0.7: # 70%概率尝试收集奖励
self.collect_reward()
# 随机休息间隔
sleep_time = random.randint(5, 15)
print(f"等待{sleep_time}秒后继续...")
time.sleep(sleep_time)
except Exception as e:
print(f"发生错误: {e}")
self.device.app_stop("com.kuaishou.nebula")
time.sleep(10)
self.start_kuaishou()
print(f"任务完成,共观看广告{self.ad_watch_count}次")
if name == "main":
auto = KuaishouAutoAd()
auto.run()
iautomator2==2.16.24
opencv-python==4.9.0.80
pillow==10.1.0
numpy==1.26.4