aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/track_status.py
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-02-12 08:16:26 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-02-12 08:16:26 +0300
commit6bba781c496ad692d1bce21c561abab7e971e061 (patch)
tree8b2498ada3907e548eb6b29e2c127407493b3367 /track_status.py
parentrefactoring (diff)
downloadvk-scripts-6bba781c496ad692d1bce21c561abab7e971e061.tar.gz
vk-scripts-6bba781c496ad692d1bce21c561abab7e971e061.zip
rename the scripts
Diffstat (limited to 'track_status.py')
-rw-r--r--track_status.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/track_status.py b/track_status.py
new file mode 100644
index 0000000..2c6776e
--- /dev/null
+++ b/track_status.py
@@ -0,0 +1,104 @@
+# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is licensed under the terms of the MIT License.
+# See LICENSE.txt for details.
+
+import argparse
+import logging
+import time
+import sys
+
+from api import *
+
+def format_user(user):
+ if user.has_last_name():
+ return '{} {}'.format(user.get_last_name(), user.get_first_name())
+ else:
+ return '{}'.format(user.get_first_name())
+
+def format_user_is_online(user):
+ return '{} is ONLINE'.format(format_user(user))
+
+def format_user_is_offline(user):
+ return '{} is OFFLINE'.format(format_user(user))
+
+def format_user_last_seen(user):
+ return '{} was last seen at {}'.format(format_user(user), user.get_last_seen())
+
+def format_user_went_online(user):
+ return '{} went ONLINE'.format(format_user(user))
+
+def format_user_went_offline(user):
+ return '{} went OFFLINE'.format(format_user(user))
+
+def user_is_offline(user):
+ logging.info(format_user_is_offline(user))
+ logging.info(format_user_last_seen(user))
+
+def user_went_online(user):
+ logging.info(format_user_went_online(user))
+
+def user_went_offline(user):
+ logging.info(format_usre_went_offline(user))
+
+def print_status(user):
+ if user.is_online():
+ user_is_online(user)
+ else:
+ user_is_offline(user)
+
+def print_status_update(user):
+ if user.is_online():
+ user_went_online(user)
+ else:
+ user_went_offline(user)
+
+def parse_timeout(source):
+ timeout = int(source)
+ if timeout < 1:
+ raise argparse.ArgumentTypeError(
+ 'please specify a positive number of seconds as refresh timeout')
+ return timeout
+
+DEFAULT_TIMEOUT=5
+
+def loop_update_status(api, user_ids, timeout=DEFAULT_TIMEOUT):
+ fields = User.Field.ONLINE, User.Field.LAST_SEEN
+ users = list(api.users_get(user_ids, fields))
+ for user in users:
+ print_status(user)
+ while True:
+ time.sleep(timeout)
+ updated_users = list(api.users_get(user_ids, fields))
+ for i in range(len(updated_users)):
+ if users[i].is_online() != updated_users[i].is_online():
+ users[i] = updated_users[i]
+ print_status_update(updated_users[i])
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(
+ description='Track when people go online/offline.')
+
+ parser.add_argument(metavar='UID', dest='user_ids', nargs='+',
+ help='user IDs or "screen names"')
+ parser.add_argument('-t', '--timeout', default=DEFAULT_TIMEOUT,
+ type=parse_timeout,
+ help='set refresh interval (seconds)')
+ parser.add_argument('-l', '--log', type=argparse.FileType('w'),
+ default=sys.stdout,
+ help='set log file path (stdout by default)')
+ args = parser.parse_args()
+
+ logging.basicConfig(format='[%(asctime)s] %(message)s',
+ stream=args.log,
+ level=logging.INFO,
+ datefmt='%Y-%m-%d %H:%M:%S')
+
+ api = API(Language.EN)
+
+ try:
+ loop_update_status(api, args.user_ids, timeout=args.timeout)
+ except KeyboardInterrupt:
+ pass
+ except Exception as e:
+ logging.exception(e)
+ sys.exit(1)