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 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
-------
.. code-block:: bash
pip install aiovk
Examples
========
Annotation
----------
In all the examples below, I will give only the ``{code}``
.. code-block:: python
async def func():
{code}
loop = asyncio.get_event_loop()
loop.run_until_complete(func())
Authorization
-------------
**TokenSession** - if you already have token or you use requests which don't require token
.. code-block:: python
session = TokenSession()
session = TokenSession(access_token='asdf123..')
**ImplicitSession** - client authorization in js apps and standalone (desktop and mobile) apps
.. code-block:: python
>>> session = ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID)
>>> await session.authorize()
>>> session.access_token
asdfa2321afsdf12eadasf123...
With scopes:
.. code-block:: python
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
See https://vk.com/dev/authcode_flow_user for getting the CODE
.. code-block:: python
>>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI, CODE)
>>> await session.authorize()
>>> session.access_token
asdfa2321afsdf12eadasf123...
Or:
.. code-block:: python
>>> 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
.. code-block:: python
async with aiovk.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
Drivers
-------
**HttpDriver** - default driver for using ``aiohttp``
.. code-block:: python
>>> driver = HttpDriver()
>>> driver = HttpDriver(timeout=10) # default timeout for all requests
.. code-block:: python
>>> 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:
.. code-block:: python
>>> session = TokenSession(..., driver=HttpDriver())
How to use driver with own loop:
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> class ExampleDriver(LimitRateDriverMixin, HttpDriver):
... requests_per_period = 3
... period = 1 #seconds
VK API
------
First variant:
.. code-block:: python
>>> session = TokenSession()
>>> api = API(session)
>>> await api.users.get(user_ids=1)
[{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]
Second variant:
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
>>> 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
.. code-block:: python
from aiovk.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
.. code-block:: python
from aiovk.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()
...
Raw data
{
"_id": null,
"home_page": "https://github.com/alexanderlarin/aiovk",
"name": "aiovk",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "vk.com api vk wrappper asyncio",
"author": "Alexander Larin",
"author_email": "ekzebox@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/70/a2/9ca19811b16e0d3e71326595afd5f3f7206419abdbab7e00b012b10bad3c/aiovk-4.1.0.tar.gz",
"platform": null,
"description": "vk.com API python wrapper for asyncio\r\n=====================================\r\nfor old version of python you can use https://github.com/dimka665/vk\r\n\r\nFeatures\r\n--------\r\n* asynchronous\r\n* support python 3.5+ versions\r\n* have only one dependency - ``aiohttp 3+``\r\n* support two-factor authentication\r\n* support socks proxy with ``aiohttp-socks``\r\n* support rate limit of requests\r\n* support Long Poll connection\r\n\r\nTODO\r\n----\r\n* need refactoring tests for ``AsyncVkExecuteRequestPool``\r\n\r\nInstall\r\n-------\r\n\r\n.. code-block:: bash\r\n\r\n pip install aiovk\r\n\r\nExamples\r\n========\r\nAnnotation\r\n----------\r\nIn all the examples below, I will give only the ``{code}``\r\n\r\n.. code-block:: python\r\n\r\n async def func():\r\n {code}\r\n\r\n loop = asyncio.get_event_loop()\r\n loop.run_until_complete(func())\r\n\r\n\r\nAuthorization\r\n-------------\r\n**TokenSession** - if you already have token or you use requests which don't require token\r\n\r\n.. code-block:: python\r\n\r\n session = TokenSession()\r\n session = TokenSession(access_token='asdf123..')\r\n\r\n**ImplicitSession** - client authorization in js apps and standalone (desktop and mobile) apps\r\n\r\n.. code-block:: python\r\n\r\n >>> session = ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID)\r\n >>> await session.authorize()\r\n >>> session.access_token\r\n asdfa2321afsdf12eadasf123...\r\n\r\nWith scopes:\r\n\r\n.. code-block:: python\r\n\r\n ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 'notify')\r\n ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 'notify,friends')\r\n ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, ['notify', 'friends'])\r\n ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 3) # notify and friends\r\n\r\nAlso you can use ``SimpleImplicitSessionMixin`` for entering confirmation code\r\nor captcha key\r\n\r\n**AuthorizationCodeSession** - authorization for server apps or Open API\r\n\r\nSee https://vk.com/dev/authcode_flow_user for getting the CODE\r\n\r\n.. code-block:: python\r\n\r\n >>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI, CODE)\r\n >>> await session.authorize()\r\n >>> session.access_token\r\n asdfa2321afsdf12eadasf123...\r\n\r\nOr:\r\n\r\n.. code-block:: python\r\n\r\n >>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI)\r\n >>> await session.authorize(CODE)\r\n >>> session.access_token\r\n asdfa2321afsdf12eadasf123...\r\n\r\n**Authorization using context manager** - you won't need to use session.close() after work\r\n\r\n.. code-block:: python\r\n\r\n async with aiovk.TokenSession(access_token=YOUR_VK_TOKEN) as ses:\r\n api = API(ses)...\r\n\r\nAnd your session will be closed after all done or code fail(similar to simple \"with\" usage)\r\nWorks with all types of authorization\r\n\r\nDrivers\r\n-------\r\n**HttpDriver** - default driver for using ``aiohttp``\r\n\r\n.. code-block:: python\r\n\r\n >>> driver = HttpDriver()\r\n >>> driver = HttpDriver(timeout=10) # default timeout for all requests\r\n\r\n.. code-block:: python\r\n\r\n >>> driver = ProxyDriver(PROXY_ADDRESS, PORT) # 1234 is port\r\n >>> driver = ProxyDriver(PROXY_ADDRESS, PORT, timeout=10)\r\n >>> driver = ProxyDriver(PROXY_ADDRESS, PORT, PROXY_LOGIN, PROXY_PASSWORD, timeout=10)\r\n\r\nHow to use custom driver with session:\r\n\r\n.. code-block:: python\r\n\r\n >>> session = TokenSession(..., driver=HttpDriver())\r\n\r\nHow to use driver with own loop:\r\n\r\n.. code-block:: python\r\n\r\n >>> loop = asyncio.get_event_loop()\r\n >>> asyncio.set_event_loop(None)\r\n >>> session = TokenSession(driver=HttpDriver(loop=loop)) # or ProxyDriver\r\n\r\nHow to use driver with custom http session object:\r\n\r\nSolve next problem: https://stackoverflow.com/questions/29827642/asynchronous-aiohttp-requests-fails-but-synchronous-requests-succeed\r\n\r\n.. code-block:: python\r\n\r\n >>> connector = aiohttp.TCPConnector(verify_ssl=False)\r\n >>> session = aiohttp.ClientSession(connector=connector)\r\n >>> driver = HttpDriver(loop=loop, session=session)\r\n\r\n\r\n**LimitRateDriverMixin** - mixin class what allow you create new drivers with speed rate limits\r\n\r\n.. code-block:: python\r\n\r\n >>> class ExampleDriver(LimitRateDriverMixin, HttpDriver):\r\n ... requests_per_period = 3\r\n ... period = 1 #seconds\r\n\r\nVK API\r\n------\r\nFirst variant:\r\n\r\n.. code-block:: python\r\n\r\n >>> session = TokenSession()\r\n >>> api = API(session)\r\n >>> await api.users.get(user_ids=1)\r\n [{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]\r\n\r\nSecond variant:\r\n\r\n.. code-block:: python\r\n\r\n >>> session = TokenSession()\r\n >>> api = API(session)\r\n >>> await api('users.get', user_ids=1)\r\n [{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]\r\n\r\nAlso you can add ``timeout`` argument for each request or define it in the session\r\n\r\nSee https://vk.com/dev/methods for detailed API guide.\r\n\r\nLazy VK API\r\n-----------\r\nIt is useful when a bot has a large message flow\r\n\r\n.. code-block:: python\r\n\r\n >>> session = TokenSession()\r\n >>> api = LazyAPI(session)\r\n >>> message = api.users.get(user_ids=1)\r\n >>> await message()\r\n [{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]\r\n\r\nSupports both variants like API object\r\n\r\nUser Long Poll\r\n--------------\r\nFor documentation, see: https://vk.com/dev/using_longpoll\r\n\r\nUse exist API object\r\n\r\n.. code-block:: python\r\n\r\n >>> api = API(session)\r\n >>> lp = UserLongPoll(api, mode=2) # default wait=25\r\n >>> await lp.wait()\r\n {\"ts\":1820350345,\"updates\":[...]}\r\n >>> await lp.wait()\r\n {\"ts\":1820351011,\"updates\":[...]}\r\n\r\nUse Session object\r\n\r\n.. code-block:: python\r\n\r\n >>> lp = UserLongPoll(session, mode=2) # default wait=25\r\n >>> await lp.wait()\r\n {\"ts\":1820350345,\"updates\":[...]}\r\n >>> await lp.get_pts() # return pts\r\n 191231223\r\n >>> await lp.get_pts(need_ts=True) # return pts, ts\r\n 191231223, 1820350345\r\n\r\nYou can iterate over events\r\n\r\n.. code-block:: python\r\n\r\n >>> async for event in lp.iter():\r\n ... print(event)\r\n {\"type\":..., \"object\": {...}}\r\n\r\nNotice that ``wait`` value only for long pool connection.\r\n\r\nReal pause could be more ``wait`` time because of need time\r\nfor authorization (if needed), reconnect and etc.\r\n\r\nBots Long Poll\r\n--------------\r\nFor documentation, see: https://vk.com/dev/bots_longpoll\r\n\r\nUse exist API object\r\n\r\n.. code-block:: python\r\n\r\n >>> api = API(session)\r\n >>> lp = BotsLongPoll(api, group_id=1) # default wait=25\r\n >>> await lp.wait()\r\n {\"ts\":345,\"updates\":[...]}\r\n >>> await lp.wait()\r\n {\"ts\":346,\"updates\":[...]}\r\n\r\nUse Session object\r\n\r\n.. code-block:: python\r\n\r\n >>> lp = BotsLongPoll(session, group_id=1) # default wait=25\r\n >>> await lp.wait()\r\n {\"ts\":78455,\"updates\":[...]}\r\n >>> await lp.get_pts() # return pts\r\n 191231223\r\n >>> await lp.get_pts(need_ts=True) # return pts, ts\r\n 191231223, 1820350345\r\n\r\nBotsLongPoll supports iterating too\r\n\r\n.. code-block:: python\r\n\r\n >>> async for event in lp.iter():\r\n ... print(event)\r\n {\"type\":..., \"object\": {...}}\r\n\r\nNotice that ``wait`` value only for long pool connection.\r\n\r\nReal pause could be more ``wait`` time because of need time\r\nfor authorization (if needed), reconnect and etc.\r\n\r\nAsync execute request pool\r\n--------------------------\r\nFor documentation, see: https://vk.com/dev/execute\r\n\r\n.. code-block:: python\r\n\r\n from aiovk.pools import AsyncVkExecuteRequestPool\r\n\r\n async with AsyncVkExecuteRequestPool() as pool:\r\n response = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 1})\r\n response2 = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 2})\r\n response3 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': 1})\r\n response4 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': -1})\r\n\r\n >>> print(response.ok)\r\n True\r\n >>> print(response.result)\r\n [{'id': 1, 'first_name': '\u041f\u0430\u0432\u0435\u043b', 'last_name': '\u0414\u0443\u0440\u043e\u0432'}]\r\n >>> print(response2.result)\r\n [{'id': 2, 'first_name': '\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440\u0430', 'last_name': '\u0412\u043b\u0430\u0434\u0438\u043c\u0438\u0440\u043e\u0432\u0430'}]\r\n >>> print(response3.result)\r\n [{'id': 1, 'first_name': '\u041f\u0430\u0432\u0435\u043b', 'last_name': '\u0414\u0443\u0440\u043e\u0432'}]\r\n >>> print(response4.ok)\r\n False\r\n >>> print(response4.error)\r\n {'method': 'users.get', 'error_code': 113, 'error_msg': 'Invalid user id'}\r\n\r\nor\r\n\r\n.. code-block:: python\r\n\r\n from aiovk.pools import AsyncVkExecuteRequestPool\r\n\r\n pool = AsyncVkExecuteRequestPool()\r\n response = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 1})\r\n response2 = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 2})\r\n response3 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': 1})\r\n response4 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': -1})\r\n await pool.execute()\r\n ...\r\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "vk.com API python wrapper for asyncio",
"version": "4.1.0",
"split_keywords": [
"vk.com",
"api",
"vk",
"wrappper",
"asyncio"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b7861ca6bd9c49f9f63164008e67f405ba1a75c8a9b81d58e98e060fbf27f35d",
"md5": "bdf1b652e2eddd1d641b21952ded5b96",
"sha256": "21a8a405bfc075866e3a8744d143d7421ae758dcd4185e3abfe0e2ad4b7a9a0e"
},
"downloads": -1,
"filename": "aiovk-4.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bdf1b652e2eddd1d641b21952ded5b96",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 23567,
"upload_time": "2023-04-10T12:11:11",
"upload_time_iso_8601": "2023-04-10T12:11:11.605288Z",
"url": "https://files.pythonhosted.org/packages/b7/86/1ca6bd9c49f9f63164008e67f405ba1a75c8a9b81d58e98e060fbf27f35d/aiovk-4.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "70a29ca19811b16e0d3e71326595afd5f3f7206419abdbab7e00b012b10bad3c",
"md5": "471fe598886e39cd71d166a307c9c120",
"sha256": "713365a0054b30b42cd47dcd54d21af75a50e561e05f39b1bb1f88e0f4f67939"
},
"downloads": -1,
"filename": "aiovk-4.1.0.tar.gz",
"has_sig": false,
"md5_digest": "471fe598886e39cd71d166a307c9c120",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 25045,
"upload_time": "2023-04-10T12:11:13",
"upload_time_iso_8601": "2023-04-10T12:11:13.920340Z",
"url": "https://files.pythonhosted.org/packages/70/a2/9ca19811b16e0d3e71326595afd5f3f7206419abdbab7e00b012b10bad3c/aiovk-4.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-10 12:11:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "alexanderlarin",
"github_project": "aiovk",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "aiovk"
}