vk.com API python wrapper for asyncio
Project description
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
Features
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
TODO
need refactoring tests for AsyncVkExecuteRequestPool
Install
pip install aiovk2
Examples
Annotation
In all the examples below, I will give only the {code}
async def func():
{code}
loop = asyncio.get_event_loop()
loop.run_until_complete(func())
Drivers
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:
Solve next problem: https://stackoverflow.com/questions/29827642/asynchronous-aiohttp-requests-fails-but-synchronous-requests-succeed
>>> 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
VK API
First variant:
>>> session = TokenSession()
>>> api = API(session)
>>> await api.users.get(user_ids=1)
[{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]
Second variant:
>>> 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
See https://vk.com/dev/methods for detailed API guide.
Lazy VK API
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
User Long Poll
For documentation, see: https://vk.com/dev/using_longpoll
Use exist 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":[...]}
Use Session object
>>> 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.
Bots Long Poll
For documentation, see: https://vk.com/dev/bots_longpoll
Use exist API object
>>> api = API(session)
>>> lp = BotsLongPoll(api, group_id=1) # default wait=25
>>> await lp.wait()
{"ts":345,"updates":[...]}
>>> await lp.wait()
{"ts":346,"updates":[...]}
Use Session object
>>> 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
For documentation, see: https://vk.com/dev/execute
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'}
or
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()
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiovk2-1.0.0.tar.gz
.
File metadata
- Download URL: aiovk2-1.0.0.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23616539576be1a1ac72bb86cd00ad61022ef7e8b48c03affcff89a80f91f6d3 |
|
MD5 | 89217091a244454b4e0d8bfa9e9e33be |
|
BLAKE2b-256 | 95b9d92886c4a26a89fd33c4bd8a5a8c1019ce1501ca8036d459786bd0bf33b6 |
File details
Details for the file aiovk2-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: aiovk2-1.0.0-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f88fd99584585e51f5d661c55095d0697f47d8b835adf8831bfe65b911bb2fd5 |
|
MD5 | e329fb3dc435320899a9ef37bdb305d3 |
|
BLAKE2b-256 | bb200474e5deac81b1bbc60d98d778213e6d5e6214b7267a0497def7b20df0c9 |