async def method(self, key, **data): """ Return a result of executing vk's method `method` Function for special cases only! This method doesn't process nor errors nor captcha. """ url = f"https://api.vk.com/method/{key}?access_token={self.token}&v={VERSION}" if data is None: data = {} if data.get("_replace_nl", True): for k, v in data.items(): data[k] = v.replace("\n", "<br>") if "_replace_nl" in data: del data["_replace_nl"] async with self.session.post(url, data=data, **self.req_kwargs) as resp: try: results = json_iter_parse(await resp.text()) for data in results: if 'response' in data: return data['response'] except json.JSONDecodeError: self.logger.error( "Error while executing vk method: vk's response is wrong!") return False return False
async def execute(self, code, reties=0, **additional_values): """Execute a `code` from vk's "execute" method""" if reties > 4: self.logger.warning("Can't login to VK!") return False if additional_values.get("_replace_nl", True): code = code.replace("\n", "<br>") if "_replace_nl" in additional_values: del additional_values["_replace_nl"] url = f"https://api.vk.com/method/execute?access_token={self.token}&v={VERSION}" async with self.session.post(url, data={ "code": code, **additional_values }, **self.req_kwargs) as resp: errors = [] errors_codes = [] try: response = await resp.text() self.logger.debug( f"Request with code:\n{code}\nResponse:\n{response}") results = json_iter_parse(response) for data in results: if 'error' in data: if data['error']['error_code'] == CAPTCHA_IS_NEEDED: captcha_key = await self.enter_captcha( data['error']["captcha_img"]) if not captcha_key: return False new_data = { "captcha_key": captcha_key, "captcha_sid": data['error']["captcha_sid"] } new_data.update(additional_values) return await self.execute(code, **new_data) errors.append(data['error']) errors_codes.append(data['error']['error_code']) if 'execute_errors' in data: for error in data['execute_errors']: errors.append({ 'code': error['error_code'], 'method': error['method'], 'error_msg': error['error_msg'] }) errors_codes.append(error['error_code']) errors_codes.append(EXECUTE_ERROR) continue if 'response' in data: return data['response'] if INTERNAL_ERROR in errors_codes: await asyncio.sleep(1) if self.app_id: await self.user(self.username, self.password, self.app_id, self.scope) return await self.execute(code, reties + 1) if AUTHORIZATION_FAILED in errors_codes: if self.app_id: await self.user(self.username, self.password, self.app_id, self.scope) return await self.execute(code, reties + 1) except json.JSONDecodeError: self.logger.error( "Error while executing vk method: vk's response is wrong!") return False error_text = "" for error in errors: error_text += (str(error)) + ", " self.logger.error("Errors while executing vk method: " + error_text[:-2]) return False