class ReqCtrl: check_ctrl = Ctrl(extend=( { 'code': -500 }, JsonRspType.LOGOUT, { 'code': 0 }, JsonRspType.OK, { 'code': -400 }, JsonRspType.OK, )) join_ctrl = Ctrl(extend=( { 'code': -500, 'msg': In('登录') }, JsonRspType.LOGOUT, { 'code': -500, 'msg': In('非法') }, JsonRspType.OK, { 'code': 0 }, JsonRspType.OK, { 'code': -1 }, JsonRspType.OK, # 未开始抽奖 { 'code': -400 }, JsonRspType.OK, # 不存在/已经过期 { 'code': -3 }, JsonRspType.OK, # 已抽过 { 'code': 400, 'msg': In('拒绝') }, JsonRspType.OK, # 小黑屋 ))
class ReqCtrl: check_ctrl = Ctrl( BaseCtrl(logout_verifiers=[CtrlElem(code=-500)], ok_verifiers=[CtrlElem(code=0), CtrlElem(code=-400)])) join_ctrl = Ctrl( BaseCtrl( logout_verifiers=[CtrlElem(code=-500)], ok_verifiers=[ CtrlElem(code=0), CtrlElem(code=-1), # 未开始抽奖 CtrlElem(code=-400), # 不存在/已经过期 CtrlElem(code=-3), # 已抽过 CtrlElem(code=400, others=[In('msg', '拒绝')]) # 小黑屋 ]))
class ReqCtrl: join_v4_ctrl = Ctrl(extend=( { 'code': -401, 'msg': In('登录') }, JsonRspType.LOGOUT, { 'code': 0 }, JsonRspType.OK, { 'code': -405 }, JsonRspType.OK, # 奖品都被领完啦 { 'code': -403, 'msg': In('已') }, JsonRspType.OK, # 'code': -403, 'msg': '您已参加抽奖~' { 'code': -403, 'msg': In('拒绝') }, JsonRspType.OK, # 'code': -403, 'msg': '访问被拒绝' { 'code': -401, 'msg': In('没开始') }, JsonRspType.OK, # 抽奖还没开始哦 ))
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)
class ReqCtrl: join_v4_ctrl = Ctrl( BaseCtrl( logout_verifiers=[CtrlElem(code=-401, others=[In('msg', '登录')])], ok_verifiers=[ CtrlElem(code=0), CtrlElem(code=-405), # 奖品都被领完啦 CtrlElem(code=-403, others=[In('msg', '已')]), # 'code': -403, 'msg': '您已参加抽奖~' CtrlElem(code=-403, others=[In('msg', '拒绝')]), # 'code': -403, 'msg': '访问被拒绝' CtrlElem(code=-401, others=[In('msg', '没开始')]) # 抽奖还没开始哦 ]))
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='提示没有登陆')
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
# 其余和 BASE_CTRL 一样就行 LOGIN_CTRL = Ctrl( extend=( { 'code': 0 }, JsonRspType.OK, # 目前为止,0 肯定成功,如果例外,自己另写 { 'code': 1024 }, JsonRspType.IGNORE, { 'msg': In('操作太快') }, JsonRspType.IGNORE, { 'msg': In('系统繁忙') }, JsonRspType.IGNORE, { 'msg': In('过于频繁') }, JsonRspType.IGNORE, { 'message': In('服务繁忙') }, JsonRspType.IGNORE, ), base=None, default=JsonRspType.OK)
class ReqCtrl: fetch_wearing_medal_ctrl = Ctrl(BaseCtrl(ok_verifiers=[ CtrlElem(code=0), ]))
import utils from json_rsp_ctrl import Ctrl, JsonRspType, In, BASE_CTRL MANGA_SIGN_CTRL = Ctrl(extend=( { 'msg': In('uid must > 0') }, JsonRspType.LOGOUT, ), base=BASE_CTRL, default=JsonRspType.OK) class MangaSignReq: @staticmethod async def sign(user): url = 'https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn' extra_params = [ f'access_key={user.dict_bili["access_key"]}', f'ts={utils.curr_time()}' ] params = user.sort_and_sign(extra_params) json_rsp = await user.other_session.request_json( 'POST', url, # 不知道为啥必须这么做2333 headers={ **user.dict_bili['appheaders'], 'Content-Type': "application/x-www-form-urlencoded" },