diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-16 02:40:51 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-16 02:40:51 +0300 |
commit | 8bd8f1f2d09db33697fc61d5caaa3b37b856916d (patch) | |
tree | bdb11964cdaf5c985663638f9dbedd5167e3ee4b /vk/utils/tracking/status_tracker.py | |
parent | track_status.py: add a database of statuses (diff) | |
download | vk-scripts-8bd8f1f2d09db33697fc61d5caaa3b37b856916d.tar.gz vk-scripts-8bd8f1f2d09db33697fc61d5caaa3b37b856916d.zip |
track_status.py: move stuff to the package
Diffstat (limited to 'vk/utils/tracking/status_tracker.py')
-rw-r--r-- | vk/utils/tracking/status_tracker.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/vk/utils/tracking/status_tracker.py b/vk/utils/tracking/status_tracker.py new file mode 100644 index 0000000..dad14c0 --- /dev/null +++ b/vk/utils/tracking/status_tracker.py @@ -0,0 +1,94 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections import Callable +import time + +import vk.error +from vk.user import Field + +class StatusTracker: + DEFAULT_TIMEOUT = 5 + + def __init__(self, api, timeout=DEFAULT_TIMEOUT): + self._api = api + self._timeout = timeout + self._on_initial_status = [] + self._on_status_update = [] + self._on_connection_error = [] + + def _wait_after_connection_error(self): + time.sleep(self._timeout) + + def add_initial_status_handler(self, fn): + self._assert_is_callback(fn) + self._on_initial_status.append(fn) + + def add_status_update_handler(self, fn): + self._assert_is_callback(fn) + self._on_status_update.append(fn) + + def add_connection_error_handler(self, fn): + self._assert_is_callback(fn) + self._on_connection_error.append(fn) + + @staticmethod + def _assert_is_callback(fn): + if not isinstance(fn, Callable): + raise TypeError() + + _USER_FIELDS = Field.SCREEN_NAME, Field.ONLINE, Field.LAST_SEEN + + def _query_status(self, uids): + return {user.get_uid(): user for user in self._api.users_get(uids, self._USER_FIELDS)} + + def _notify_status(self, user): + for fn in self._on_initial_status: + fn(user) + + def _notify_status_update(self, user): + for fn in self._on_status_update: + fn(user) + + def _notify_connection_error(self, e): + for fn in self._on_connection_error: + fn(e) + + def _query_initial_status(self, uids): + while True: + try: + return self._query_status(uids) + except vk.error.ConnectionError as e: + self._notify_connection_error(e) + self._wait_after_connection_error() + + def _query_status_updates(self, uids): + while True: + self._wait_after_connection_error() + try: + return self._query_status(uids) + except vk.error.ConnectionError as e: + self._notify_connection_error(e) + + @staticmethod + def _filter_status_updates(old_users, new_users): + for uid, user in new_users.items(): + if old_users[uid].is_online() != user.is_online(): + old_users[uid] = user + yield user + + def _do_loop(self, uids): + users = self._query_initial_status(uids) + for user in users.values(): + self._notify_status(user) + while True: + updated_users = self._query_status_updates(uids) + for user in self._filter_status_updates(users, updated_users): + self._notify_status_update(user) + + def loop(self, uids): + try: + self._do_loop(uids) + except KeyboardInterrupt: + pass |