Vk api асинхронность python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

vk.com API python wrapper for asyncio

License

alexanderlarin/aiovk2

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Радиокнопки html выбираются все

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.rst

vk.com API python wrapper for asyncio

This is a fork of https://github.com/Fahreeve/aiovk package which looks currently outdated and unmaintained

for old version of python you can use https://github.com/dimka665/vk

  • asynchronous
  • support python 3.5+ versions
  • have only one dependency — aiohttp 3+
  • support two-factor authentication
  • support socks proxy with aiohttp-socks
  • support rate limit of requests
  • support Long Poll connection

In all the examples below, I will give only the

async def func(): code> loop = asyncio.get_event_loop() loop.run_until_complete(func())

TokenSession — if you already have token or you use requests which don’t require token

session = TokenSession() session = TokenSession(access_token='asdf123..')

ImplicitSession — client authorization in js apps and standalone (desktop and mobile) apps

>>> session = ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID) >>> await session.authorize() >>> session.access_token asdfa2321afsdf12eadasf123.
ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 'notify') ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 'notify,friends') ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, ['notify', 'friends']) ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 3) # notify and friends

Also you can use SimpleImplicitSessionMixin for entering confirmation code or captcha key

AuthorizationCodeSession — authorization for server apps or Open API

>>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI, CODE) >>> await session.authorize() >>> session.access_token asdfa2321afsdf12eadasf123.
>>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI) >>> await session.authorize(CODE) >>> session.access_token asdfa2321afsdf12eadasf123.

Authorization using context manager — you won’t need to use session.close() after work

async with aiovk2.TokenSession(access_token=YOUR_VK_TOKEN) as ses: api = API(ses).

And your session will be closed after all done or code fail(similar to simple «with» usage) Works with all types of authorization

HttpDriver — default driver for using aiohttp

>>> driver = HttpDriver() >>> driver = HttpDriver(timeout=10) # default timeout for all requests
>>> driver = ProxyDriver(PROXY_ADDRESS, PORT) # 1234 is port >>> driver = ProxyDriver(PROXY_ADDRESS, PORT, timeout=10) >>> driver = ProxyDriver(PROXY_ADDRESS, PORT, PROXY_LOGIN, PROXY_PASSWORD, timeout=10)

How to use custom driver with session:

>>> session = TokenSession(. driver=HttpDriver())

How to use driver with own loop:

>>> loop = asyncio.get_event_loop() >>> asyncio.set_event_loop(None) >>> session = TokenSession(driver=HttpDriver(loop=loop)) # or ProxyDriver

How to use driver with custom http session object:

>>> connector = aiohttp.TCPConnector(verify_ssl=False) >>> session = aiohttp.ClientSession(connector=connector) >>> driver = HttpDriver(loop=loop, session=session)

LimitRateDriverMixin — mixin class what allow you create new drivers with speed rate limits

>>> class ExampleDriver(LimitRateDriverMixin, HttpDriver): . requests_per_period = 3 . period = 1 #seconds
>>> session = TokenSession() >>> api = API(session) >>> await api.users.get(user_ids=1) ['first_name': 'Pavel', 'last_name': 'Durov', 'id': 1>]
>>> session = TokenSession() >>> api = API(session) >>> await api('users.get', user_ids=1) ['first_name': 'Pavel', 'last_name': 'Durov', 'id': 1>]

Also you can add timeout argument for each request or define it in the session

It is useful when a bot has a large message flow

>>> session = TokenSession() >>> api = LazyAPI(session) >>> message = api.users.get(user_ids=1) >>> await message() ['first_name': 'Pavel', 'last_name': 'Durov', 'id': 1>]

Supports both variants like API object

>>> api = API(session) >>> lp = UserLongPoll(api, mode=2) # default wait=25 >>> await lp.wait() "ts":1820350345,"updates":[. ]> >>> await lp.wait() "ts":1820351011,"updates":[. ]>
>>> lp = UserLongPoll(session, mode=2) # default wait=25 >>> await lp.wait() "ts":1820350345,"updates":[. ]> >>> await lp.get_pts() # return pts 191231223 >>> await lp.get_pts(need_ts=True) # return pts, ts 191231223, 1820350345

You can iterate over events

>>> async for event in lp.iter(): . print(event) "type". "object": >

Notice that wait value only for long pool connection.

Real pause could be more wait time because of need time for authorization (if needed), reconnect and etc.

>>> api = API(session) >>> lp = BotsLongPoll(api, group_id=1) # default wait=25 >>> await lp.wait() "ts":345,"updates":[. ]> >>> await lp.wait() "ts":346,"updates":[. ]>
>>> lp = BotsLongPoll(session, group_id=1) # default wait=25 >>> await lp.wait() "ts":78455,"updates":[. ]> >>> await lp.get_pts() # return pts 191231223 >>> await lp.get_pts(need_ts=True) # return pts, ts 191231223, 1820350345

