async def _get_now_app_pkg_name(self, logger=None): """ 获取当前app的pkg_name :return: """ device_list = await self._init_device_list() tasks = [] for device_obj in device_list: _print(msg='create task[where device_id: {}] ...'.format( device_obj.device_id, )) tasks.append( self.loop.create_task( unblock_func(func_name=self._print_now_pkg_name, func_args=[ device_obj, ]))) all_res = await async_wait_tasks_finished(tasks=tasks) _print(msg='所有设备安装完成@', logger=logger) try: del tasks except: pass return
def _select_table(self, sql_str, params=None, logger=None): self.engine.begin() self.conn = self.engine.connect() result = None try: # 设置隔离级别为脏读 self.conn.execute('set tran isolation level read uncommitted;') self.conn.execute('set lock_timeout 20000;') # 设置客户端执行超时等待为20秒 if params is not None: if not isinstance(params, tuple): params = tuple(params) result = self.conn.execute(sql_str, params).fetchall() else: result = self.conn.execute(sql_str).fetchall() except Exception as e: _print(msg='遇到错误:', logger=logger, exception=e, log_level=2) finally: try: self.conn.close() except Exception: pass return result
async def intercept_request(self, request: PyppeteerRequest): """ 截获request(do something you like ...) :param request: :return: """ url = request.url headers = request.headers post_data = request.postData request_resource_type = request.resourceType msg = '[{:8s}] url: {}, post_data: {}'.format( 'request', url, post_data, ) if self.target_url in url: _print(msg=msg, logger=self.lg, log_level=1) else: pass if not self.load_images and request_resource_type in [ 'image', 'media' ]: await request.abort() else: # 放行请求 await request.continue_()
async def app_install_all_devices(self, app_url, logger=None) -> None: """ 注入所有设备 or 直接: $ adb -s 816QECTK24ND8 install xxxx.apk :param app_url: 可以用临时文件的形式来下载(先下载好原始apk, 再上传)(eg: https://www.file.io/) :param logger: :return: """ device_list = await self._init_device_list() tasks = [] for device_obj in device_list: _print( msg='create task[where device_id: {}] to install app_url: {} ...' .format(device_obj.device_id, app_url)) func_args = [ device_obj, app_url, logger, ] tasks.append( self.loop.create_task( unblock_func( func_name=self.app_install_someone_device, func_args=func_args, ))) all_res = await async_wait_tasks_finished(tasks=tasks) _print(msg='所有设备安装完成@', logger=logger) try: del tasks except: pass return
async def intercept_response(self, response: PyppeteerResponse): """ 截获response(do something you like ...) :param response: :return: """ global cate_level_queue response_status = response.status request = response.request request_url = request.url request_headers = request.headers request_method = request.method request_resource_type = request.resourceType post_data = request.postData msg = '[{:8s}] request_resource_type: {:10s}, request_method: {:6s}, response_status: {:5s}, request_url: {}'.format( 'response', request_resource_type, request_method, str(response_status), request_url, ) if self.target_url in request_url: _print(msg=msg, logger=self.lg, log_level=1) else: pass if request_resource_type in [ 'xhr', 'fetch', 'document', 'script', 'websocket', 'eventsource', 'other' ]: if self.target_url in request_url: try: body = await response.text() intercept_body = body.replace('\n', '').replace( '\t', '').replace(' ', '') msg = '[{:8s}] {}'.format(colored('@-data-@', 'green'), intercept_body[0:1000]) _print(msg=msg, logger=self.lg) cate_level_queue.put(intercept_body) except (PyppeteerNetworkError, IndexError, Exception) as e: _print(msg='遇到错误:', logger=self.lg, log_level=2, exception=e) else: pass else: pass
def get_tm_coupon_url_from_lq5u(goods_id='', goods_name_or_m_url: str='', proxy_type=PROXY_TYPE_HTTP, num_retries=5, logger=None,) -> str: """ 从领券无忧根据goods_name搜索tm优惠券 :param goods_id: 推荐使用商品id来查券 :param goods_name_or_m_url: 商品名 or 商品地址 :return: """ # todo 测试发现无需搜索, 只需把goods_id 改为领券无忧的对应的url即可查询是否有券 # 基于领券无忧来根据商品名获取其优惠券 headers = get_random_headers( user_agent_type=1, connection_status_keep_alive=False, ) headers.update({ 'Proxy-Connection': 'keep-alive', 'Origin': 'http://www.lq5u.com', 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': 'http://www.lq5u.com/', }) # # 只搜索天猫的 # data = { # 'p': '1', # 'cid': '0', # 'sort': '0', # 'b2c': '1', # '0'为搜索tb, tm | '1'为只搜索tm # 'coupon': '1', # 'k': goods_name_or_m_url, # } # body = Requests.get_url_body( # method='post', # url='http://www.lq5u.com/', # headers=headers, # # cookies=cookies, # data=data, # verify=False, # ip_pool_type=IP_POOL_TYPE, # num_retries=num_retries, # proxy_type=proxy_type,) # assert body != '' # # print(body) # # lq5u_url_list_sel = { # 'method': 'css', # 'selector': 'li a ::attr("onmousedown")', # } # ori_lq5u_url_list = parse_field( # parser=lq5u_url_list_sel, # target_obj=body, # is_first=False,) # lq5u_url_list = [] # for item in ori_lq5u_url_list: # try: # url = re.compile('this.href=\'(.*?)\'').findall(item)[0] # assert url != '' # except Exception: # continue # # lq5u_url_list.append('http://www.lq5u.com' + url) # # assert lq5u_url_list != [] # pprint(lq5u_url_list) # 领券无忧对应页面如下 # url = 'http://www.lq5u.com/item/index/iid/{}.html'.format(goods_id) # body = Requests.get_url_body( # method='get', # url=url, # headers=headers, # verify=False, # ip_pool_type=IP_POOL_TYPE, # num_retries=num_retries, # proxy_type=proxy_type, ) # assert body != '' # print(body) # # coupon_info_sel = { # 'method': 'css', # 'selector': 'span.b.red ::text', # } # coupon_info = parse_field( # parser=coupon_info_sel, # target_obj=body, # ) # if '很遗憾,该商品没有优惠券' in coupon_info: # return [] # else: # _print(msg='goods_id: {}, 存在优惠券'.format(goods_id), logger=logger) # return [] # 根据领券无忧接口 from random import uniform as random_uniform headers = get_random_headers( user_agent_type=1, connection_status_keep_alive=False, ) headers.update({ 'Origin': 'http://www.lq5u.com', 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': 'http://www.lq5u.com/item/index/iid/{}.html'.format(goods_id), 'X-Requested-With': 'XMLHttpRequest', }) params = ( ('rnd', str(random_uniform(0, 1))), # eg: '0.4925945510743117' ) data = { 'iid': goods_id } body = Requests.get_url_body( method='post', url='http://www.lq5u.com/item/ajax_get_auction_code.html', headers=headers, params=params, data=data, verify=False, ip_pool_type=IP_POOL_TYPE, num_retries=num_retries, proxy_type=proxy_type,) assert body != '' # print(body) data = json_2_dict( json_str=body, default_res={}, logger=logger,).get('data', {}) # pprint(data) coupon_url = data.get('coupon_click_url', '') if coupon_url != '': _print(msg='该goods_id: {} 含 有优惠券'.format(goods_id), logger=logger) else: _print(msg='该goods_id: {} 不含 有优惠券'.format(goods_id), logger=logger) return coupon_url