From 768fe6080c0ef35935dce0b271d84d442ce0e641 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 3 Mar 2018 15:10:20 +0300 Subject: switch to VK API version 5.73 --- bin/mutual_friends.py | 2 +- bin/track_status.py | 2 +- vk/api.py | 38 ++++++++++++++++++++++++-------------- vk/tracking/status_tracker.py | 2 +- vk/user.py | 2 +- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/bin/mutual_friends.py b/bin/mutual_friends.py index f67f987..c17c6b3 100644 --- a/bin/mutual_friends.py +++ b/bin/mutual_friends.py @@ -90,7 +90,7 @@ def _parse_args(args=None): return parser.parse_args(args) def write_mutual_friends(uids, out_path=None, out_fmt=OutputFormat.CSV): - api = API(Language.EN) + api = API() users = api.users_get(uids) friend_lists = (frozenset(_query_friend_list(api, user)) for user in users) diff --git a/bin/track_status.py b/bin/track_status.py index 4c679c5..c61f242 100644 --- a/bin/track_status.py +++ b/bin/track_status.py @@ -57,7 +57,7 @@ def track_status( log_path=None, db_path=None, db_fmt=DEFAULT_DB_FORMAT): - api = API(Language.EN, deactivated_users=False) + api = API() tracker = StatusTracker(api, timeout) if db_fmt is DatabaseFormat.LOG or db_path is None: diff --git a/vk/api.py b/vk/api.py index eb1914e..392d4b8 100644 --- a/vk/api.py +++ b/vk/api.py @@ -56,9 +56,16 @@ def _join_path(base, url): base += '/' return urllib.parse.urljoin(base, url) +class Version(Enum): + V5_73 = '5.73' + DEFAULT = V5_73 + + def __str__(self): + return self.value + class Language(Enum): - DEFAULT = None EN = 'en' + DEFAULT = EN def __str__(self): return self.value @@ -71,6 +78,7 @@ class Method(Enum): return self.value class CommonParameters(Enum): + VERSION = 'v' LANGUAGE = 'lang' def __str__(self): @@ -81,11 +89,11 @@ class API: _SCHEME, _HOST, _ROOT_PATH = _split_url(_ROOT_URL) - def __init__(self, lang=Language.DEFAULT, deactivated_users=True): + def __init__(self, version=Version.DEFAULT, lang=Language.DEFAULT): self._common_params = { + CommonParameters.VERSION: version, CommonParameters.LANGUAGE: lang, } - self._skip_deactivated_users = not deactivated_users def _build_method_url(self, method, **params): path = _join_path(self._ROOT_PATH, str(method)) @@ -98,28 +106,30 @@ class API: try: with urlopen(url) as response: response = json.loads(response.read().decode()) + #print(response) if 'response' not in response: raise vk.error.InvalidAPIResponseError(response) - #print(response) return response['response'] except (ConnectionError, URLError) as e: raise vk.error.APIConnectionError(str(e)) from e - def _should_skip_user(self, user): - return self._skip_deactivated_users and user.is_deactivated() - - def _filter_response_with_users(self, user_list): + def _filter_response_with_users(self, user_list, deactivated_users=True): user_list = map(User.from_api_response, user_list) - return [user for user in user_list if not self._should_skip_user(user)] + if deactivated_users: + return user_list + return [user for user in user_list if not user.is_deactivated()] - def users_get(self, user_ids, fields=()): + def users_get(self, user_ids, fields=(), deactivated_users=True): return self._filter_response_with_users(self._call_method( Method.USERS_GET, user_ids=_join_param_values(user_ids), - fields=_join_param_values(fields))) + fields=_join_param_values(fields)), deactivated_users) - def friends_get(self, user_id, fields=()): - return self._filter_response_with_users(self._call_method( + def friends_get(self, user_id, fields=(), deactivated_users=True): + response = self._call_method( Method.FRIENDS_GET, user_id=user_id, - fields=_join_param_values(fields))) + fields=_join_param_values(fields)) + if 'items' not in response: + raise vk.error.InvalidAPIResponseError(response) + return self._filter_response_with_users(response['items'], deactivated_users) diff --git a/vk/tracking/status_tracker.py b/vk/tracking/status_tracker.py index d47de39..0dba0f9 100644 --- a/vk/tracking/status_tracker.py +++ b/vk/tracking/status_tracker.py @@ -47,7 +47,7 @@ class StatusTracker: _USER_FIELDS = UserField.DOMAIN, UserField.ONLINE, UserField.LAST_SEEN, def _query_status(self, uids): - return {user.get_uid(): user for user in self._api.users_get(uids, self._USER_FIELDS)} + return {user.get_uid(): user for user in self._api.users_get(uids, self._USER_FIELDS, deactivated_users=False)} def _notify_status(self, user): for fn in self._on_initial_status: diff --git a/vk/user.py b/vk/user.py index f63f793..36d4dec 100644 --- a/vk/user.py +++ b/vk/user.py @@ -35,7 +35,7 @@ def _parse_online_flag(x): return _parse_bool(x) class UserField(Enum): - UID = 'uid' + UID = 'id' FIRST_NAME = 'first_name' LAST_NAME = 'last_name' DEACTIVATED = 'deactivated' -- cgit v1.2.3