aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/track_status.py
diff options
context:
space:
mode:
Diffstat (limited to 'track_status.py')
-rw-r--r--track_status.py48
1 files changed, 25 insertions, 23 deletions
diff --git a/track_status.py b/track_status.py
index f930f4c..2761b2b 100644
--- a/track_status.py
+++ b/track_status.py
@@ -2,10 +2,7 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
-import argparse
-import logging
-import time
-import sys
+import logging, time
from api import *
@@ -55,36 +52,41 @@ def print_status_update(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
+USER_FIELDS = User.Field.ONLINE, User.Field.LAST_SEEN
+
+def update_status(api, uids):
+ return {user.get_uid(): user for user in api.users_get(uids, USER_FIELDS)}
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)
+def loop_update_status(api, uids, timeout=DEFAULT_TIMEOUT):
+ users = update_status(api, uids)
+ for uid in users:
+ print_status(users[uid])
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])
+ updated_users = update_status(api, uids)
+ for uid in updated_users:
+ if users[uid].is_online() != updated_users[uid].is_online():
+ users[uid] = updated_users[uid]
+ print_status_update(updated_users[uid])
if __name__ == '__main__':
+ import argparse, sys
+
+ def natural_number(s):
+ x = int(s)
+ if x < 1:
+ raise argparse.ArgumentTypeError()
+ return x
+
parser = argparse.ArgumentParser(
description='Track when people go online/offline.')
- parser.add_argument(metavar='UID', dest='user_ids', nargs='+',
+ parser.add_argument(metavar='UID', dest='uids', nargs='+',
help='user IDs or "screen names"')
parser.add_argument('-t', '--timeout', default=DEFAULT_TIMEOUT,
- type=parse_timeout,
+ type=natural_number,
help='set refresh interval (seconds)')
parser.add_argument('-l', '--log', type=argparse.FileType('w'),
default=sys.stdout,
@@ -99,7 +101,7 @@ if __name__ == '__main__':
api = API(Language.EN)
try:
- loop_update_status(api, args.user_ids, timeout=args.timeout)
+ loop_update_status(api, args.uids, timeout=args.timeout)
except KeyboardInterrupt:
pass
except Exception as e: