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 + ']返回首页异常')
pass else: continue if isRunning: break except psutil.NoSuchProcess: pass if isRunning: pass else: # 判断程序是否是自己停掉,否则就要重启 DbContext = DbHelper() mode = DbContext.GetDeviceRunningMode(DeviceNum) if mode == 1: taskList = DbContext.GetDeviceTask(DeviceNum) elif mode == 2: taskList = DbContext.GetDeviceTaskByMode2() elif mode == 3: taskList = DbContext.GetDeviceTaskByMode3() elif mode == 5: taskList = [] elif mode == 6: taskList = [] if len(taskList) > 0: print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Reatarting~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' ) try: path = '/root/airtest/log/Device/' + datetime.datetime.now(