def push_my_saves(saves_name, package): save_from = './saves/' + saves_name ''' save_from = './saves/save_common' if test_id == Cases.TUTORIAL_1: save_from = './saves/save_tutorial_1' if test_id == Cases.TUTORIAL_2: save_from = './saves/save_tutorial_2' if test_id == Cases.TUTORIAL_3: save_from = './saves/save_tutorial_3' # if test_id == Cases.TUTORIAL_4: save_from = './saves/save_tutorial_4' if test_id == Cases.TUTORIAL_5: save_from = './saves/save_tutorial_5' if test_id == Cases.TUTORIAL_6: save_from = './saves/save_tutorial_6' if test_id == Cases.TUTORIAL_7: save_from = './saves/save_tutorial_7' if test_id == Cases.TUTORIAL_8: save_from = './saves/save_tutorial_8' if test_id == Cases.TUTORIAL_9: save_from = './saves/save_tutorial_9' if test_id == Cases.TUTORIAL_10: save_from = './saves/save_tutorial_10' ''' clear_app(package) full_path_to = 'sdcard/Android/data/' + package shell('mkdir ' + full_path_to) full_path_to = 'sdcard/Android/data/' + package + "/files" shell('mkdir ' + full_path_to) shell('mkdir ' + full_path_to + '/2020-04-09-17-10-42') shell('mkdir ' + full_path_to + '/Settings') a = Android() a.adb.push(save_from + '/events.tsf', full_path_to + '/2020-04-09-17-10-42/') a.adb.push(save_from + '/profile.tsf', full_path_to + '/2020-04-09-17-10-42/') a.adb.push('./saves/Settings/settings.tsf', full_path_to + '/Settings/') a.adb.push('./saves/nw_save10.save', full_path_to + '/')
def test(self): DeviceNum = 'APU0215B25001477' device = Android(DeviceNum) poco = AndroidUiautomationPoco(device) poco.swipe([0.4, 0.8], [0.4, 0.5], duration=0.3) pass
def __init__(self): # 通过系统时间自动计算时间间隔 a = str(datetime.now()) hour = a.split(' ')[-1].split(':')[0] num = int(hour) / 3 num = int(num) * 3 if num == 0: # 对于凌晨 0 点的判断 # 时间判断部分 date = datetime.now() - timedelta(days=1) news_start_time = str(date).split(' ')[0] yesterday = datetime.now() - timedelta(days=1) # 昨天时间 yesterday = str(yesterday).split(' ')[0] else: # 时间判断部分 date = datetime.now() - timedelta(days=0) news_start_time = str(date).split(' ')[0] yesterday = datetime.now() - timedelta(days=0) # 昨天时间 yesterday = str(yesterday).split(' ')[0] print('爬取时间段:{}到{}'.format(news_start_time, yesterday)) self.start_time = news_start_time # self.start_time = '2019-09-09' # 定义结束时间 y-m-d 离现在时间近 self.end_time = yesterday # end_time = '2019-03-22' self.phone_name = device_name_list[0]['device_id'] self.device_num = device_name_list[0]['device_num'] print(self.device_num) self.device_1 = Android(self.device_num) self.poco = AndroidUiautomationPoco(self.device_1, deuse_airtest_input=True, screenshot_each_action=False) self.keyword_list = ['奔驰 C级', '奔驰 GLA', '奔驰 S级'] # '宝马 X1', '宝马 X2','宝马 3系','宝马 7系', '宝马 X5', '宝马 X7',, '奔驰 GLE' self.size = self.poco.get_screen_size() self.is_break = False self.same_num = 0 # 表示评论数相同的计数,用来判断视频翻页是否到底 self.up_title = '' pool = redis.ConnectionPool(host='127.0.0.1') # 实现一个Redis连接池 self.redis_example = redis.Redis(connection_pool=pool) self.zhu_num = 0 # 视频的翻页数,主要用来断点续爬,多少个视频之后开始采集评论 self.is_first = True # 是否是第一次启动的第一个关键词,主要用来断点续爬 self.keyword = ''
def switch_other_work(self, work_string): # 切换到企业,返回当前页面的活动, poco = self.get_poco() poco(text="其他企业").wait_for_appearance(20) poco(text="其他企业").click() poco(text=work_string).wait_for_appearance(20) if not poco(text=work_string).parent().sibling('com.tencent.wework:id/bim').exists(): poco(text=work_string).click() else: poco("com.tencent.wework:id/hxb").click() return Android().get_top_activity()[1]
def generatePoco(sn=None, use_airtest_input=True, screenshot_each_action=False): if sn is None: dev = None else: dev = Android(sn) #指定sn选择目标设备 poco = AndroidUiautomationPoco( device=dev, use_airtest_input=use_airtest_input, screenshot_each_action=screenshot_each_action) return poco
def __init_airtest(self): """ 初始化Airtest :return: """ device_1 = Android('822QEDTL225T7') # device_1 = Android('emulator-5554') connect_device("android:///") self.poco = AndroidUiautomationPoco(device_1, screenshot_each_action=False) auto_setup(__file__)
def poco_screenshots(self, p_value, save_as_path): # 元素截图 image_path = "d:\\screen.png" snapshot(image_path) # 将当前截图保存到image_path img = Image.open(image_path) # 打开图片:image_path # 获取手机分辨率 app_info = Android().get_display_info() # x,y 为屏幕分辨率 x = int(app_info['width']) y = int(app_info['height']) a, b, c, d = self.poco(p_value).get_bounds() cropped = img.crop( (x * d, y * a, x * b, y * c)) # (left, upper, right, lower) if '.png' not in save_as_path: save_as_path = os.path.join(save_as_path, 'pic.png') cropped.save(save_as_path) os.remove(image_path) # 删除原图 return save_as_path
from utils.file_utils import * from utils.string_utils import * from queue import Queue import datetime from comments import generate_a_comment from airtest.core.android import Android from airtest_utils import * from utils.device_utils import * from utils.norm_utils import current_time # 应用包名和启动Activity package_name = 'com.songheng.eastnews' activity = 'com.oa.eastfirst.activity.WelcomeActivity' device_1 = Android('c54613d') poco = AndroidUiautomationPoco(device_1, use_airtest_input=True, screenshot_each_action=False) auto_setup(__file__) # 收益来源 # 0.顶部的时长领取金币 # 1.任务:包含签到、 # 1.收益 # 2.阅读新闻 # 3.评论 class DongFangTouTiao(object):
with open(image, 'rb') as fp: image = fp.read() elif not isinstance(image, bytes): return result = client.basicGeneral(image) # print(result) # {'words_result': [{'words': '109382'}], 'log_id': 1347901057883176960, 'words_result_num': 1} if 'words_result' in result: return '\n'.join([w['words'] for w in result['words_result']]) from airtest.core.settings import Settings as ST # ST.CVSTRATEGY = ["surf", "tpl"] dev = Android() while True: s = dev.snapshot() # pos = Template('../item/jin-sum.png').match_in(s) # info = dev.get_display_info() # x = int(info['width'] / 2) # y = int(info['height'] / 2) # dx = random.randint(-500, 500) # dy = random.randint(-500, 500) # # print('%d,%d %d,%d' % (x, y, x + dx, y + dy)) # dev.swipe([x, y], [x + dx, y + dy]) pos = Template('../item/res-sum.png').match_in(s) if pos:
class DouYin(): ''' 操作模拟器,操控软件 ''' def __init__(self): # 通过系统时间自动计算时间间隔 a = str(datetime.now()) hour = a.split(' ')[-1].split(':')[0] num = int(hour) / 3 num = int(num) * 3 if num == 0: # 对于凌晨 0 点的判断 # 时间判断部分 date = datetime.now() - timedelta(days=1) news_start_time = str(date).split(' ')[0] yesterday = datetime.now() - timedelta(days=1) # 昨天时间 yesterday = str(yesterday).split(' ')[0] else: # 时间判断部分 date = datetime.now() - timedelta(days=0) news_start_time = str(date).split(' ')[0] yesterday = datetime.now() - timedelta(days=0) # 昨天时间 yesterday = str(yesterday).split(' ')[0] print('爬取时间段:{}到{}'.format(news_start_time, yesterday)) self.start_time = news_start_time # self.start_time = '2019-09-09' # 定义结束时间 y-m-d 离现在时间近 self.end_time = yesterday # end_time = '2019-03-22' self.phone_name = device_name_list[0]['device_id'] self.device_num = device_name_list[0]['device_num'] print(self.device_num) self.device_1 = Android(self.device_num) self.poco = AndroidUiautomationPoco(self.device_1, deuse_airtest_input=True, screenshot_each_action=False) self.keyword_list = ['奔驰 C级', '奔驰 GLA', '奔驰 S级'] # '宝马 X1', '宝马 X2','宝马 3系','宝马 7系', '宝马 X5', '宝马 X7',, '奔驰 GLE' self.size = self.poco.get_screen_size() self.is_break = False self.same_num = 0 # 表示评论数相同的计数,用来判断视频翻页是否到底 self.up_title = '' pool = redis.ConnectionPool(host='127.0.0.1') # 实现一个Redis连接池 self.redis_example = redis.Redis(connection_pool=pool) self.zhu_num = 0 # 视频的翻页数,主要用来断点续爬,多少个视频之后开始采集评论 self.is_first = True # 是否是第一次启动的第一个关键词,主要用来断点续爬 self.keyword = '' def hua(self): ''' 评论滑动 :return: ''' # print(size) x = self.size[0] # 获取屏幕宽 y = self.size[1] self.poco.swipe([(1 / 2) * x / x, (4 / 5) * y / y], [(1 / 2) * x / x, (1 / 4) * y / y], duration=0.2) def first_hua(self): ''' 防止评论输入模式滑动 :return: ''' x = self.size[0] # 获取屏幕宽 y = self.size[1] # # 向上滑 self.poco.swipe([(1 / 2) * x / x, (3 / 4) * y / y], [(1 / 2) * x / x, (1 / 6) * y / y], duration=0.1) def hua_zhuping(self): ''' 视频列表页滑动 :return: ''' # 获取屏幕的高 x = self.size[0] # 获取屏幕宽 y = self.size[1] # 向上滑动 # self.poco.swipe([(1 / 2) * x / x, (4 / 7) * y / y], [(1 / 2) * x / x, (3 / 7) * y / y], duration=0.1) self.poco.swipe([(1 / 2) * x / x, (5 / 6) * y / y], [(1 / 2) * x / x, (1 / 6) * y / y], duration=0.35) time.sleep(0.1) def hua_up(self): ''' 视频列表页滑动 :return: ''' # # 获取屏幕的高 # x = self.size[0] # # 获取屏幕宽 # y = self.size[1] # # 向上滑动 # self.poco.swipe([(1 / 2) * x / x, (4 / 7) * y / y], [(1 / 2) * x / x, (3 / 7) * y / y], duration=0.1) self.poco.swipe([0.5, 0.6], [0.5, 0.8], duration=0.2) time.sleep(0.1) def is_chinese(self, uchar): """判断一串字符中是否有汉字""" for child in uchar: if child >= u'\u4e00' and child <= u'\u9fa5': return True else: continue def main_work(self): """ 这里进行滑动的逻辑操作 :return: """ for i in range(300): print(i) souye = self.poco(text='首页') if souye: return print('进行一次主页滑动') self.hua_zhuping() time.sleep(0.5) if self.poco(text='暂时没有更多了'): break # time.sleep(5) # self.device_1.keyevent('KEYCODE_BACK') # time.sleep(5) # self.device_1.keyevent('KEYCODE_BACK') # time.sleep(5) # self.device_1.keyevent('KEYCODE_BACK') # time.sleep(5) # self.device_1.keyevent('KEYCODE_BACK') # time.sleep(5) # self.device_1.keyevent('KEYCODE_BACK') print('目前返回到抖音首页.....') time.sleep(5) def input_keyword(self, keyword, num): """ 这里进行点击输入框,输入文本操作,并搜索 """ # try: # print('点击输入文本框-1') # search_btn = self.poco(name='com.ss.android.ugc.aweme:id/aex')[0] # search_btn.click() # time.sleep(1) # except: # print('点击输入文本框-2') # self.poco(name='com.ss.android.ugc.aweme:id/aex').click() # time.sleep(1) if num > 0: self.poco(name='com.ss.android.ugc.aweme:id/a5j').click() else: self.poco(type='android.widget.EditText').click() print('输入搜索关键词') self.poco(type='android.widget.EditText').set_text(keyword) time.sleep(1) print('点击搜索按钮') self.poco(text='搜索').click() time.sleep(2) # self.poco(text='视频').click() # time.sleep(1) # self.poco.swipe([0.3, 0.3], [0.3, 0.3], duration=0.2) time.sleep(15) def time_change(self, str_time): """ 时间可是转换, 将‘分钟前’,‘小时前’,‘昨天’,‘前天’, '天前',转换成标准时间格式Y-m-d h:m:s :param str_time: :return: """ if '秒' in str_time: get_time = str(datetime.now()).split('.')[0] return get_time elif '分钟' in str_time: get_time_num = re.search('\d{1,2}', str_time).group(0) get_time_num = int(get_time_num) * 60 print(get_time_num) int_time = int(str(time.time()).split('.')[0]) - get_time_num # #转换成localtime time_local = time.localtime(float(int_time)) # 转换成新的时间格式(2016-05-05 20:28:54) dt = time.strftime("%Y-%m-%d", time_local) # "%Y-%m-%d %H:%M:%S" return dt elif '小时' in str_time: get_time_num = re.search('\d{1,2}', str_time).group(0) get_time_num = int(get_time_num) * 60 * 60 print(get_time_num) int_time = int(str(time.time()).split('.')[0]) - get_time_num # #转换成localtime time_local = time.localtime(float(int_time)) # 转换成新的时间格式(2016-05-05 20:28:54) dt = time.strftime("%Y-%m-%d", time_local) # "%Y-%m-%d %H:%M:%S" return dt elif '昨天' in str_time: try: part_time = str_time.split(' ')[1] yesterday = datetime.now() - timedelta(days=1) # 昨天时间 yesterday = str(yesterday).split(' ')[0] except: yesterday = datetime.now() - timedelta(days=1) # 昨天时间 yesterday = str(yesterday).split(' ')[0] return yesterday elif '前天' in str_time: part_time = str_time.split(' ')[1] two_days_ago = datetime.now() - timedelta(days=2) # 昨天时间 two_days_ago = str(two_days_ago).split( ' ')[0] + ' ' + part_time.replace('点', ':').replace('分', '') return two_days_ago elif '天前' in str_time: part_time = str_time.split('天前')[0] two_days_ago = datetime.now() - timedelta( days=int(part_time)) # 昨天时间 two_days_ago = str(two_days_ago).split(' ')[0] return two_days_ago elif '年' in str_time: str_time = str_time.replace('年', '-').replace('月', '-').replace('日', '') return str_time else: # str_time = '2019-' + str_time.replace('月', '-').replace('日', '') return str_time def run(self): self.homing() # if not self.poco(text='首页'): # self.device_1.shell('input keyevent 26') # time.sleep(1) # self.poco.swipe([0.5, 0.9], [0.5, 0.4], duration=0.3) # time.sleep(5) # try: # self.poco(name='com.ss.android.ugc.aweme:id/ap1').click() # print('点击搜索按钮-1') # time.sleep(1) # except: # print(traceback.format_exc()) # print('点击搜索按钮-2') # self.poco(name='com.ss.android.ugc.aweme:id/agt').click() # time.sleep(1) keyword_list = eval(self.redis_example.get('keyword_list')) print(type(keyword_list)) # for keyword in self.keyword_list: for i in range(1000): # 通过将关键词存入rides中,然后各个爬虫实例来取 # self.homing() time.sleep(5) keyword_index = int(self.redis_example.get('keyword_index')) if keyword_index > len(keyword_list): # 判断关键词是否爬取完毕 print('关键词爬取完毕......,最后一个爬取关键词为:', self.keyword) break self.keyword = keyword_list[keyword_index] print('获得关键词:', self.keyword) keyword_index += 1 self.redis_example.set('keyword_index', keyword_index) self.redis_example.set(self.phone_name, self.keyword) # time.sleep(30) print(111111, self.redis_example.get(self.phone_name).decode('utf8')) # try: if i == 0: self.poco(desc='搜索').click() # except: # time.sleep(10) # self.device_1.keyevent('KEYCODE_BACK') # time.sleep(10) # self.poco(desc='搜索').click() time.sleep(10) self.input_keyword(self.keyword, i) # 输入关键词 self.main_work() # 活动操作 else: self.input_keyword(self.keyword, i) # 输入关键词 self.main_work() # 活动操作 def homing(self): for i in range(6): souye = self.poco(text='关注') if not souye: self.device_1.keyevent('KEYCODE_BACK') time.sleep(5) else: print('返回到首页.....') time.sleep(20) break
def add_client(self, site, count): connect_device( "Android:///?cap_method=JAVACAP&&touch_method=ADBTOUCH&&ori_method=ADBORI" ) poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False) android_info = Android().get_display_info() android_px = (android_info['width'], android_info['height']) stop_app("com.tencent.mm") start_app("com.tencent.mm") sleep(3) poco(textMatches='^微信.*$').wait_for_appearance() sleep(5) while not poco(text="文件传输助手"): poco.scroll(direction='vertical', percent=0.3, duration=1.0) poco(text="文件传输助手").click() poco("com.tencent.mm:id/al_").set_text(site) poco(text="发送").click() sleep(2) xx = AppBase().touch_click_xy( poco("com.tencent.mm:id/ala")[-1].get_position()) touch([xx[0], xx[1]]) sleep(3) poco.long_click(pos=(0.5, 0.5)) sleep(5) path = os.path.abspath( os.path.dirname(os.path.dirname(__file__)) + '\\materials\\pic\\try.png') touch(Template(path, target_pos=8, resolution=android_px)) sleep(2) if poco(text="添加到通讯录").exists(): poco(text="添加到通讯录").click() sleep(4) poco(text="发消息").click() sleep(10) welcome_text = [] for i in range(-count, 0): if poco("com.tencent.mm:id/al4").offspring( "com.tencent.mm:id/an3").child( "android.widget.RelativeLayout")[i].offspring( 'com.tencent.mm:id/ala').exists(): poco("com.tencent.mm:id/al4").offspring( "com.tencent.mm:id/an3").child( "android.widget.RelativeLayout")[i].offspring( 'com.tencent.mm:id/ala').long_click() elif poco("com.tencent.mm:id/al4").offspring( "com.tencent.mm:id/an3").child( "android.widget.RelativeLayout")[i].offspring( 'com.tencent.mm:id/pp').exists(): poco("com.tencent.mm:id/al4").offspring( "com.tencent.mm:id/an3").child( "android.widget.RelativeLayout")[i].offspring( 'com.tencent.mm:id/pp').long_click() path1 = os.path.abspath( os.path.dirname(os.path.dirname(__file__)) + '\\materials\\pic\\icon1.png') touch(Template(path1, resolution=android_px)) welcome_text.append( poco("com.tencent.mm:id/dv0").get_text()) # 欢迎语引用 text = [] text1 = [] for i in welcome_text: text.append((i.split(':'))[1:]) for i in text: a = ''.join(i) if a.startswith('[链接]'): a = a.split('[链接]') text1.append(''.join(a)) stop_app('com.tencent.mm') return text1
# -*- encoding=utf8 -*- """ 负责点掉首页弹窗 """ from airtest.core.android import Android from poco.drivers.android.uiautomation import AndroidUiautomationPoco from common.app.get_current_activity import * poco = AndroidUiautomationPoco(force_restart=False) android = Android() def featurelist_popup_window(): if current_activity( ) == "com.cmcm.live/com.cmcm.cmlive.activity.VideoListActivity": # 异常断播弹窗 if poco("com.cmcm.live:id/scrollView").exists(): poco("com.cmcm.live:id/button2").click() # 隐私弹窗 if poco("com.cmcm.live:id/agree_tv").exists(): poco("com.cmcm.live:id/agree_tv").click() # 星光任务 if poco("com.cmcm.live:id/check_in_positive").exists(): poco("com.cmcm.live:id/check_in_close").click() # 私信弹窗 if poco("com.cmcm.live:id/letter_remind_img").exists(): android.keyevent("back") # 评分弹窗 if poco("com.cmcm.live:id/rating_positive").exists(): poco("com.cmcm.live:id/rating_close").click()
def setUpClass(cls): cls.device = Android('a6c29d2b', adbhost=('10.250.190.230', 5038)) cls.poco = AndroidUiautomationPoco(cls.device)
VM = 'emulator-5554' WIRE_VIVO = '192.168.1.101:5555' BASE_DIR = os.path.dirname(__file__) ROK_PACKAGE_NAME = 'com.lilithgames.rok.offical.cn' logger = get_logger("airtest") logger.setLevel(logging.INFO) if not cli_setup(): auto_setup(__file__, logdir=True, devices=[ "android://127.0.0.1:5037/%s" % (VM if DEBUG else WIRE_VIVO), ]) dev = Android() jcap = Javacap(dev.adb) import matplotlib.pyplot as plt # ret = find_all(Template(r"tpl1610026421082.png", record_pos=(-0.429, 0.195), resolution=(2280, 1080))) # print(ret) # exists(Template(r"tpl1610026442515.png", record_pos=(-0.431, 0.118), resolution=(2280, 1080))) # ret = find_all(Template(r"tpl1610026478715.png", record_pos=(-0.429, 0.194), resolution=(2280, 1080))) # print(ret) # ret = find_all(Template(r"tpl1610026507266.png", record_pos=(-0.431, 0.118), resolution=(2280, 1080))) # print(ret) # exists(Template(r"tpl1610026732683.png", record_pos=(0.437, 0.203), resolution=(2280, 1080))) # exists(Template(r"tpl1610026787532.png", record_pos=(0.226, 0.116), resolution=(2280, 1080))) # exists(Template(r"tpl1610026804600.png", record_pos=(-0.228, 0.008), resolution=(2280, 1080)))
def __init__(self, device, package, activity): self.device = Android(device) self.package = package self.activity = activity self.pack = '/'.join([package, activity]) self.poco = None
def main(DeviceNum): # 多设备连接时,可指定设备编号 # python -m airtest run main.py --device Android://127.0.0.1:5037/0123456789ABCDEF # device = Android('GWY0216C16002906') # 设备类型 1 手机[720,1280][1080,1920] 2平板[1200,1920] # DeviceNum = 'c5bac654' NeedSwipe = ['CLB0218414001154', 'DLQ0216824000142', 'E4J4C17405011422', 'APU0216530000778', 'APU0216408028484', 'APU0216111008105'] try: DbContext = DbHelper() DeviceType = 0 device = Android(DeviceNum) # device.adb.start_shell("su") # device.adb.start_shell("wipe data") # device.adb.start_shell("wipe cache") # device.adb.start_cmd("adb reboot") if '0123456789ABCDEF' not in DeviceNum: device.wake() # 唤醒页面 poco = AndroidUiautomationPoco(device) # if DeviceNum in NeedSwipe: poco.swipe([0.4, 0.9], [0.4, 0.55], duration=0.1) time.sleep(2) # 复位一下,防止之前没有睡眠也滑动 poco.swipe([0.4, 0.45], [0.4, 0.9], duration=0.1) else: poco = AndroidUiautomationPoco( use_airtest_input=True, screenshot_each_action=False) # ClearMemory(device,poco,'') # return device_screen = poco.get_screen_size() device_x = device_screen[0] device_y = device_screen[1] print(str(device_y), str(device_x)) if device_y > 1600 and device_x > 1080: DeviceType = 2 else: DeviceType = 1 if DeviceType == 0: print( '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o(╥﹏╥)o 设备型号无法确定 o(╥﹏╥)o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') DbContext.AddLog(DeviceNum, 3, '设备[' + DeviceNum + ']型号无法确定') return elif DeviceType == 1: print( '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~O(∩_∩)O 设备型号为手机 O(∩_∩)O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n') else: print( '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~O(∩_∩)O 设备型号为平板 O(∩_∩)O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n') except Exception as e: print('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o(╥﹏╥)o 设备连接异常 o(╥﹏╥)o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') DbContext.AddLog( DeviceNum, 3, "设备[" + DeviceNum + "]连接异常:" + repr(e).replace("'", "")) else: BackHomeStatus = BackHomePage( poco, DbContext, DeviceNum, device) # 返回首页 if BackHomeStatus: mode = DbContext.GetDeviceRunningMode(DeviceNum) taskList = [] # 获取当前设备要抓取的城市 IsRunning = True if mode == 1: taskList = DbContext.GetDeviceTask(DeviceNum) elif mode == 2: taskList = DbContext.GetDeviceTaskByMode2() elif mode == 3: taskList = DbContext.GetDeviceTaskByMode3() elif mode == 5: IsRunning = False elif mode == 6: # 根据店名爬取 while True: data = get_shopName() id_name_addr_city = data[0] shopid = id_name_addr_city[0] storeName = id_name_addr_city[1] addr = id_name_addr_city[2] city = (id_name_addr_city[3])[:-1] print(storeName, addr, city) if data[1] == 3: crawl_status_code(storeName, 3) else: crawl_status_code(storeName, 1) search_store(storeName, poco, device, addr, city, shopid) BackHomePage(poco, DbContext, DeviceNum, device) crawl_status_code(storeName, 2) if(len(taskList) == 1): # 更新任务为运行中 DbContext.UpdateTaskStatus( int(taskList[0]['TaskId']), 1, 0, mode) while IsRunning: BackHomeStatus = BackHomePage( poco, DbContext, DeviceNum, device) # 返回首页 if BackHomeStatus: for task in taskList: # AddressList(task['TargetCity']) #获取未抓取的坐标点 AllPosition = [ {'RepresentativeAdress': task['RepresentativeAdress'], 'Genhash':task['Genhash']}] taskId = task['TaskId'] cityCode = task['CityCode'] # 更新任务为运行中 DbContext.UpdateTaskStatus(int(taskId), 1, 0, mode) # 计时 StartTime = datetime.datetime.now() # 返回值需要写进队列 currentTaskResult, IsEmergencyStop = StartCapture( poco, AllPosition, DeviceType, task['TargetCity'], DeviceNum, cityCode, device) # 抓取数据 # 没有紧急置停的情况下才完成后续的更新 if not IsEmergencyStop: EndTime = datetime.datetime.now() DbContext = DbHelper() DbContext.AddLog(DeviceNum, 2, '设备[' + DeviceNum + ']本次抓取[' + AllPosition[0]['RepresentativeAdress'] + '] [' + str( len(currentTaskResult)) + '] 家店,耗时:' + str(((EndTime - StartTime).seconds)/60)) # 更新任务为完成 DbContext.UpdateTaskStatus( int(taskId), 2, len(currentTaskResult), mode) # 将任务的执行结果回写到队列 if mode != 3: Produce = SendMessage() result = Produce.sendMessage( taskId, currentTaskResult) if result: # 将任务状态改为已回写队列 DbContext.UpdateTaskStatus( int(taskId), 3, len(currentTaskResult), mode) else: break mode = DbContext.GetDeviceRunningMode(DeviceNum) if len(taskList) > 0: taskList.clear() if mode == 1: taskList = DbContext.GetDeviceTask(DeviceNum) elif mode == 2: taskList = DbContext.GetDeviceTaskByMode2() elif mode == 3: taskList = DbContext.GetDeviceTaskByMode3() elif mode == 5: print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~O(∩_∩)O 紧急置停 O(∩_∩)O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') break if len(taskList) > 0: print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~O(∩_∩)O 抽取一条任务 O(∩_∩)O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') else: print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~O(∩_∩)O 任务执行完毕 O(∩_∩)O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') break else: DbContext.AddLog( DeviceNum, 3, '设备[' + DeviceNum + ']返回首页异常') break else: DbContext.AddLog(DeviceNum, 3, '设备[' + DeviceNum + ']返回首页异常')
def unlocked(self): while not Android().is_screenon(): wake() while Android().is_locked(): self.poco("com.android.systemui:id/unlock_indicator").swipe( [0.0, -1])