aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-06-09 03:14:08 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-06-09 03:14:08 +0300
commitb4130ad6ee77fbfbf521dc5860de3f843451d118 (patch)
treeb4ecfae1c518b5403339bc52a3601659c12385bb
parentREADME update (diff)
downloadvk-scripts-b4130ad6ee77fbfbf521dc5860de3f843451d118.tar.gz
vk-scripts-b4130ad6ee77fbfbf521dc5860de3f843451d118.zip
refactoring
-rw-r--r--mutual_friends.py7
-rw-r--r--track_status.py48
2 files changed, 28 insertions, 27 deletions
diff --git a/mutual_friends.py b/mutual_friends.py
index 9de77cb..20bb32a 100644
--- a/mutual_friends.py
+++ b/mutual_friends.py
@@ -2,8 +2,6 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
-import sys
-
from api import *
def query_friends(api, user):
@@ -17,15 +15,16 @@ def format_user(user):
if __name__ == '__main__':
import argparse
+
parser = argparse.ArgumentParser(
description='Learn who your ex and her new boyfriend are both friends with.')
- parser.add_argument(metavar='UID', dest='user_ids', nargs='+',
+ parser.add_argument(metavar='UID', dest='uids', nargs='+',
help='user IDs or "screen names"')
args = parser.parse_args()
api = API(Language.EN)
- users = api.users_get(args.user_ids, fields=User.Field.SCREEN_NAME)
+ users = api.users_get(args.uids, fields=User.Field.SCREEN_NAME)
friend_lists = map(lambda user: frozenset(query_friends(api, user)), users)
mutual_friends = frozenset.intersection(*friend_lists)
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: