Пример #1
0
 async def request_json(self,
                        method,
                        url,
                        ctrl: Ctrl = ZERO_ONLY_CTRL,
                        **kwargs) -> dict:
     async with sem:
         i = 0
         while True:
             i += 1
             if i >= 10:
                 printer.warn(url)
             try:
                 async with self.var_session.request(method, url,
                                                     **kwargs) as rsp:
                     if rsp.status == 200:
                         json_body = await self.__get_json_body(rsp)
                         if json_body:  # 有时候是None或空,直接屏蔽。下面的read/text类似,禁止返回空的东西
                             json_rsp_type = ctrl.verify(json_body)
                             if json_rsp_type == JsonRspType.OK:
                                 return json_body
                             elif json_rsp_type == JsonRspType.IGNORE:
                                 await asyncio.sleep(1.0)
                     elif rsp.status in (412, 403):
                         printer.warn(f'403频繁, {url}')
                         await asyncio.sleep(240)
             except asyncio.CancelledError:
                 raise
             except:
                 # print('当前网络不好,正在重试,请反馈开发者!!!!')
                 print(sys.exc_info()[0], sys.exc_info()[1], url)
             await asyncio.sleep(0.02)
Пример #2
0
 async def request_json(self,
                        method,
                        url,
                        ctrl: Ctrl = DEFAULT_CTRL,
                        **kwargs) -> dict:
     while True:
         body = await self._req(self._recv_json, method, url, **kwargs)
         if not isinstance(body, dict):  # 这里是强制定制的,与b站配合的!!!!
             continue
         json_rsp_type = ctrl.verify(body)
         if json_rsp_type == JsonRspType.OK:
             return body
         elif json_rsp_type == JsonRspType.IGNORE:
             await asyncio.sleep(0.75)
         elif json_rsp_type == JsonRspType.LOGOUT:
             print('api提示没有登录')
             print(body)
             raise LogoutError(msg='提示没有登陆')
Пример #3
0
 async def request_json(self,
                        method,
                        url,
                        headers=None,
                        data=None,
                        params=None,
                        is_login=False,
                        ctrl: Ctrl = TMP_DEFAULT_CTRL)->dict:
     async with sem:
         i = 0
         while True:
             i += 1
             if i >= 10:
                 printer.warn(url)
             try:
                 async with self.var_session.request(method, url, headers=headers, data=data, params=params) as rsp:
                     if rsp.status == 200:
                         json_body = await self.__get_json_body(rsp)
                         if not json_body:  # 有时候是None或空,直接屏蔽。下面的read/text类似,禁止返回空的东西
                             continue
                         json_rsp_type = ctrl.verify(json_body)
                         # print('test', json_body, json_rsp_type)
                         if json_rsp_type == JsonRspType.OK:
                             return json_body
                         if json_rsp_type == JsonRspType.IGNORE:
                             await asyncio.sleep(1.0)
                             continue
                         if json_rsp_type == JsonRspType.LOGOUT:
                             print('api提示没有登录')
                             print(json_body)
                             if not is_login:
                                 raise LogoutError(msg='提示没有登陆')
                             else:
                                 return json_body
                     elif rsp.status == 403:
                         printer.warn(f'403频繁, {url}')
                         await asyncio.sleep(240)
             except RspError:
                 raise
             except:
                 # print('当前网络不好,正在重试,请反馈开发者!!!!')
                 print(sys.exc_info()[0], sys.exc_info()[1], url)
                 continue