├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.rst ├── aiovk ├── __init__.py ├── api.py ├── drivers.py ├── exceptions.py ├── longpoll.py ├── mixins.py ├── parser.py ├── pools.py ├── sessions.py └── shaping.py ├── requirements-dev.txt ├── requirements.txt ├── setup.py └── tests ├── __init__.py ├── functional ├── __init__.py ├── conftest.py ├── test_drivers.py ├── test_longpool.py └── test_sessions.py ├── responses ├── auth_redirect.jinja2 ├── authorize_page.jinja2 └── blank.jinja2 ├── unit ├── __init__.py ├── test_api.py ├── test_longpool.py ├── test_pools.py └── test_shaping.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Project files 3 | # auth data 4 | .pytest_cache/ 5 | 6 | ### Python template 7 | # Byte-compiled / optimized / DLL files 8 | __pycache__/ 9 | *.py[cod] 10 | *$py.class 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | env/ 18 | build/ 19 | develop-eggs/ 20 | dist/ 21 | downloads/ 22 | eggs/ 23 | .eggs/ 24 | lib/ 25 | lib64/ 26 | parts/ 27 | sdist/ 28 | var/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | 33 | # PyInstaller 34 | # Usually these files are written by a python script from a template 35 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 36 | *.manifest 37 | *.spec 38 | 39 | # Installer logs 40 | pip-log.txt 41 | pip-delete-this-directory.txt 42 | 43 | # Unit test / coverage reports 44 | htmlcov/ 45 | .tox/ 46 | .coverage 47 | .coverage.* 48 | .cache 49 | nosetests.xml 50 | coverage.xml 51 | *,cover 52 | .hypothesis/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | 62 | # Flask stuff: 63 | instance/ 64 | .webassets-cache 65 | 66 | # Scrapy stuff: 67 | .scrapy 68 | 69 | # Sphinx documentation 70 | docs/_build/ 71 | 72 | # PyBuilder 73 | target/ 74 | 75 | # IPython Notebook 76 | .ipynb_checkpoints 77 | 78 | # pyenv 79 | .python-version 80 | 81 | # celery beat schedule file 82 | celerybeat-schedule 83 | 84 | # dotenv 85 | .env 86 | 87 | # virtualenv 88 | venv/ 89 | ENV/ 90 | 91 | # Spyder project settings 92 | .spyderproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | ### JetBrains template 97 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 98 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 99 | .idea/ 100 | # User-specific stuff: 101 | .idea/workspace.xml 102 | .idea/tasks.xml 103 | .idea/dictionaries 104 | .idea/vcs.xml 105 | .idea/jsLibraryMappings.xml 106 | 107 | # Sensitive or high-churn files: 108 | .idea/dataSources.ids 109 | .idea/dataSources.xml 110 | .idea/dataSources.local.xml 111 | .idea/sqlDataSources.xml 112 | .idea/dynamic.xml 113 | .idea/uiDesigner.xml 114 | 115 | # Gradle: 116 | .idea/gradle.xml 117 | .idea/libraries 118 | 119 | # Mongo Explorer plugin: 120 | .idea/mongoSettings.xml 121 | 122 | ## File-based project format: 123 | *.iws 124 | 125 | ## Plugin-specific files: 126 | 127 | # IntelliJ 128 | /out/ 129 | 130 | # mpeltonen/sbt-idea plugin 131 | .idea_modules/ 132 | 133 | # JIRA plugin 134 | atlassian-ide-plugin.xml 135 | 136 | # Crashlytics plugin (for Android Studio and IntelliJ) 137 | com_crashlytics_export_strings.xml 138 | crashlytics.properties 139 | crashlytics-build.properties 140 | fabric.properties 141 | 142 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Eldar Fahreev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include requirements.txt 2 | include requirements-dev.txt 3 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | vk.com API python wrapper for asyncio 2 | ===================================== 3 | for old version of python you can use https://github.com/dimka665/vk 4 | 5 | Features 6 | -------- 7 | * asynchronous 8 | * support python 3.5+ versions 9 | * have only one dependency - ``aiohttp 3+`` 10 | * support two-factor authentication 11 | * support socks proxy with ``aiohttp-socks`` 12 | * support rate limit of requests 13 | * support Long Poll connection 14 | 15 | TODO 16 | ---- 17 | * need refactoring tests for ``AsyncVkExecuteRequestPool`` 18 | 19 | Install 20 | ------- 21 | 22 | .. code-block:: bash 23 | 24 | pip install aiovk 25 | 26 | Examples 27 | ======== 28 | Annotation 29 | ---------- 30 | In all the examples below, I will give only the ``{code}`` 31 | 32 | .. code-block:: python 33 | 34 | async def func(): 35 | {code} 36 | 37 | loop = asyncio.get_event_loop() 38 | loop.run_until_complete(func()) 39 | 40 | 41 | Authorization 42 | ------------- 43 | **TokenSession** - if you already have token or you use requests which don't require token 44 | 45 | .. code-block:: python 46 | 47 | session = TokenSession() 48 | session = TokenSession(access_token='asdf123..') 49 | 50 | **ImplicitSession** - client authorization in js apps and standalone (desktop and mobile) apps 51 | 52 | .. code-block:: python 53 | 54 | >>> session = ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID) 55 | >>> await session.authorize() 56 | >>> session.access_token 57 | asdfa2321afsdf12eadasf123... 58 | 59 | With scopes: 60 | 61 | .. code-block:: python 62 | 63 | ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 'notify') 64 | ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 'notify,friends') 65 | ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, ['notify', 'friends']) 66 | ImplicitSession(USER_LOGIN, USER_PASSWORD, APP_ID, 3) # notify and friends 67 | 68 | Also you can use ``SimpleImplicitSessionMixin`` for entering confirmation code 69 | or captcha key 70 | 71 | **AuthorizationCodeSession** - authorization for server apps or Open API 72 | 73 | See https://vk.com/dev/authcode_flow_user for getting the CODE 74 | 75 | .. code-block:: python 76 | 77 | >>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI, CODE) 78 | >>> await session.authorize() 79 | >>> session.access_token 80 | asdfa2321afsdf12eadasf123... 81 | 82 | Or: 83 | 84 | .. code-block:: python 85 | 86 | >>> session = AuthorizationCodeSession(APP_ID, APP_SECRET, REDIRECT_URI) 87 | >>> await session.authorize(CODE) 88 | >>> session.access_token 89 | asdfa2321afsdf12eadasf123... 90 | 91 | **Authorization using context manager** - you won't need to use session.close() after work 92 | 93 | .. code-block:: python 94 | 95 | async with aiovk.TokenSession(access_token=YOUR_VK_TOKEN) as ses: 96 | api = API(ses)... 97 | 98 | And your session will be closed after all done or code fail(similar to simple "with" usage) 99 | Works with all types of authorization 100 | 101 | Drivers 102 | ------- 103 | **HttpDriver** - default driver for using ``aiohttp`` 104 | 105 | .. code-block:: python 106 | 107 | >>> driver = HttpDriver() 108 | >>> driver = HttpDriver(timeout=10) # default timeout for all requests 109 | 110 | .. code-block:: python 111 | 112 | >>> driver = ProxyDriver(PROXY_ADDRESS, PORT) # 1234 is port 113 | >>> driver = ProxyDriver(PROXY_ADDRESS, PORT, timeout=10) 114 | >>> driver = ProxyDriver(PROXY_ADDRESS, PORT, PROXY_LOGIN, PROXY_PASSWORD, timeout=10) 115 | 116 | How to use custom driver with session: 117 | 118 | .. code-block:: python 119 | 120 | >>> session = TokenSession(..., driver=HttpDriver()) 121 | 122 | How to use driver with own loop: 123 | 124 | .. code-block:: python 125 | 126 | >>> loop = asyncio.get_event_loop() 127 | >>> asyncio.set_event_loop(None) 128 | >>> session = TokenSession(driver=HttpDriver(loop=loop)) # or ProxyDriver 129 | 130 | How to use driver with custom http session object: 131 | 132 | Solve next problem: https://stackoverflow.com/questions/29827642/asynchronous-aiohttp-requests-fails-but-synchronous-requests-succeed 133 | 134 | .. code-block:: python 135 | 136 | >>> connector = aiohttp.TCPConnector(verify_ssl=False) 137 | >>> session = aiohttp.ClientSession(connector=connector) 138 | >>> driver = HttpDriver(loop=loop, session=session) 139 | 140 | 141 | **LimitRateDriverMixin** - mixin class what allow you create new drivers with speed rate limits 142 | 143 | .. code-block:: python 144 | 145 | >>> class ExampleDriver(LimitRateDriverMixin, HttpDriver): 146 | ... requests_per_period = 3 147 | ... period = 1 #seconds 148 | 149 | VK API 150 | ------ 151 | First variant: 152 | 153 | .. code-block:: python 154 | 155 | >>> session = TokenSession() 156 | >>> api = API(session) 157 | >>> await api.users.get(user_ids=1) 158 | [{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}] 159 | 160 | Second variant: 161 | 162 | .. code-block:: python 163 | 164 | >>> session = TokenSession() 165 | >>> api = API(session) 166 | >>> await api('users.get', user_ids=1) 167 | [{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}] 168 | 169 | Also you can add ``timeout`` argument for each request or define it in the session 170 | 171 | See https://vk.com/dev/methods for detailed API guide. 172 | 173 | Lazy VK API 174 | ----------- 175 | It is useful when a bot has a large message flow 176 | 177 | .. code-block:: python 178 | 179 | >>> session = TokenSession() 180 | >>> api = LazyAPI(session) 181 | >>> message = api.users.get(user_ids=1) 182 | >>> await message() 183 | [{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}] 184 | 185 | Supports both variants like API object 186 | 187 | User Long Poll 188 | -------------- 189 | For documentation, see: https://vk.com/dev/using_longpoll 190 | 191 | Use exist API object 192 | 193 | .. code-block:: python 194 | 195 | >>> api = API(session) 196 | >>> lp = UserLongPoll(api, mode=2) # default wait=25 197 | >>> await lp.wait() 198 | {"ts":1820350345,"updates":[...]} 199 | >>> await lp.wait() 200 | {"ts":1820351011,"updates":[...]} 201 | 202 | Use Session object 203 | 204 | .. code-block:: python 205 | 206 | >>> lp = UserLongPoll(session, mode=2) # default wait=25 207 | >>> await lp.wait() 208 | {"ts":1820350345,"updates":[...]} 209 | >>> await lp.get_pts() # return pts 210 | 191231223 211 | >>> await lp.get_pts(need_ts=True) # return pts, ts 212 | 191231223, 1820350345 213 | 214 | You can iterate over events 215 | 216 | .. code-block:: python 217 | 218 | >>> async for event in lp.iter(): 219 | ... print(event) 220 | {"type":..., "object": {...}} 221 | 222 | Notice that ``wait`` value only for long pool connection. 223 | 224 | Real pause could be more ``wait`` time because of need time 225 | for authorization (if needed), reconnect and etc. 226 | 227 | Bots Long Poll 228 | -------------- 229 | For documentation, see: https://vk.com/dev/bots_longpoll 230 | 231 | Use exist API object 232 | 233 | .. code-block:: python 234 | 235 | >>> api = API(session) 236 | >>> lp = BotsLongPoll(api, group_id=1) # default wait=25 237 | >>> await lp.wait() 238 | {"ts":345,"updates":[...]} 239 | >>> await lp.wait() 240 | {"ts":346,"updates":[...]} 241 | 242 | Use Session object 243 | 244 | .. code-block:: python 245 | 246 | >>> lp = BotsLongPoll(session, group_id=1) # default wait=25 247 | >>> await lp.wait() 248 | {"ts":78455,"updates":[...]} 249 | >>> await lp.get_pts() # return pts 250 | 191231223 251 | >>> await lp.get_pts(need_ts=True) # return pts, ts 252 | 191231223, 1820350345 253 | 254 | BotsLongPoll supports iterating too 255 | 256 | .. code-block:: python 257 | 258 | >>> async for event in lp.iter(): 259 | ... print(event) 260 | {"type":..., "object": {...}} 261 | 262 | Notice that ``wait`` value only for long pool connection. 263 | 264 | Real pause could be more ``wait`` time because of need time 265 | for authorization (if needed), reconnect and etc. 266 | 267 | Async execute request pool 268 | -------------------------- 269 | For documentation, see: https://vk.com/dev/execute 270 | 271 | .. code-block:: python 272 | 273 | from aiovk.pools import AsyncVkExecuteRequestPool 274 | 275 | async with AsyncVkExecuteRequestPool() as pool: 276 | response = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 1}) 277 | response2 = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 2}) 278 | response3 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': 1}) 279 | response4 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': -1}) 280 | 281 | >>> print(response.ok) 282 | True 283 | >>> print(response.result) 284 | [{'id': 1, 'first_name': 'Павел', 'last_name': 'Дуров'}] 285 | >>> print(response2.result) 286 | [{'id': 2, 'first_name': 'Александра', 'last_name': 'Владимирова'}] 287 | >>> print(response3.result) 288 | [{'id': 1, 'first_name': 'Павел', 'last_name': 'Дуров'}] 289 | >>> print(response4.ok) 290 | False 291 | >>> print(response4.error) 292 | {'method': 'users.get', 'error_code': 113, 'error_msg': 'Invalid user id'} 293 | 294 | or 295 | 296 | .. code-block:: python 297 | 298 | from aiovk.pools import AsyncVkExecuteRequestPool 299 | 300 | pool = AsyncVkExecuteRequestPool() 301 | response = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 1}) 302 | response2 = pool.add_call('users.get', 'YOUR_TOKEN', {'user_ids': 2}) 303 | response3 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': 1}) 304 | response4 = pool.add_call('users.get', 'ANOTHER_TOKEN', {'user_ids': -1}) 305 | await pool.execute() 306 | ... 307 | -------------------------------------------------------------------------------- /aiovk/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = '4.1.0' 2 | 3 | from .api import API 4 | from .sessions import ImplicitSession, TokenSession, AuthorizationCodeSession 5 | from .longpoll import LongPoll 6 | -------------------------------------------------------------------------------- /aiovk/api.py: -------------------------------------------------------------------------------- 1 | from functools import partial 2 | 3 | 4 | class API: 5 | def __init__(self, session): 6 | self._session = session 7 | 8 | def __getattr__(self, method_name): 9 | return Request(self, method_name) 10 | 11 | async def __call__(self, method_name, **method_kwargs): 12 | return await getattr(self, method_name)(**method_kwargs) 13 | 14 | 15 | class Request: 16 | __slots__ = ('_api', '_method_name', '_method_args') 17 | 18 | def __init__(self, api, method_name): 19 | self._api = api 20 | self._method_name = method_name 21 | 22 | def __getattr__(self, method_name): 23 | return Request(self._api, self._method_name + '.' + method_name) 24 | 25 | async def __call__(self, **method_args): 26 | timeout = method_args.pop('timeout', None) 27 | need_raw_response = method_args.pop('raw_response', False) 28 | self._method_args = method_args 29 | return await self._api._session.send_api_request(self._method_name, method_args, timeout, need_raw_response) 30 | 31 | 32 | class LazyAPI: 33 | def __init__(self, session): 34 | self._session = session 35 | 36 | def __getattr__(self, method_name): 37 | return LazyRequest(self, method_name) 38 | 39 | def __call__(self, method_name, **method_kwargs): 40 | return getattr(self, method_name)(**method_kwargs) 41 | 42 | 43 | class LazyRequest: 44 | __slots__ = ('_api', '_method_name', '_method_args') 45 | 46 | def __init__(self, api, method_name): 47 | self._api = api 48 | self._method_name = method_name 49 | 50 | def __getattr__(self, method_name): 51 | return LazyRequest(self._api, self._method_name + '.' + method_name) 52 | 53 | def __call__(self, **method_args): 54 | timeout = method_args.pop('timeout', None) 55 | self._method_args = method_args 56 | return partial( 57 | self._api._session.send_api_request, 58 | self._method_name, 59 | method_args, 60 | timeout 61 | ) 62 | -------------------------------------------------------------------------------- /aiovk/drivers.py: -------------------------------------------------------------------------------- 1 | import aiohttp 2 | 3 | try: 4 | from aiohttp_socks.connector import ProxyConnector 5 | except ImportError as e: 6 | ProxyConnector = None 7 | 8 | 9 | class BaseDriver: 10 | def __init__(self, timeout=10, loop=None): 11 | self.timeout = timeout 12 | self._loop = loop 13 | 14 | async def post_json(self, url, params, headers=None, timeout=None): 15 | """ 16 | :param params: dict of query params 17 | :return: http status code, dict from json response 18 | """ 19 | raise NotImplementedError 20 | 21 | async def get_bin(self, url, params, headers=None, timeout=None): 22 | """ 23 | :param params: dict of query params 24 | :return: http status code, binary body of response 25 | """ 26 | raise NotImplementedError 27 | 28 | async def get_text(self, url, params, headers=None, timeout=None): 29 | """ 30 | :param params: dict of query params 31 | :return: http status code, text body of response and redirect_url 32 | """ 33 | raise NotImplementedError 34 | 35 | async def post_text(self, url, data, headers=None, timeout=None): 36 | """ 37 | :param data: dict pr string 38 | :return: http status code, text body of response and redirect url 39 | """ 40 | raise NotImplementedError 41 | 42 | async def close(self): 43 | raise NotImplementedError 44 | 45 | 46 | class HttpDriver(BaseDriver): 47 | def __init__(self, timeout=10, loop=None, session=None): 48 | super().__init__(timeout, loop) 49 | if not session: 50 | self.session = aiohttp.ClientSession(loop=loop) 51 | else: 52 | self.session = session 53 | 54 | async def post_json(self, url, params, headers=None, timeout=None): 55 | async with self.session.post(url, data=params, headers=headers, timeout=timeout or self.timeout) as response: 56 | return response.status, await response.json() 57 | 58 | async def get_bin(self, url, params, headers=None, timeout=None): 59 | async with self.session.get(url, params=params, headers=headers, timeout=timeout or self.timeout) as response: 60 | return response.status, await response.read() 61 | 62 | async def get_text(self, url, params, headers=None, timeout=None): 63 | async with self.session.get(url, params=params, headers=headers, timeout=timeout or self.timeout) as response: 64 | return response.status, await response.text(), response.real_url 65 | 66 | async def post_text(self, url, data, headers=None, timeout=None): 67 | async with self.session.post(url, data=data, headers=headers, timeout=timeout or self.timeout) as response: 68 | return response.status, await response.text(), response.real_url 69 | 70 | async def close(self): 71 | await self.session.close() 72 | 73 | 74 | class ProxyDriver(HttpDriver): 75 | connector = ProxyConnector 76 | 77 | def __init__(self, address, port, login=None, password=None, timeout=10, **kwargs): 78 | connector = ProxyConnector( 79 | host=address, 80 | port=port, 81 | username=login, 82 | password=password, 83 | **kwargs 84 | ) 85 | session = aiohttp.ClientSession(connector=connector) 86 | super().__init__(timeout, kwargs.get('loop'), session) 87 | -------------------------------------------------------------------------------- /aiovk/exceptions.py: -------------------------------------------------------------------------------- 1 | from urllib.parse import urlencode 2 | 3 | 4 | CAPTCHA_IS_NEEDED = 14 5 | AUTHORIZATION_FAILED = 5 # invalid access token 6 | 7 | 8 | class VkException(Exception): 9 | pass 10 | 11 | 12 | class VkAuthError(VkException): 13 | def __init__(self, error, description, url='', params=''): 14 | self.error = error 15 | self.description = description 16 | self.url = "{}?{}".format(url, urlencode(params)) 17 | 18 | def __str__(self): 19 | return self.description 20 | 21 | 22 | class VkCaptchaNeeded(VkException): 23 | def __init__(self, url, sid): 24 | self.url = url 25 | self.sid = sid 26 | 27 | def __str__(self): 28 | return "You must enter the captcha" 29 | 30 | 31 | class VkTwoFactorCodeNeeded(VkException): 32 | def __str__(self): 33 | return "In order to confirm that you are the owner of this page " \ 34 | "please enter the code provided by the code generating app." 35 | 36 | 37 | class VkAPIError(VkException): 38 | def __init__(self, error, url): 39 | self.error_code = error.get('error_code') 40 | self.error_msg = error.get('error_msg') 41 | self.params = {param['key']: param['value'] for param in error.get('request_params', [])} 42 | self.url = url 43 | 44 | 45 | class VkLongPollError(VkException): 46 | def __init__(self, error, description, url='', params=''): 47 | self.error = error 48 | self.description = description 49 | self.url = "{}?{}".format(url, urlencode(params)) 50 | 51 | def __str__(self): 52 | return self.description 53 | -------------------------------------------------------------------------------- /aiovk/longpoll.py: -------------------------------------------------------------------------------- 1 | import json 2 | from abc import ABC, abstractmethod 3 | from typing import Union, Optional 4 | 5 | from . import API 6 | from .api import LazyAPI 7 | from .exceptions import VkLongPollError 8 | 9 | 10 | class BaseLongPoll(ABC): 11 | """Interface for all types of Longpoll API""" 12 | def __init__(self, session_or_api, mode: Optional[Union[int, list]], 13 | wait: int = 25, version: int = 2, timeout: int = None): 14 | """ 15 | :param session_or_api: session object or data for creating a new session 16 | :type session_or_api: BaseSession or API or LazyAPI 17 | :param mode: additional answer options 18 | :param wait: waiting period 19 | :param version: protocol version 20 | :param timeout: timeout for *.getLongPollServer request in current session 21 | """ 22 | if isinstance(session_or_api, (API, LazyAPI)): 23 | self.api = session_or_api 24 | else: 25 | self.api = API(session_or_api) 26 | 27 | self.timeout = timeout or self.api._session.timeout 28 | 29 | if type(mode) == list: 30 | mode = sum(mode) 31 | 32 | self.base_params = { 33 | 'version': version, 34 | 'wait': wait, 35 | 'act': 'a_check' 36 | } 37 | 38 | if mode is not None: 39 | self.base_params['mode'] = mode 40 | 41 | self.pts = None 42 | self.ts = None 43 | self.key = None 44 | self.base_url = None 45 | 46 | @abstractmethod 47 | async def _get_long_poll_server(self, need_pts: bool = False) -> None: 48 | """Send *.getLongPollServer request and update internal data 49 | 50 | :param need_pts: need return the pts field 51 | """ 52 | 53 | async def wait(self, need_pts=False) -> dict: 54 | """Send long poll request 55 | 56 | :param need_pts: need return the pts field 57 | """ 58 | if not self.base_url: 59 | await self._get_long_poll_server(need_pts) 60 | 61 | params = { 62 | 'ts': self.ts, 63 | 'key': self.key, 64 | } 65 | params.update(self.base_params) 66 | # invalid mimetype from server 67 | status, response, _ = await self.api._session.driver.get_text( 68 | self.base_url, params, 69 | timeout=2 * self.base_params['wait'] 70 | ) 71 | 72 | if status == 403: 73 | raise VkLongPollError(403, 'smth weth wrong', self.base_url + '/', params) 74 | 75 | response = json.loads(response) 76 | failed = response.get('failed') 77 | 78 | if not failed: 79 | self.ts = response['ts'] 80 | return response 81 | 82 | if failed == 1: 83 | self.ts = response['ts'] 84 | elif failed == 4: 85 | raise VkLongPollError( 86 | 4, 87 | 'An invalid version number was passed in the version parameter', 88 | self.base_url + '/', 89 | params 90 | ) 91 | else: 92 | self.base_url = None 93 | 94 | return await self.wait() 95 | 96 | async def iter(self): 97 | while True: 98 | response = await self.wait() 99 | for event in response['updates']: 100 | yield event 101 | 102 | async def get_pts(self, need_ts=False): 103 | if not self.base_url or not self.pts: 104 | await self._get_long_poll_server(need_pts=True) 105 | 106 | if need_ts: 107 | return self.pts, self.ts 108 | return self.pts 109 | 110 | 111 | class UserLongPoll(BaseLongPoll): 112 | """Implements https://vk.com/dev/using_longpoll""" 113 | # False for testing 114 | use_https = True 115 | 116 | async def _get_long_poll_server(self, need_pts=False): 117 | response = await self.api('messages.getLongPollServer', need_pts=int(need_pts), timeout=self.timeout) 118 | self.pts = response.get('pts') 119 | self.ts = response['ts'] 120 | self.key = response['key'] 121 | # fucking differences between long poll methods in vk api! 122 | self.base_url = f'http{"s" if self.use_https else ""}://{response["server"]}' 123 | 124 | 125 | class LongPoll(UserLongPoll): 126 | """Implements https://vk.com/dev/using_longpoll 127 | 128 | This class for backward compatibility 129 | """ 130 | 131 | 132 | class BotsLongPoll(BaseLongPoll): 133 | """Implements https://vk.com/dev/bots_longpoll""" 134 | def __init__(self, session_or_api, group_id, wait=25, version=1, timeout=None): 135 | super().__init__(session_or_api, None, wait, version, timeout) 136 | self.group_id = group_id 137 | 138 | async def _get_long_poll_server(self, need_pts=False): 139 | response = await self.api('groups.getLongPollServer', group_id=self.group_id) 140 | self.pts = response.get('pts') 141 | self.ts = response['ts'] 142 | self.key = response['key'] 143 | self.base_url = '{}'.format(response['server']) # Method already returning url with https:// 144 | -------------------------------------------------------------------------------- /aiovk/mixins.py: -------------------------------------------------------------------------------- 1 | from .drivers import BaseDriver 2 | from .shaping import TaskQueue, wait_free_slot 3 | 4 | 5 | class LimitRateDriverMixin(BaseDriver): 6 | def __init__(self, *args, requests_per_period=3, period=1, **kwargs): 7 | super().__init__(*args, **kwargs) 8 | self._queue = TaskQueue(requests_per_period, period) 9 | 10 | @wait_free_slot 11 | async def post_json(self, *args, **kwargs): 12 | return await super().post_json(*args, **kwargs) 13 | 14 | @wait_free_slot 15 | async def get_bin(self, *args, **kwargs): 16 | return await super().get_bin(*args, **kwargs) 17 | 18 | @wait_free_slot 19 | async def get_text(self, *args, **kwargs): 20 | return await super().get_text(*args, **kwargs) 21 | 22 | @wait_free_slot 23 | async def post_text(self, *args, **kwargs): 24 | return await super().post_text(*args, **kwargs) 25 | 26 | async def close(self): 27 | await super().close() 28 | self._queue.cancel() 29 | 30 | 31 | class SimpleImplicitSessionMixin: 32 | """ 33 | Simple implementation of processing captcha and 2factor authorization 34 | """ 35 | 36 | async def enter_captcha(self, url, sid): 37 | bytes = await self.driver.get_bin(url, {}) 38 | with open('captcha.jpg', 'wb') as f: 39 | f.write(bytes) 40 | return input("Enter captcha: ") 41 | 42 | async def enter_confirmation_сode(self): 43 | return input('Enter confirmation сode: ') 44 | -------------------------------------------------------------------------------- /aiovk/parser.py: -------------------------------------------------------------------------------- 1 | import re 2 | import html.parser 3 | import urllib.parse 4 | 5 | 6 | class AuthPageParser(html.parser.HTMLParser): 7 | def __init__(self): 8 | super().__init__() 9 | self.inputs = [] 10 | self.url = '' 11 | self.message = '' 12 | self.recording = 0 13 | self.captcha_url = '' 14 | 15 | def handle_starttag(self, tag, attrs): 16 | if tag == 'input': 17 | attrs = dict(attrs) 18 | if attrs['type'] != 'submit': 19 | self.inputs.append((attrs['name'], attrs.get('value', ''))) 20 | elif tag == 'form': 21 | for name, value in attrs: 22 | if name == 'action': 23 | self.url = value 24 | elif tag == 'img': 25 | attrs = dict(attrs) 26 | if attrs.get('class', '') == 'captcha_img': 27 | self.captcha_url = attrs['src'] 28 | elif tag == 'div': 29 | attrs = dict(attrs) 30 | if attrs.get('class', '') == 'service_msg service_msg_warning': 31 | self.recording = 1 32 | 33 | def handle_endtag(self, tag): 34 | if tag == 'div': 35 | self.recording = 0 36 | 37 | def handle_data(self, data): 38 | if self.recording: 39 | self.message = data 40 | 41 | 42 | class TwoFactorCodePageParser(html.parser.HTMLParser): 43 | def __init__(self): 44 | super().__init__() 45 | self.inputs = [] 46 | self.url = '' 47 | self.message = '' 48 | self.recording = 0 49 | 50 | def handle_starttag(self, tag, attrs): 51 | if tag == 'input': 52 | attrs = dict(attrs) 53 | if attrs['type'] != 'submit': 54 | self.inputs.append((attrs['name'], attrs.get('value', ''))) 55 | elif tag == 'form': 56 | for name, value in attrs: 57 | if name == 'action': 58 | self.url = urllib.parse.urljoin('https://m.vk.com/', value) 59 | elif tag == 'div': 60 | attrs = dict(attrs) 61 | if attrs.get('class', '') == 'service_msg service_msg_warning': 62 | self.recording = 1 63 | 64 | def handle_endtag(self, tag): 65 | if tag == 'div': 66 | self.recording = 0 67 | 68 | def handle_data(self, data): 69 | if self.recording: 70 | self.message += data 71 | 72 | 73 | class AccessPageParser(html.parser.HTMLParser): 74 | def __init__(self): 75 | super().__init__() 76 | self.inputs = [] 77 | self.url = '' 78 | 79 | def handle_starttag(self, tag, attrs): 80 | if tag == 'input': 81 | attrs = dict(attrs) 82 | if attrs['type'] != 'submit': 83 | self.inputs.append((attrs['name'], attrs.get('value', ''))) 84 | elif tag == 'form': 85 | for name, value in attrs: 86 | if name == 'action': 87 | self.url = value 88 | 89 | 90 | class AuthRedirectPageParser(html.parser.HTMLParser): 91 | def __init__(self): 92 | super().__init__() 93 | self.location = '' 94 | 95 | def handle_starttag(self, tag, attrs): 96 | if tag == 'meta': 97 | attrs = dict(attrs) 98 | if attrs.get('http-equiv') == 'refresh': 99 | content = attrs['content'] 100 | self.location = re.findall(r'URL=(.*)$', content)[0] 101 | -------------------------------------------------------------------------------- /aiovk/pools.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import json 3 | from collections import defaultdict 4 | from dataclasses import dataclass 5 | from typing import List, Dict, Optional 6 | 7 | from . import TokenSession, API 8 | from .exceptions import VkAuthError 9 | 10 | 11 | class AsyncResult: 12 | def __init__(self): 13 | self._result = None 14 | self.ready = False 15 | self.error = None 16 | 17 | @property 18 | def result(self): 19 | return self._result 20 | 21 | @result.setter 22 | def result(self, val): 23 | self._result = val 24 | self.ready = True 25 | 26 | @property 27 | def ok(self): 28 | return self.ready and not self.error 29 | 30 | 31 | @dataclass 32 | class VkCall: 33 | method: str 34 | method_args: dict 35 | result: AsyncResult 36 | 37 | def get_execute_representation(self) -> str: 38 | return f"API.{self.method}({json.dumps(self.method_args, ensure_ascii=False)})" 39 | 40 | 41 | class AsyncVkExecuteRequestPool: 42 | """ 43 | Allows concatenation of api calls using one token into groups and execute each group of hits in 44 | one request using `execute` method 45 | """ 46 | 47 | def __init__(self, call_number_per_request=25, token_session_class=TokenSession): 48 | self.token_session_class = token_session_class 49 | self.call_number_per_request = call_number_per_request 50 | self.pool: Dict[str, List[VkCall]] = defaultdict(list) 51 | self.sessions = [] 52 | 53 | async def __aenter__(self): 54 | return self 55 | 56 | async def __aexit__(self, *args, **kwargs): 57 | await self.execute() 58 | 59 | async def execute(self): 60 | try: 61 | await self._execute() 62 | await asyncio.gather(*[session.close() for session in self.sessions]) 63 | finally: 64 | self.pool.clear() 65 | self.sessions.clear() 66 | 67 | async def _execute(self): 68 | """ 69 | Groups hits and executes them using the execute method, after execution the pool is cleared 70 | """ 71 | executed_pools = [] 72 | for token, calls in self.pool.items(): 73 | session = self.token_session_class(token) 74 | self.sessions.append(session) 75 | api = API(session) 76 | 77 | for methods_pool in chunks(calls, self.call_number_per_request): 78 | executed_pools.append(VkExecuteMethodsPool(methods_pool).execute(api)) 79 | await asyncio.gather(*executed_pools) 80 | 81 | def add_call(self, method, token, method_args=None) -> AsyncResult: 82 | """ 83 | Adds an any api method call to the execute pool 84 | 85 | :param method: api vk method name 86 | :param token: session token 87 | :param method_args: params 88 | :return: object that will contain the result after the pool is closed 89 | """ 90 | if method_args is None: 91 | method_args = {} 92 | 93 | result = None 94 | # searching already added calls with equal token, method and values 95 | for call in self.pool[token]: 96 | if call.method == method and call.method_args == method_args: 97 | result = call.result 98 | break 99 | if result: 100 | return result 101 | result = AsyncResult() 102 | self.pool[token].append(VkCall(method=method, method_args=method_args, result=result)) 103 | return result 104 | 105 | 106 | class VkExecuteMethodsPool: 107 | def __init__(self, pool: Optional[VkCall] = None): 108 | if not pool: 109 | pool = [] 110 | self.pool: List[VkCall] = pool 111 | 112 | async def execute(self, api: API): 113 | """ 114 | Executes calls to the pool using the execute method and stores the results for each call 115 | 116 | :param api: API object to make the request 117 | """ 118 | methods = [call.get_execute_representation() for call in self.pool] 119 | code = f"return [{','.join(methods)}];" 120 | try: 121 | response = await api.execute(code=code, raw_response=True) 122 | except VkAuthError as e: 123 | for call in self.pool: 124 | call.result.error = { 125 | 'method': call.method, 126 | 'error_code': 5, 127 | 'error_msg': e.description 128 | } 129 | return 130 | errors = response.pop('execute_errors', [])[::-1] 131 | response = response['response'] 132 | 133 | for call, result in zip(self.pool, response): 134 | if result is False: 135 | call.result.error = errors.pop() 136 | else: 137 | call.result.result = result 138 | 139 | 140 | def chunks(lst, n): 141 | """Yield successive n-sized chunks from lst.""" 142 | for i in range(0, len(lst), n): 143 | yield lst[i: i + n] 144 | -------------------------------------------------------------------------------- /aiovk/sessions.py: -------------------------------------------------------------------------------- 1 | import json 2 | from abc import ABC, abstractmethod 3 | from typing import Tuple 4 | from urllib.parse import parse_qsl 5 | 6 | import aiohttp.hdrs 7 | 8 | from .drivers import HttpDriver 9 | from .exceptions import AUTHORIZATION_FAILED, CAPTCHA_IS_NEEDED, VkAPIError, VkAuthError, VkCaptchaNeeded, \ 10 | VkTwoFactorCodeNeeded 11 | from .parser import AccessPageParser, AuthPageParser, TwoFactorCodePageParser, AuthRedirectPageParser 12 | 13 | 14 | class BaseSession(ABC): 15 | """Interface for all types of sessions""" 16 | 17 | @abstractmethod 18 | async def __aenter__(self): 19 | """Make avaliable usage of "async with" context manager""" 20 | 21 | async def __aexit__(self, exc_type, exc_val, exc_tb): 22 | """Closes session after usage of context manager with Session""" 23 | await self.close() 24 | 25 | async def close(self) -> None: 26 | """Perform the actions associated with the completion of the current session""" 27 | 28 | @abstractmethod 29 | async def send_api_request(self, method_name: str, params: dict = None, timeout: int = None, 30 | raw_response: bool = False) -> dict: 31 | """Method that use API instance for sending request to vk server 32 | 33 | :param method_name: any value from the left column of the methods table from `https://vk.com/dev/methods` 34 | :param params: dict of params that available for current method. 35 | For example see `Parameters` block from: `https://vk.com/dev/account.getInfo` 36 | :param timeout: timeout for response from the server 37 | :param raw_response: return full response 38 | :return: dict that contain data from `Result` block. Example see here: `https://vk.com/dev/account.getInfo` 39 | """ 40 | 41 | 42 | class TokenSession(BaseSession): 43 | """Implements simple session that uses existed token for work""" 44 | 45 | API_VERSION = '5.81' 46 | REQUEST_URL = 'https://api.vk.com/method/' 47 | 48 | def __init__(self, access_token: str = None, timeout: int = 10, driver=None): 49 | """ 50 | :param access_token: see `User Token` block from `https://vk.com/dev/access_token` 51 | :param timeout: default time out for any request in current session 52 | :param driver: TODO add description 53 | """ 54 | self.timeout = timeout 55 | self.access_token = access_token 56 | self.driver = HttpDriver(timeout) if driver is None else driver 57 | 58 | async def __aenter__(self) -> BaseSession: 59 | """Make available usage of `async with` context manager""" 60 | return self 61 | 62 | async def __aexit__(self, exc_type, exc_val, exc_tb): 63 | return await self.close() 64 | 65 | async def send_api_request(self, method_name: str, params: dict = None, timeout: int = None, 66 | raw_response: bool = False) -> dict: 67 | # Prepare request 68 | if not timeout: 69 | timeout = self.timeout 70 | if not params: 71 | params = {} 72 | if self.access_token: 73 | params['access_token'] = self.access_token 74 | if 'v' not in params: 75 | params['v'] = self.API_VERSION 76 | 77 | # Send request 78 | _, response = await self.driver.post_json(self.REQUEST_URL + method_name, params, timeout=timeout) 79 | 80 | # Process response 81 | # Checking the section with errors 82 | error = response.get('error') 83 | if error: 84 | err_code = error.get('error_code') 85 | if err_code == CAPTCHA_IS_NEEDED: 86 | # Collect information about Captcha 87 | captcha_sid = error.get('captcha_sid') 88 | captcha_url = error.get('captcha_img') 89 | params['captcha_key'] = await self.enter_captcha(captcha_url, captcha_sid) 90 | params['captcha_sid'] = captcha_sid 91 | # Send request again 92 | # Provide one attempt to repeat the request 93 | return await self.send_api_request(method_name, params, timeout, raw_response) 94 | elif err_code == AUTHORIZATION_FAILED: 95 | await self.authorize() 96 | # Send request again 97 | # Provide one attempt to repeat the request 98 | return await self.send_api_request(method_name, params, timeout, raw_response) 99 | else: 100 | # Other errors is not related with security 101 | raise VkAPIError(error, self.REQUEST_URL + method_name) 102 | if raw_response: 103 | return response 104 | # Return only useful data 105 | return response['response'] 106 | 107 | async def authorize(self) -> None: 108 | """Getting a new token from server""" 109 | # For `TokenSession` we have not credentials for getting new token 110 | raise VkAuthError('invalid_token', 'User authorization failed') 111 | 112 | async def enter_captcha(self, url: str, sid: str) -> str: 113 | """ 114 | Override this method for processing captcha. 115 | 116 | :param url: link to captcha image 117 | :param sid: captcha id. I do not know why pass here but may be useful 118 | :return captcha value 119 | """ 120 | raise VkCaptchaNeeded(url, sid) 121 | 122 | async def close(self): 123 | return await self.driver.close() 124 | 125 | 126 | class ImplicitSession(TokenSession): 127 | """ 128 | For client authorization in js apps and standalone (desktop and mobile) apps 129 | See more in https://new.vk.com/dev/implicit_flow_user 130 | """ 131 | AUTH_URL = 'https://oauth.vk.com/authorize' 132 | 133 | def __init__(self, login: str, password: str, app_id: int, scope: str or int or list = None, 134 | timeout: int = 10, num_of_attempts: int = 5, driver=None): 135 | """ 136 | :param login: user login 137 | :param password: user password 138 | :param app_id: application id. More details in `Application registration` block in `https://vk.com/dev/first_guide` 139 | :param scope: access rights. See `Access rights` block in `https://vk.com/dev/first_guide` 140 | :param timeout: default time out for any request in current session 141 | :param num_of_attempts: number of authorization attempts 142 | :param driver: TODO add description 143 | """ 144 | super().__init__(access_token=None, timeout=timeout, driver=driver) 145 | self.login = login 146 | self.password = password 147 | self.app_id = app_id 148 | self.num_of_attempts = num_of_attempts 149 | if isinstance(scope, (str, int, type(None))): 150 | self.scope = scope 151 | elif isinstance(scope, list): 152 | self.scope = ",".join(scope) 153 | 154 | async def authorize(self) -> None: 155 | """Getting a new token from server""" 156 | url, html = await self._get_auth_page() 157 | for step in range(self.num_of_attempts): 158 | if url.path == '/authorize': 159 | if '__q_hash' in url.query: 160 | # Give rights for app 161 | url, html = await self._process_access_form(html) 162 | else: 163 | # Invalid login or password and 'email' in q.query 164 | url, html = await self._process_auth_form(html) 165 | if url.path == '/login' and url.query.get('act', '') == 'authcheck': 166 | # Entering 2auth code 167 | url, html = await self._process_2auth_form(html) 168 | if url.path == '/login' and url.query.get('act', '') == 'authcheck_code': 169 | # Need captcha 170 | url, html = await self._process_auth_form(html) 171 | if url.path == '/authorize' and '__q_hash' in url.query: 172 | # Give rights for app 173 | url, html = await self._process_access_form(html) 174 | if url.path == '/auth_redirect': 175 | # Process client-side auth redirect 176 | url, html = await self._process_auth_redirect_page(html) 177 | if url.path == '/blank.html': 178 | # Success 179 | parsed_fragments = dict(parse_qsl(url.fragment)) 180 | self.access_token = parsed_fragments['access_token'] 181 | return 182 | raise VkAuthError('Something went wrong', 'Exceeded the number of attempts to log in') 183 | 184 | async def _get_auth_page(self) -> Tuple[str, str]: 185 | """ 186 | Get authorization mobile page without js 187 | :return: redirect_url, html page 188 | """ 189 | # Prepare request 190 | params = { 191 | 'client_id': self.app_id, 192 | 'redirect_uri': 'https://oauth.vk.com/blank.html', 193 | 'display': 'mobile', 194 | 'response_type': 'token', 195 | 'v': self.API_VERSION 196 | } 197 | if self.scope: 198 | params['scope'] = self.scope 199 | 200 | # Send request 201 | status, response, redirect_url = await self.driver.get_text(self.AUTH_URL, params) 202 | 203 | # Process response 204 | if status != 200: 205 | error_dict = json.loads(response) 206 | raise VkAuthError(error_dict['error'], error_dict['error_description'], self.AUTH_URL, params) 207 | return redirect_url, response 208 | 209 | async def _process_auth_form(self, html: str) -> (str, str): 210 | """ 211 | Parsing data from authorization page and filling the form and submitting the form 212 | 213 | :param html: html page 214 | :return: url and html from redirected page 215 | """ 216 | # Parse page 217 | p = AuthPageParser() 218 | p.feed(html) 219 | p.close() 220 | 221 | # Get data from hidden inputs 222 | form_data = dict(p.inputs) 223 | form_url = p.url 224 | form_data['email'] = self.login 225 | form_data['pass'] = self.password 226 | if p.message: 227 | # Show form errors 228 | raise VkAuthError('invalid_data', p.message, form_url, form_data) 229 | elif p.captcha_url: 230 | form_data['captcha_key'] = await self.enter_captcha( 231 | "https://m.vk.com{}".format(p.captcha_url), 232 | form_data['captcha_sid'] 233 | ) 234 | form_url = "https://m.vk.com{}".format(form_url) 235 | 236 | # Send request 237 | _, html, redirect_url = await self.driver.post_text( 238 | form_url, form_data, headers={aiohttp.hdrs.REFERER: 'https://oauth.vk.com/'}) 239 | return redirect_url, html 240 | 241 | async def _process_2auth_form(self, html: str) -> (str, str): 242 | """ 243 | Parsing two-factor authorization page and filling the code 244 | 245 | :param html: html page 246 | :return: url and html from redirected page 247 | """ 248 | # Parse page 249 | p = TwoFactorCodePageParser() 250 | p.feed(html) 251 | p.close() 252 | 253 | # Prepare request data 254 | form_url = p.url 255 | form_data = dict(p.inputs) 256 | form_data['remember'] = 0 257 | if p.message: 258 | raise VkAuthError('invalid_data', p.message, form_url, form_data) 259 | form_data['code'] = await self.enter_confirmation_code() 260 | 261 | # Send request 262 | _, html, redirect_url = await self.driver.post_text(form_url, form_data) 263 | return redirect_url, html 264 | 265 | async def _process_access_form(self, html: str) -> (str, str): 266 | """ 267 | Parsing page with access rights 268 | 269 | :param html: html page 270 | :return: url and html from redirected page 271 | """ 272 | # Parse page 273 | p = AccessPageParser() 274 | p.feed(html) 275 | p.close() 276 | 277 | form_url = p.url 278 | form_data = dict(p.inputs) 279 | 280 | # Send request 281 | _, html, redirect_url = await self.driver.post_text(form_url, form_data) 282 | return redirect_url, html 283 | 284 | async def enter_confirmation_code(self) -> str: 285 | """ 286 | Override this method for processing confirmation 2uth code. 287 | :return confirmation code 288 | """ 289 | raise VkTwoFactorCodeNeeded() 290 | 291 | async def _process_auth_redirect_page(self, html: str) -> (str, str): 292 | """ 293 | Parse client auth redirect page 294 | 295 | :param html: html page 296 | :return: url and html from redirected page 297 | """ 298 | # Parse page 299 | p = AuthRedirectPageParser() 300 | p.feed(html) 301 | p.close() 302 | 303 | # Send request 304 | _, html, redirect_url = await self.driver.get_text(p.location, {}) 305 | return redirect_url, html 306 | 307 | 308 | class AuthorizationCodeSession(TokenSession): 309 | """ 310 | For client authorization in js apps and standalone (desktop and mobile) apps 311 | See more in https://new.vk.com/dev/implicit_flow_user 312 | """ 313 | CODE_URL = 'https://oauth.vk.com/access_token' 314 | 315 | def __init__(self, app_id: int, app_secret: str, redirect_uri: str, code: str, timeout: int = 10, driver=None): 316 | """ 317 | :param app_id: application id. More details in `Application registration` block in `https://vk.com/dev/first_guide` 318 | :param app_secret: application secure key. See https://vk.com/editapp?id={app_id}§ion=options 319 | :param redirect_uri: Authorized redirect URI. 320 | :param code: See `https://vk.com/dev/authcode_flow_user` 321 | :param timeout:default time out for any request in current session 322 | :param driver: TODO add description 323 | """ 324 | super().__init__(access_token=None, timeout=timeout, driver=driver) 325 | self.code = code 326 | self.app_id = app_id 327 | self.app_secret = app_secret 328 | self.redirect_uri = redirect_uri 329 | 330 | async def authorize(self, code: str = None) -> None: 331 | """Getting a new token from server""" 332 | code = await self.get_code(code) 333 | params = { 334 | 'client_id': self.app_id, 335 | 'client_secret': self.app_secret, 336 | 'redirect_uri': self.redirect_uri, 337 | 'code': code 338 | } 339 | _, response = await self.driver.post_json(self.CODE_URL, params, timeout=self.timeout) 340 | if 'error' in response: 341 | raise VkAuthError(response['error'], response['error_description'], self.CODE_URL, params) 342 | self.access_token = response['access_token'] 343 | 344 | async def get_code(self, code: str = None) -> str: 345 | """Get temporary code from external sources""" 346 | return code or self.code 347 | -------------------------------------------------------------------------------- /aiovk/shaping.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | 4 | class TaskQueue(asyncio.Queue): 5 | def __init__(self, max_size, period, *args, **kwargs): 6 | super().__init__(max_size, *args, **kwargs) 7 | self.period = period 8 | 9 | def _init(self, maxsize): 10 | super()._init(maxsize) 11 | for i in range(maxsize): 12 | self._queue.append(1) 13 | self.task = asyncio.ensure_future(self.dispatcher(maxsize)) 14 | 15 | async def dispatcher(self, maxsize): 16 | while True: 17 | await asyncio.sleep(self.period) 18 | for i in range(maxsize - self.qsize()): 19 | self.put_nowait(1) 20 | 21 | def cancel(self): 22 | self.task.cancel() 23 | 24 | 25 | def wait_free_slot(func): 26 | async def wrapper(self, *args, **kwargs): 27 | await self._queue.get() 28 | return await func(self, *args, **kwargs) 29 | return wrapper 30 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | -r requirements.txt 2 | aiohttp-jinja2==1.4.2 3 | jinja2<3.1 4 | pyotp==2.4.1 5 | proxy.py==2.2.0 6 | pytest==6.2.1 7 | pytest-asyncio==0.14.0 8 | pytest-aiohttp==0.3.0 9 | python-dotenv -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiohttp>=3.7.3 2 | aiohttp-socks>=0.5.5 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import codecs 2 | import os 3 | import re 4 | 5 | from setuptools import setup, find_packages 6 | 7 | with open('README.rst', 'r', encoding='utf8') as f: 8 | readme = f.read() 9 | 10 | with open('requirements.txt') as f: 11 | requirements = list(map(lambda x: x.strip(), f.readlines())) 12 | 13 | with codecs.open(os.path.join(os.path.abspath(os.path.dirname( 14 | __file__)), 'aiovk', '__init__.py'), 'r', 'latin1') as fp: 15 | try: 16 | version = re.findall(r"^__version__ = '([^']+)'\r?$", 17 | fp.read(), re.M)[0] 18 | except IndexError: 19 | raise RuntimeError('Unable to determine version.') 20 | 21 | setup( 22 | name='aiovk', 23 | version=version, 24 | 25 | author='Alexander Larin', 26 | author_email='ekzebox@gmail.com', 27 | 28 | url='https://github.com/alexanderlarin/aiovk', 29 | description='vk.com API python wrapper for asyncio', 30 | long_description=readme, 31 | 32 | packages=find_packages(), 33 | install_requires=requirements, 34 | 35 | license='MIT License', 36 | classifiers=[ 37 | 'Environment :: Web Environment', 38 | 'Intended Audience :: Developers', 39 | 'License :: OSI Approved :: MIT License', 40 | 'Programming Language :: Python :: 3.6', 41 | 'Programming Language :: Python :: 3.7', 42 | 'Programming Language :: Python :: 3.8', 43 | 'Programming Language :: Python :: 3.9', 44 | 'Topic :: Software Development :: Libraries :: Python Modules', 45 | ], 46 | keywords='vk.com api vk wrappper asyncio', 47 | test_suite="tests", 48 | python_requires='>=3.6' 49 | ) 50 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexanderlarin/aiovk/9d263c82ec59dc4f5762f59313ca8871a11033ec/tests/__init__.py -------------------------------------------------------------------------------- /tests/functional/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexanderlarin/aiovk/9d263c82ec59dc4f5762f59313ca8871a11033ec/tests/functional/__init__.py -------------------------------------------------------------------------------- /tests/functional/conftest.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | import aiohttp_jinja2 4 | import jinja2 5 | import pytest 6 | from aiohttp import web 7 | 8 | 9 | @pytest.fixture() 10 | def user_1_data(): 11 | return [{'id': 1, 'first_name': 'Павел', 'last_name': 'Дуров'}] 12 | 13 | 14 | @pytest.fixture() 15 | def valid_token(): 16 | return 'token' 17 | 18 | 19 | @pytest.fixture() 20 | async def vk_server(aiohttp_server, user_1_data, valid_token): 21 | @aiohttp_jinja2.template('authorize_page.jinja2') 22 | async def authorize(request): 23 | if 'client_id' in request.query: 24 | return {'base_url': request.host} 25 | else: 26 | raise web.HTTPNotImplemented 27 | 28 | @aiohttp_jinja2.template('auth_redirect.jinja2') 29 | async def auth_redirect(request): 30 | return { 31 | 'redirect_url': f'http://{request.host}{request.app.router["blank"].url_for()!s}', 32 | 'access_token': valid_token, 33 | } 34 | 35 | @aiohttp_jinja2.template('blank.jinja2') 36 | async def blank(request): 37 | return 38 | 39 | async def longpoolwait(request): 40 | return web.json_response({'ts': 1857669127, 'updates': []}) 41 | 42 | async def user_get_error(request): 43 | return web.json_response({'error': {'error_code': 5, 44 | 'error_msg': 'User authorization failed: invalid access_token (4).', 45 | 'request_params': [{'key': 'oauth', 'value': '1'}, 46 | {'key': 'method', 'value': 'users.get'}, 47 | {'key': 'user_ids', 'value': '1'}, 48 | {'key': 'v', 'value': '5.74'}]}}) 49 | 50 | async def user_get(request): 51 | return web.json_response({'response': user_1_data}) 52 | 53 | async def get_long_poll_server(request): 54 | data = await request.post() 55 | if data.get('access_token') == valid_token: 56 | return web.json_response( 57 | {'response': {'key': 'long_pool_key', 'server': f'{request.host}/im1774', 'ts': 1857669095}}) 58 | else: 59 | return web.json_response({'error': {'error_code': 5, 60 | 'error_msg': 'User authorization failed: no access_token passed.', 61 | 'request_params': [{'key': 'oauth', 'value': '1'}, {'key': 'method', 62 | 'value': 'messages.getLongPollServer'}, 63 | {'key': 'need_pts', 'value': '0'}, 64 | {'key': 'v', 'value': '5.74'}]}}) 65 | 66 | async def root(request): 67 | data = await request.post() 68 | if data.get('email') == 'login': 69 | location = request.app.router['auth_redirect'].url_for() 70 | raise web.HTTPFound(location=f'{location}') 71 | else: 72 | response = aiohttp_jinja2.render_template('auth_redirect', request, None) 73 | return response 74 | 75 | app = web.Application() 76 | template_dir = Path(__file__).parent.parent / 'responses' 77 | aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(template_dir)) 78 | app.add_routes([ 79 | web.get('/authorize', authorize), 80 | web.get('/auth_redirect', auth_redirect, name='auth_redirect'), 81 | web.get('/blank.html', blank, name='blank'), 82 | web.get('/im1774', longpoolwait), 83 | web.post('/method/users.get.error', user_get_error), 84 | web.post('/method/users.get', user_get), 85 | web.post('/method/messages.getLongPollServer', get_long_poll_server), 86 | web.post('/', root), 87 | ]) 88 | server = await aiohttp_server(app) 89 | yield server 90 | -------------------------------------------------------------------------------- /tests/functional/test_drivers.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import proxy 4 | import pytest 5 | from aiohttp import web 6 | from aiohttp.test_utils import unused_port 7 | from python_socks import ProxyType 8 | from yarl import URL 9 | 10 | from aiovk.drivers import HttpDriver, ProxyDriver 11 | 12 | pytestmark = pytest.mark.asyncio 13 | 14 | 15 | @pytest.fixture() 16 | def simple_response_data(): 17 | return {'a': 1} 18 | 19 | 20 | @pytest.fixture() 21 | async def vk_server(aiohttp_server, simple_response_data): 22 | async def simple_response_data_handler(request): 23 | return web.json_response(simple_response_data) 24 | 25 | app = web.Application() 26 | app.add_routes([web.route('*', r'/{name:.*}', simple_response_data_handler)]) 27 | server = await aiohttp_server(app) 28 | yield server 29 | 30 | 31 | @pytest.fixture(scope='module') 32 | def proxy_server(): 33 | with proxy.start(['--hostname', '127.0.0.1', '--num-workers', '1', '--port', str(unused_port())]) as p: 34 | yield p 35 | 36 | 37 | @pytest.mark.parametrize( 38 | 'driver_class, use_proxy', 39 | [ 40 | (HttpDriver, False), 41 | (ProxyDriver, True), 42 | ] 43 | ) 44 | async def test_post_json(simple_response_data, vk_server, proxy_server, driver_class, use_proxy): 45 | url = f'http://{vk_server.host}:{vk_server.port}/' 46 | params = {} 47 | if use_proxy: 48 | params['address'] = str(proxy_server.flags.hostname) 49 | params['port'] = proxy_server.flags.port 50 | params['proxy_type'] = ProxyType.HTTP 51 | driver = driver_class(**params) 52 | status, jsn = await driver.post_json(url, {}) 53 | await driver.close() 54 | 55 | assert status == 200 56 | assert jsn == simple_response_data 57 | 58 | 59 | @pytest.mark.parametrize( 60 | 'driver_class, use_proxy', 61 | [ 62 | (HttpDriver, False), 63 | (ProxyDriver, True), 64 | ] 65 | ) 66 | async def test_get_bin(simple_response_data, vk_server, proxy_server, driver_class, use_proxy): 67 | url = f'http://{vk_server.host}:{vk_server.port}/' 68 | params = {} 69 | if use_proxy: 70 | params['address'] = str(proxy_server.flags.hostname) 71 | params['port'] = proxy_server.flags.port 72 | params['proxy_type'] = ProxyType.HTTP 73 | driver = driver_class(**params) 74 | status, text = await driver.get_bin(url, {}) 75 | await driver.close() 76 | 77 | assert status == 200 78 | assert text == json.dumps(simple_response_data).encode() 79 | 80 | 81 | @pytest.mark.parametrize( 82 | 'driver_class, use_proxy', 83 | [ 84 | (HttpDriver, False), 85 | (ProxyDriver, True), 86 | ] 87 | ) 88 | async def test_get_text(simple_response_data, vk_server, proxy_server, driver_class, use_proxy): 89 | url = f'http://{vk_server.host}:{vk_server.port}/' 90 | params = {} 91 | if use_proxy: 92 | params['address'] = str(proxy_server.flags.hostname) 93 | params['port'] = proxy_server.flags.port 94 | params['proxy_type'] = ProxyType.HTTP 95 | driver = driver_class(**params) 96 | status, text, redirect_url = await driver.get_text(url, {}) 97 | await driver.close() 98 | 99 | assert status == 200 100 | assert text == json.dumps(simple_response_data) 101 | assert redirect_url == URL(url) 102 | 103 | 104 | @pytest.mark.parametrize( 105 | 'driver_class, use_proxy', 106 | [ 107 | (HttpDriver, False), 108 | (ProxyDriver, True), 109 | ] 110 | ) 111 | async def test_post_text(simple_response_data, vk_server, proxy_server, driver_class, use_proxy): 112 | data = { 113 | 'login': 'test', 114 | 'password': 'test' 115 | } 116 | url = f'http://{vk_server.host}:{vk_server.port}/' 117 | params = {} 118 | if use_proxy: 119 | params['address'] = str(proxy_server.flags.hostname) 120 | params['port'] = proxy_server.flags.port 121 | params['proxy_type'] = ProxyType.HTTP 122 | driver = driver_class(**params) 123 | status, text, redirect_url = await driver.post_text(url, data=data) 124 | await driver.close() 125 | 126 | assert status == 200 127 | assert text == json.dumps(simple_response_data) 128 | assert redirect_url == URL(url) 129 | -------------------------------------------------------------------------------- /tests/functional/test_longpool.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from aiovk import ImplicitSession, API, LongPoll, TokenSession 4 | 5 | pytestmark = pytest.mark.asyncio 6 | 7 | 8 | async def test_wait_valid_with_token_session(vk_server, valid_token): 9 | url = f'http://{vk_server.host}:{vk_server.port}' 10 | t = TokenSession(valid_token, timeout=1000) 11 | t.BASE_URL = url 12 | t.REQUEST_URL = f'{url}/method/' 13 | api = API(t) 14 | lp = LongPoll(api, mode=2, wait=2) 15 | lp.use_https = False 16 | 17 | response = await lp.wait() 18 | await t.close() 19 | assert 'ts' in response 20 | assert 'updates' in response 21 | 22 | 23 | async def test_wait_valid_with_session_authorised(vk_server): 24 | url = f'http://{vk_server.host}:{vk_server.port}' 25 | s = ImplicitSession(login='login', password='pass', app_id='123', scope='messages') 26 | s.REQUEST_URL = f'{url}/method/' 27 | s.AUTH_URL = f'{url}/authorize' 28 | await s.authorize() 29 | 30 | lp = LongPoll(s, mode=2, wait=2) 31 | lp.use_https = False 32 | 33 | response = await lp.wait() 34 | await s.close() 35 | assert 'ts' in response 36 | assert 'updates' in response 37 | 38 | 39 | async def test_wait_valid_with_session_auto_auth(vk_server): 40 | url = f'http://{vk_server.host}:{vk_server.port}' 41 | s = ImplicitSession(login='login', password='pass', app_id='123', scope='messages') 42 | s.REQUEST_URL = f'{url}/method/' 43 | s.AUTH_URL = f'{url}/authorize' 44 | 45 | api = API(s) 46 | lp = LongPoll(api, mode=2, wait=2) 47 | lp.use_https = False 48 | 49 | response = await lp.wait() 50 | await s.close() 51 | assert 'ts' in response 52 | assert 'updates' in response 53 | -------------------------------------------------------------------------------- /tests/functional/test_sessions.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from aiovk import ImplicitSession, TokenSession 4 | from aiovk.exceptions import VkAuthError 5 | 6 | pytestmark = pytest.mark.asyncio 7 | 8 | 9 | async def test_token_session_auth_with_empty_token(vk_server): 10 | s = TokenSession() 11 | with pytest.raises(VkAuthError): 12 | await s.authorize() 13 | await s.close() 14 | 15 | 16 | async def test_token_session_auth_with_token(vk_server): 17 | s = TokenSession('token') 18 | with pytest.raises(VkAuthError): 19 | await s.authorize() 20 | await s.close() 21 | 22 | 23 | async def test_token_session_auth_token_free_request_without_token(vk_server, user_1_data): 24 | url = f'http://{vk_server.host}:{vk_server.port}' 25 | s = TokenSession() 26 | s.REQUEST_URL = f'{url}/method/' 27 | result = await s.send_api_request('users.get', {'user_ids': 1}) 28 | await s.close() 29 | assert result == user_1_data 30 | 31 | 32 | async def test_token_session_auth_token_request_without_token(vk_server): 33 | url = f'http://{vk_server.host}:{vk_server.port}' 34 | s = TokenSession('token') 35 | s.REQUEST_URL = f'{url}/method/' 36 | with pytest.raises(VkAuthError): 37 | await s.send_api_request('users.get.error', {'user_ids': 1}) 38 | await s.close() 39 | 40 | 41 | async def test_implicit_session_auth_with_empty_data(vk_server): 42 | url = f'http://{vk_server.host}:{vk_server.port}' 43 | s = ImplicitSession(login='', password='', app_id='') 44 | s.REQUEST_URL = f'{url}/method/' 45 | s.AUTH_URL = f'{url}/authorize' 46 | 47 | with pytest.raises(VkAuthError): 48 | await s.authorize() 49 | await s.close() 50 | 51 | 52 | async def test_implicit_session_auth_with_2factor(vk_server): 53 | url = f'http://{vk_server.host}:{vk_server.port}' 54 | s = ImplicitSession(login='login', password='pass', app_id='123') 55 | s.REQUEST_URL = f'{url}/method/' 56 | s.AUTH_URL = f'{url}/authorize' 57 | 58 | await s.authorize() 59 | await s.close() 60 | 61 | 62 | @pytest.mark.skip('TODO add captcha test') 63 | async def test_implicit_session_auth_process_captcha_without(vk_server): 64 | url = f'http://{vk_server.host}:{vk_server.port}' 65 | s = ImplicitSession(login='login', password='pass', app_id='123') 66 | s.REQUEST_URL = f'{url}/method/' 67 | s.AUTH_URL = f'{url}/authorize' 68 | 69 | await s.authorize() 70 | await s.close() 71 | -------------------------------------------------------------------------------- /tests/responses/auth_redirect.jinja2: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |