async def test_get_pts_cached_value(self): session = API(TestSession()) lp = LongPoll(session, mode=0) await lp.get_pts() pts = await lp.get_pts() self.assertIsInstance(pts, type(TestSession.PTS)) self.assertEqual(pts, TestSession.PTS)
async def main(args): app_id = get_app_id(args) if app_id is None: logger.critical('No APP_ID found') return False # Loading modules logger.info('Loading modules') modules = ModuleCache(args.modules) assert modules, 'No modules loaded' # Loading token with closing(appstorage.create('vk-dump', 'the-island.ru')) as db: token = await get_or_create_token(args, app_id, db, modules.scope) async with TokenSession(token) as session: api = API(session) rate = TokenLimiter(args.parallel) async with aiohttp.ClientSession() as session: if not os.path.isdir(args.root): logger.info('Creating root dump directory %s', args.root) os.makedirs(args.root) downloader = Downloader(args.root, session, rate) xs = ( stream.merge(*[m.get_items(api, rate) for m in modules]) | pipe.map(downloader.download) # pylint: disable=E1101 ) await xs logger.info('Done')
def __init__(self, session_or_api, mode: Optional[Union[int, list]], wait: int = 25, version: int = 2, timeout: int = None): """ :param session_or_api: session object or data for creating a new session :type session_or_api: BaseSession or API or LazyAPI :param mode: additional answer options :param wait: waiting period :param version: protocol version :param timeout: timeout for *.getLongPollServer request in current session """ if isinstance(session_or_api, (API, LazyAPI)): self.api = session_or_api else: self.api = API(session_or_api) self.timeout = timeout or self.api._session.timeout if type(mode) == list: mode = sum(mode) self.base_params = { 'version': version, 'wait': wait, 'act': 'a_check' } if mode is not None: self.base_params['mode'] = mode self.pts = None self.ts = None self.key = None self.base_url = None
def __init__(self, config_filename): config = json.from_path(config_filename) session = TokenSession(access_token=config['token']) self.api = API(session) self.longpoll = BotsLongPoll(session, mode=0, group_id=config['group_id'])
def test_wait_error_code_3(self): TestDriver.message = {'failed': 3} session = API(TestSession()) lp = LongPoll(session, mode=0) response = yield from lp.wait() self.assertDictEqual(response, {'ts': -TestSession.TS})
async def test_get_pts_need_ts(self): session = API(TestSession()) lp = LongPoll(session, mode=0) result = await lp.get_pts(need_ts=True) self.assertEqual(len(result), 2) self.assertEqual(result[0], TestSession.PTS) self.assertEqual(result[1], TestSession.TS)
async def test_wait_valid(self): TestDriver.message = {'ts': -TestSession.TS} session = API(TestSession()) lp = LongPoll(session, mode=0) response = await lp.wait() self.assertDictEqual(response, TestDriver.message)
async def test_wait_error_code_1(self): TestDriver.message = {'failed': 1, 'ts': 42} session = API(TestSession()) lp = LongPoll(session, mode=0) response = await lp.wait() self.assertDictEqual(response, {'ts': -TestSession.TS})
async def get_wall_id_from_public_url(url: str) -> Optional[int]: try: screen_name, check_id = url.split("/")[-1], None async with TokenSession(VK_TOKEN) as session: api = API(session) wall_object = await api.utils.resolveScreenName( screen_name=screen_name) if wall_object: return (-wall_object["object_id"] if wall_object["type"] in ["group", "page"] else wall_object["object_id"]) else: if screen_name.startswith("id", 0): check_id = int(screen_name[2:]) elif screen_name.startswith("public", 0): check_id = -int(screen_name[6:]) elif screen_name.startswith("club", 0): check_id = -int(screen_name[4:]) if check_id: check_data = await api.wall.get(owner_id=check_id, count=1)["items"] if check_data: return check_id return check_id except Exception as err: logger.error(f"User failed input vk_wall url - {err}") return None
def __init__(self, token, group_id, message_raid, symbols, send_keyboard=False, keyboard_text="Raidy perfect.."): """ :param token: Токен паблика ВКонтакте :param group_id: ID паблика ВКонтакте :param message_raid: Сообщение для рейда :param symbols: Количество символов для обрезки сообщения (см. utils.py -> trimming_message) :param send_keyboard: Отправление клавиатуры :param keyboard_text: Текст для клавиатуры (если не указан, то по умолчанию будет использоватся <<Raidy perfect...>>) """ self.token = token self.group_id = group_id self.message_raid = message_raid self.symbols = symbols if symbols > 2 else 5 self.send_keyboard = send_keyboard self.keyboard_text = keyboard_text self.keyboard = self._create_keyboard(text=self.keyboard_text) # keyboard object (json) after generation self.session = TokenSession(access_token=self.token) # vk session object self.api = API(self.session) # API object self.lp = BotsLongPoll(self.api, mode=2, group_id=self.group_id) # longpoll object self.logger = Log("raid_log") # logger object. create log file (raid_log.log) self.message_util = MessagesUtil(self.text, self.symbols) # MessagesUtil initialization
async def test_wait_error_code_4(self): TestDriver.message = {'failed': 4} session = API(TestSession()) lp = LongPoll(session, mode=0) with self.assertRaises(VkLongPollError): await lp.wait()
async def run(config): interval = int(config.interval) client = motor.motor_asyncio.AsyncIOMotorClient(config.mongo_url) db = client.memes_db collection = db.test_collection session = TokenSession(config.vk.token) api = API(session) bot = Bot(api_token=config.token, proxy=config.proxy['proxy_url']) channel = bot.channel("@MemesAggregation") while True: logging.info('Start checking groups') for group in config.vk.groups: asyncio.sleep(1) r = await api('wall.get', domain=group) for item in r['items']: if len(item['attachments'] ) == 1 and item['marked_as_ads'] == 0 and item[ 'attachments'][0]['type'] == 'photo': url = item['attachments'][0]['photo']['photo_604'] name = re.findall('(?=\w+\.\w{3,4}$).+', url)[0] if await collection.find_one({'name': name}) is None: logging.info('Found new meme') collection.insert_one({'name': name}) # TODO: Make it async urllib.request.urlretrieve(url, f"data/images/{name}") await channel.send_photo( photo=open(f"data/images/{name}", "rb")) os.remove(f"data/images/{name}") logging.info('Groups checked, waiting for next time...') await asyncio.sleep(interval)
async def test_request_with_timeout(self): api = API(TestSession()) request = LazyRequest(api, 'test') message = request(timeout=1) first = await message() second = {'params': {}, 'timeout': 1, 'method_name': 'test'} self.assertDictEqual(first, second)
def test_request_with_method_name_and_args(self): api = API(TestSession()) first = yield from api('test', arg=1) second = {'params': {'arg': 1}, 'timeout': None, 'method_name': 'test' } self.assertDictEqual(first, second)
def test_complex_request_with_args(self): api = API(TestSession()) first = yield from api.test1.test2(arg=1) second = {'params': {'arg': 1}, 'timeout': None, 'method_name': 'test1.test2' } self.assertDictEqual(first, second)
def test_request_with_timeout(self): api = API(TestSession()) first = yield from api.test(timeout=1) second = {'params': {}, 'timeout': 1, 'method_name': 'test' } self.assertDictEqual(first, second)
async def test_request_with_args(self): api = API(TestSession()) first = await api.test(arg=1) second = {'params': {'arg': 1}, 'timeout': None, 'method_name': 'test' } self.assertDictEqual(first, second)
async def test_request_with_method_name_and_timeout(self): api = API(TestSession()) first = await api('test', timeout=1) second = {'params': {}, 'timeout': 1, 'method_name': 'test' } self.assertDictEqual(first, second)
async def test_complex_request(self): api = API(TestSession()) first = await api.test1.test2(timeout=1) second = {'params': {}, 'timeout': 1, 'method_name': 'test1.test2' } self.assertDictEqual(first, second)
def test_request_without_args(self): api = API(TestSession()) request = Request(api, 'test') first = yield from request() second = {'params': {}, 'timeout': None, 'method_name': 'test' } self.assertDictEqual(first, second)
async def init_vk(self): vk_session = None if self.config['IMPLICIT']: vk_session = ImplicitSession(self.config['USER_LOGIN'], self.config['USER_PASSWORD'], self.config['APP_ID'], ['messages', 'wall']) else: # TODO(spark): implement TokenSession pass self.logger.info('Auth in VK...') await vk_session.authorize() self.vk_api = vk_api = API(vk_session) vk_lp = LongPoll(vk_api, mode=2) while self.running: # Main working loop response = await vk_lp.wait() for action in response['updates']: if action[0] is 4: message_id = action[1] sender = action[3] sender_id = sender message = str(action[6]) attachment = action[7] self.logger.debug('Got message: {}'.format(message)) if sender > 2000000000: # Groupchat sender_id = int(attachment['from']) f_flag = False for f in self.filters: f_res = await f(sender, sender_id, message, attachment) if f_res is False: f_flag = True continue if f_flag: continue if message.startswith(self.config['COMMAND_SYMBOL']) and message[1] is not ' ': message = message[1:] flag = False for c in self.commands: if message.startswith(c) and not flag: flag = True command = message.split(' ')[0] if command in self.admin_commands and sender_id not in self.config['ADMINS']: await self.send_message(sender, 'Access denied') else: await self.commands[command](sender, sender_id, message, attachment) if flag is False: await self.send_message(sender, 'Command not found') vk_session.close()
async def _session(self): session = TokenSession(access_token=self.token) try: yield API(session) except Exception as err: logger.error(f"Error: {err}") raise finally: await session.close()
def test_wait_valid_with_session_auto_auth(self): s = TestAuthSession(login=USER_LOGIN, password=USER_PASSWORD, app_id=APP_ID, scope='messages') api = API(s) lp = LongPoll(api, mode=2, wait=2) response = yield from lp.wait() s.close() self.assertTrue('ts' in response) self.assertTrue('updates' in response)
def vk(self): if not hasattr(self, 'vk_api'): loop = asyncio.get_event_loop() connector = aiohttp.TCPConnector(verify_ssl=False) session = aiohttp.ClientSession(connector=connector) driver = HttpDriver(loop=loop, session=session) self.session = TokenSession(access_token=self.credentials.token, driver=driver) self.vk_api = API(self.session) return self.vk_api
def get_user_api(driver=drivers.HttpDriver()): user = input('Login: '******'photos', driver=driver) api = API(session) return api
async def func(): connector = TCPConnector(ssl=False) session = ClientSession(connector=connector) driver = HttpDriver(loop=loop, session=session) async with TokenSession( access_token= 'e0b61b2781bb2e3ec1b262fe35af1fcca3c27d6d12806b12ba1bc65665c62cd93311f2b8cbb2f7cc1aebf', driver=driver) as session: lp = BotsLongPoll(session, group_id=156616044, mode=2) api = API(session) print(api) while True: data = await lp.wait() updates = data["updates"] print(updates) # TODO: add async for for i in updates: if i['type'] == 'message_new': try: search_string = i['object']['payload'] search_command = re.search('(\"command\":\")(.*)(\")', search_string) command = search_command.group(2) step = await Step.filter(command=command).first() await api.messages.send(message=step.message, peer_id=i['object']['from_id'], random_id=0, keyboard=step.keyboard) except ContentTypeError as e: search_string = i['object']['payload'] search_command = re.search('(\"command\":\")(.*)(\")', search_string) command = search_command.group(2) step = await Step.filter(command=command).first() pattern = re.compile(r'([А-Я].{,800}[\.])', re.S) chunked_message = pattern.findall(step.message) for chunk in chunked_message[:-1]: await api.messages.send( message=chunk, peer_id=i['object']['from_id'], random_id=0) await api.messages.send( message=chunked_message[-1:][0], peer_id=i['object']['from_id'], random_id=0, keyboard=step.keyboard) except Exception: step = await Step.filter(command='start').first() await api.messages.send(message='Что-то пошло не так', peer_id=i['object']['from_id'], random_id=0, keyboard=step.keyboard)
async def _execute(self): """ Groups hits and executes them using the execute method, after execution the pool is cleared """ executed_pools = [] for token, calls in self.pool.items(): session = self.token_session_class(token) self.sessions.append(session) api = API(session) for methods_pool in chunks(calls, self.call_number_per_request): executed_pools.append( VkExecuteMethodsPool(methods_pool).execute(api)) await asyncio.gather(*executed_pools)
async def test_wait_valid_with_session_auto_auth(self): s = TestInternalAuthSession(login='******', password='******', app_id='123', scope='messages') s.BASE_URL = self.base_url api = API(s) lp = LongPoll(api, mode=2, wait=2) response = await lp.wait() await s.close() self.assertTrue('ts' in response) self.assertTrue('updates' in response)
async def test_longpoll_wait(longpoll, messages, exception, ts): session = Session() session.driver.messages = messages session = API(session) lp = longpoll(session) session._session.driver.expected_mode = lp.base_params.get('mode') session._session.driver.expected_version = lp.base_params.get('version') if exception is None: response = await lp.wait() assert response == messages[-1] _, real_ts = await lp.get_pts(need_ts=True) assert real_ts == ts else: with pytest.raises(exception): await lp.wait()
def __init__(self, session_or_api, mode, wait=25, version=1, timeout=None): if type(session_or_api) == API: self.api = session_or_api else: self.api = API(session_or_api) self.timeout = timeout or self.api._session.timeout if type(mode) == list: mode = sum(mode) self.base_params = { 'version': version, 'wait': wait, 'mode': mode, 'act': 'a_check' } self.pts = None self.ts = None self.key = None self.base_url = None