BotsLongPoll supports iterating too

>>> async for event in lp.iter(): . print(event) "type". "object": >

Notice that wait value only for long pool connection.

Real pause could be more wait time because of need time for authorization (if needed), reconnect and etc.

Async execute request pool

from aiovk2.pools import AsyncVkExecuteRequestPool async with AsyncVkExecuteRequestPool() as pool: response = pool.add_call('users.get', 'YOUR_TOKEN', 'user_ids': 1>) response2 = pool.add_call('users.get', 'YOUR_TOKEN', 'user_ids': 2>) response3 = pool.add_call('users.get', 'ANOTHER_TOKEN', 'user_ids': 1>) response4 = pool.add_call('users.get', 'ANOTHER_TOKEN', 'user_ids': -1>) >>> print(response.ok) True >>> print(response.result) ['id': 1, 'first_name': 'Павел', 'last_name': 'Дуров'>] >>> print(response2.result) ['id': 2, 'first_name': 'Александра', 'last_name': 'Владимирова'>] >>> print(response3.result) ['id': 1, 'first_name': 'Павел', 'last_name': 'Дуров'>] >>> print(response4.ok) False >>> print(response4.error) 'method': 'users.get', 'error_code': 113, 'error_msg': 'Invalid user id'>
from aiovk2.pools import AsyncVkExecuteRequestPool pool = AsyncVkExecuteRequestPool() response = pool.add_call('users.get', 'YOUR_TOKEN', 'user_ids': 1>) response2 = pool.add_call('users.get', 'YOUR_TOKEN', 'user_ids': 2>) response3 = pool.add_call('users.get', 'ANOTHER_TOKEN', 'user_ids': 1>) response4 = pool.add_call('users.get', 'ANOTHER_TOKEN', 'user_ids': -1>) await pool.execute() .

About

vk.com API python wrapper for asyncio

Источник

aiovk2 1.0.0

История выпусков Уведомления о выпусках | Лента RSS

Загрузка файлов

Загрузите файл для вашей платформы. Если вы не уверены, какой выбрать, узнайте больше об установке пакетов.

Source Distribution

Uploaded 19 янв. 2022 г. source

Built Distribution

Uploaded 19 янв. 2022 г. py3

Хеши для aiovk2-1.0.0.tar.gz

Хеши для aiovk2-1.0.0.tar.gz
Алгоритм Хеш-дайджест
SHA256 23616539576be1a1ac72bb86cd00ad61022ef7e8b48c03affcff89a80f91f6d3 Копировать
MD5 89217091a244454b4e0d8bfa9e9e33be Копировать
BLAKE2b-256 95b9d92886c4a26a89fd33c4bd8a5a8c1019ce1501ca8036d459786bd0bf33b6 Копировать

Хеши для aiovk2-1.0.0-py3-none-any.whl

Хеши для aiovk2-1.0.0-py3-none-any.whl
Алгоритм Хеш-дайджест
SHA256 f88fd99584585e51f5d661c55095d0697f47d8b835adf8831bfe65b911bb2fd5 Копировать
MD5 e329fb3dc435320899a9ef37bdb305d3 Копировать
BLAKE2b-256 bb200474e5deac81b1bbc60d98d778213e6d5e6214b7267a0497def7b20df0c9 Копировать

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Simple asyncio wrapper for vk_api

abrzv/vkreal

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Simple asynchronus wrapper for vk_api.

It isn’t ideal, but it works.

Орех

vkreal.VkApi(token, sess = None, v = «5.125»)

Token from vk api, that you can get, using oauth.vk.com

httpx.AsyncClient. You can use your own session, for example for using methods with proxy.

Creates and initializes ApiContext object with current VkApi object.

Method name. You can see a list of all methods here.

Parameters, with which method will be called. You can see it in https://vk.com/dev/method.name

Wrapper, for using api methods as ordinary class methods.

vkreal.VkLongPoll(api, v = «3», mode = 10, get_pts = False, loop = None)

VkApi object for longpoll initialization.

Asyncio loop, which will be used by event listener and handlers.

Saving credintails for longpoll.

Getting one not-parsed event from longpoll.

Longpoll-listening generator, yield parsed events.

Adding an async event handler.

Asynchronus callback function, that will handle events. on_event listener call this function with passing event in first argument.

vkreal.VkBotLongPoll(api, group_id, loop = None)

Group longpoll using the same functions, as user longpoll.

VkApi object for group longpoll initialization.

Id of group, in which longpoll will be setted.

Asyncio loop, which will be used by event listener and handlers.

Parsing array-event to dict.

Источник

Оцените статью