From a320153d3ee820434fc390029328b853e3434fc6 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 17 Jul 2015 20:20:10 +0300 Subject: bugfix --- api.py | 2 +- print_mutual_friends.py | 38 +++++++++++++++++--------------------- track_online.py | 10 ++++++++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/api.py b/api.py index d584c2b..5d11e4f 100644 --- a/api.py +++ b/api.py @@ -10,7 +10,7 @@ def call_method(method_name, **kwargs): response = json.loads(urllib.request.urlopen(url).read().decode()) if 'response' not in response: print(response, file=sys.stderr) - sys.exit(-1) + sys.exit(1) return response['response'] def users_get(**kwargs): diff --git a/print_mutual_friends.py b/print_mutual_friends.py index fdc195a..6895a3c 100644 --- a/print_mutual_friends.py +++ b/print_mutual_friends.py @@ -2,29 +2,25 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. -import api +import api, sys def users_get(user_ids): - return api.users_get(user_ids=','.join(user_ids)) + response = api.users_get(user_ids=','.join(user_ids), + fields='screen_name') + if len(response) < len(user_ids): + print('Error: couldn\'t find at least one of the users!', + file=sys.stderr) + sys.exit(1) + return response def friends_get(user_id): return api.friends_get(user_id=user_id) -def format_user_name(user): - return '{} {}'.format(user['last_name'], user['first_name']) - -def join_user_names(user_names): - return '{} and {}'.format(', '.join(user_names[:-1]), user_names[-1]) - -def print_mutual_friends(users, mutual_friends): - user_names = list(map(format_user_name, users)) - user_names = join_user_names(user_names) - if not mutual_friends: - print('{} don\'t have any mutual friends'.format(user_names)) - else: - print('{} are friends with these guys:'.format(user_names)) - for friend in mutual_friends: - print('\t{}'.format(format_user_name(friend))) +def format_friend(user): + s = '{} {}'.format(user['last_name'], user['first_name']) + if 'screen_name' in user: + s += ' ({})'.format(user['screen_name']) + return s if __name__ == '__main__': import argparse @@ -34,9 +30,9 @@ if __name__ == '__main__': args = parser.parse_args() users = users_get(args.user_ids) user_ids = map(lambda user: user['uid'], users) - friend_lists = map(friends_get, user_ids) - friend_lists = map(frozenset, friend_lists) + friend_lists = map(frozenset, map(friends_get, user_ids)) mutual_friends = frozenset.intersection(*friend_lists) if mutual_friends: - mutual_friends = users_get(map(str, mutual_friends)) - print_mutual_friends(users, mutual_friends) + mutual_friends = users_get(list(map(str, mutual_friends))) + for friend in mutual_friends: + print(format_friend(friend)) diff --git a/track_online.py b/track_online.py index c1bae37..7a4085d 100644 --- a/track_online.py +++ b/track_online.py @@ -4,12 +4,18 @@ from datetime import datetime import time +import sys import api def users_get(user_ids): - return api.users_get(user_ids=','.join(user_ids), - fields='online,last_seen') + response = api.users_get(user_ids=','.join(user_ids), + fields='online,last_seen') + if len(response) < len(user_ids): + print('Error: couldn\'t find at least one of the users!', + file=sys.stderr) + sys.exit(1) + return response def log(s): print('[{}] {}'.format(datetime.now().replace(microsecond=0), s)) -- cgit v1.2.3