From b1f02229a4c6795e9f451b4f642775c259259a0c Mon Sep 17 00:00:00 2001 From: wangzhengzhen Date: Tue, 7 Jan 2025 10:12:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/apps.py | 17 ++++++++++------- script/common.py | 21 +++++++++++++++++++++ script/devices.py | 7 +++++++ script/main.py | 8 +++++--- 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 script/common.py diff --git a/script/apps.py b/script/apps.py index 08bdb4d..9af0289 100644 --- a/script/apps.py +++ b/script/apps.py @@ -1,6 +1,9 @@ import random import time import ui +import common + +log = common.get_logger("apps") class Rule: def __init__(self): @@ -43,7 +46,7 @@ class Task: @classmethod def watch_video(cls, name, rule, device): - return cls(name, rule, lambda : device.swipe_up(120, 40)) + return cls(name, rule, lambda : device.swipe_up(150, 40)) @classmethod def read_book(cls, name, rule, device): @@ -54,9 +57,9 @@ class Task: return cls(name, rule, lambda : device.swipe_up(300, 300)) def start(self): - print("开始执行任务:{}".format(self.name)) + log.info("开始执行任务:{}".format(self.name)) _exec(self.rule, self.func, self.name) - print("任务{}执行完毕".format(self.name)) + log.info("任务{}执行完毕".format(self.name)) pass def _exec(rule, func = [], log_prefix = "执行任务"): @@ -68,7 +71,7 @@ def _exec(rule, func = [], log_prefix = "执行任务"): wait_time = getattr(rule, "interval") if factor > 0: wait_time = round(random.uniform(wait_time - factor, wait_time + factor), 2) - print("{}:{}/{} ,停留 {} 秒".format(log_prefix, i, num, wait_time)) + log.info("{}:{}/{} ,停留 {} 秒".format(log_prefix, i, num, wait_time)) if isinstance(func, list): for f in func: f() @@ -87,7 +90,7 @@ def _exec(rule, func = [], log_prefix = "执行任务"): wait_time = round(random.uniform(wait_time - factor, wait_time + factor), 2) if time_total + wait_time > duration: wait_time = round(duration - time_total, 2) - print("{}:{}/{} ,停留 {} 秒".format(log_prefix, time_total, duration, wait_time)) + log.info("{}:{}/{} ,停留 {} 秒".format(log_prefix, time_total, duration, wait_time)) func() time.sleep(wait_time) time_total = round(time_total + wait_time, 2) @@ -101,8 +104,8 @@ class ToutiaoLite(_App): self.device = device def open_treasure_box(action): - print("开宝箱...") - print("开宝箱完成...") + log.info("开宝箱...") + log.info("开宝箱完成...") # 看广告 def func_ad(self): diff --git a/script/common.py b/script/common.py new file mode 100644 index 0000000..c8853c6 --- /dev/null +++ b/script/common.py @@ -0,0 +1,21 @@ +import sys +import logging + +logger_level_relations = { + 'debug': logging.DEBUG, + 'info': logging.INFO, + 'warning': logging.WARNING, + 'error': logging.ERROR, + 'crit': logging.CRITICAL +} + +def get_logger(name : str, level : str = 'info'): + + logger = logging.getLogger(name) + logger.setLevel(logger_level_relations.get(level)) + ch = logging.StreamHandler(sys.stdout) + ch.setLevel(logger_level_relations.get(level)) + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + ch.setFormatter(formatter) + logger.addHandler(ch) + return logger \ No newline at end of file diff --git a/script/devices.py b/script/devices.py index 5910adc..888c173 100644 --- a/script/devices.py +++ b/script/devices.py @@ -1,5 +1,8 @@ import adb import random +import common + +log = common.get_logger("devices", "debug") class _Action: def __init__(self, id, scr_w, scr_h): @@ -37,6 +40,7 @@ class _Action: x2 = x1 + int(random.uniform(-10, 10)) y2 = y1 - dist + int(random.uniform(-10, 10)) time = speed - int(random.uniform(-10, 10)) + log.debug("x1: {} , y1: {} , x2: {} , y2: {} , time: {}".format(x1, y1, x2, y2, time)) self.operator.swipe(x1, y1, x2, y2, time) # 下滑 @@ -46,6 +50,7 @@ class _Action: x2 = x1 + int(random.uniform(-10, 10)) y2 = y1 + dist + int(random.uniform(-10, 10)) time = speed + int(random.uniform(-10, 10)) + log.debug("x1: {} , y1: {} , x2: {} , y2: {} , time: {}".format(x1, y1, x2, y2, time)) self.operator.swipe(x1, y1, x2, y2, time) # 左滑 @@ -55,6 +60,7 @@ class _Action: x2 = x1 - dist + int(random.uniform(-10, 10)) y2 = y1 + int(random.uniform(-10, 10)) time = speed + int(random.uniform(-10, 10)) + log.debug("x1: {} , y1: {} , x2: {} , y2: {} , time: {}".format(x1, y1, x2, y2, time)) self.operator.swipe(x1, y1, x2, y2, time) # 右滑 @@ -64,6 +70,7 @@ class _Action: x2 = x1 + dist + int(random.uniform(-10, 10)) y2 = y1 + int(random.uniform(-10, 10)) time = speed + int(random.uniform(-10, 10)) + log.debug("x1: {} , y1: {} , x2: {} , y2: {} , time: {}".format(x1, y1, x2, y2, time)) self.operator.swipe(x1, y1, x2, y2, time) # 点击 diff --git a/script/main.py b/script/main.py index a5a48ca..54cc1fd 100644 --- a/script/main.py +++ b/script/main.py @@ -2,9 +2,11 @@ from devices import WikoHi70m import apps import random import threading +import common def main(): + log = common.get_logger("main") try: # adb_cmd.mobile_unlock() @@ -14,8 +16,8 @@ def main(): # 普通任务 watchAdTask = apps.Task("连续看广告", apps.Rule.count(35, 45, 2), lambda : apps.ToutiaoLite(wikoHi70m).func_ad()) - watchVideoTask = apps.Task.watch_video("刷视频", apps.Rule.count(2000, 6, 2), wikoHi70m) - readBookTask = apps.Task.read_book("看书", apps.Rule.count(1000, 5, 2), wikoHi70m) + watchVideoTask = apps.Task.watch_video("刷视频", apps.Rule.count(1000, 6, 2), wikoHi70m) + readBookTask = apps.Task.read_book("看书", apps.Rule.count(2000, 5, 2), wikoHi70m) browseTask = apps.Task.browse("逛街", apps.Rule.time(60, 4, 2), wikoHi70m) # 窗口任务 @@ -46,7 +48,7 @@ def main(): # wikoHi70m.lock() except KeyboardInterrupt: - print("程序被用户中断") + log.info("程序被用户中断") # 多任务同步执行 def sync_task(task1, task2):