aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/track_online.py
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-02-12 08:07:19 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-02-12 08:07:19 +0300
commit21c3caaba44fc13221ba63600262e6eb15cbe824 (patch)
treebae3804171c3323476373e80561a7e8037e4f09d /track_online.py
parentuse the standard logging module & refactoring (diff)
downloadvk-scripts-21c3caaba44fc13221ba63600262e6eb15cbe824.tar.gz
vk-scripts-21c3caaba44fc13221ba63600262e6eb15cbe824.zip
refactoring
Diffstat (limited to '')
-rw-r--r--track_online.py77
1 files changed, 41 insertions, 36 deletions
diff --git a/track_online.py b/track_online.py
index ed43f11..2c6776e 100644
--- a/track_online.py
+++ b/track_online.py
@@ -3,49 +3,54 @@
# See LICENSE.txt for details.
import argparse
-from datetime import datetime
import logging
import time
import sys
-import api
+from api import *
-def users_get(user_ids):
- response = api.users_get(user_ids=','.join(user_ids),
- fields='online,last_seen')
- if len(response) < len(user_ids):
- raise RuntimeError('Couldn\'t update status of at least one of the users!')
- return response
+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_name(user):
- return '{} {}'.format(user['last_name'], user['first_name'])
+def format_user_went_online(user):
+ return '{} went ONLINE'.format(format_user(user))
-def user_is_online(user):
- logging.info('{} is ONLINE'.format(format_user_name(user)))
+def format_user_went_offline(user):
+ return '{} went OFFLINE'.format(format_user(user))
def user_is_offline(user):
- user_name = format_user_name(user)
- logging.info('{} is OFFLINE'.format(user_name))
- last_seen = datetime.fromtimestamp(user['last_seen']['time'])
- logging.info('{} was last seen at {}'.format(user_name, last_seen))
+ logging.info(format_user_is_offline(user))
+ logging.info(format_user_last_seen(user))
def user_went_online(user):
- logging.info('{} went ONLINE'.format(format_user_name(user)))
+ logging.info(format_user_went_online(user))
def user_went_offline(user):
- logging.info('{} went OFFLINE'.format(format_user_name(user)))
+ logging.info(format_usre_went_offline(user))
-def print_user(status):
- if status['online']:
- user_is_online(status)
+def print_status(user):
+ if user.is_online():
+ user_is_online(user)
else:
- user_is_offline(status)
+ user_is_offline(user)
-def print_status_update(status):
- if status['online']:
- user_went_online(status)
+def print_status_update(user):
+ if user.is_online():
+ user_went_online(user)
else:
- user_went_offline(status)
+ user_went_offline(user)
def parse_timeout(source):
timeout = int(source)
@@ -56,24 +61,23 @@ def parse_timeout(source):
DEFAULT_TIMEOUT=5
-def print_initial_status(user_ids):
- users = users_get(user_ids)
+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_user(user)
- return users
-
-def loop_update_status(users, user_ids, timeout=DEFAULT_TIMEOUT):
+ print_status(user)
while True:
time.sleep(timeout)
- updated_users = users_get(user_ids)
+ updated_users = list(api.users_get(user_ids, fields))
for i in range(len(updated_users)):
- if users[i]['online'] != updated_users[i]['online']:
+ 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,
@@ -89,9 +93,10 @@ if __name__ == '__main__':
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
+ api = API(Language.EN)
+
try:
- users = print_initial_status(args.user_ids)
- loop_update_status(users, args.user_ids, timeout=args.timeout)
+ loop_update_status(api, args.user_ids, timeout=args.timeout)
except KeyboardInterrupt:
pass
except Exception as e: