vk.com API python wrapper for asyncio
Project description
vk.com API python wrapper for asyncio
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
support two-factor authentication
support socks proxy with aiosocks
support rate limit of requests
support Long Poll connection
Install
pip install aiovk
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 = Socks5Driver(PROXY_ADDRESS, PORT) # 1234 is port
>>> driver = Socks5Driver(PROXY_ADDRESS, PORT, timeout=10)
>>> driver = Socks5Driver(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 Socks5Driver
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
Long Poll
Use exist API object
>>> api = API(session)
>>> lp = LongPoll(api, mode=2) # default wait=25
>>> await lp.wait()
{"ts":1820350345,"updates":[...]}
>>> await lp.wait()
{"ts":1820351011,"updates":[...]}
Use Session object
>>> lp = LongPoll(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
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.
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 aiovk_varnar_fork-2.1.0.tar.gz
.
File metadata
- Download URL: aiovk_varnar_fork-2.1.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ec6ce58924c3425e9abb93da9fbebf17102d65103e4c3d9422285c2b00dd450 |
|
MD5 | 8b52f2b3d8e41bfa1d1ad750a10156e1 |
|
BLAKE2b-256 | 6db494a13c7bd67fab5f67ff1feff4bb434c86d4d430c3408b34688e63c2e73d |
File details
Details for the file aiovk_varnar_fork-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: aiovk_varnar_fork-2.1.0-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e17e97338b1a03b406cf8b7dedbda7d07e55cb929ca41984793a6c64e188c297 |
|
MD5 | c6d136ba08dab42e3fcec95b921fb536 |
|
BLAKE2b-256 | b60f858b33f672fc6a3934b4419e47f8420a3d19bdd4989fa8c76e89679af6fe |