From af81c32b093c12f3fbaa013d8345ac3a04bbe80a Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 12 Feb 2016 02:57:50 +0300 Subject: use the standard logging module & refactoring Including more command line parameters & better usage messages. --- print_mutual_friends.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'print_mutual_friends.py') diff --git a/print_mutual_friends.py b/print_mutual_friends.py index 6895a3c..b3beb39 100644 --- a/print_mutual_friends.py +++ b/print_mutual_friends.py @@ -8,26 +8,30 @@ def users_get(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) + raise RuntimeError('Couldn\'t find at least one of the users!') return response def friends_get(user_id): return api.friends_get(user_id=user_id) -def format_friend(user): - s = '{} {}'.format(user['last_name'], user['first_name']) +def extract_screen_name_or_uid(user): if 'screen_name' in user: - s += ' ({})'.format(user['screen_name']) - return s + return user['screen_name'] + return 'id' + str(user['uid']) + +def format_friend(user): + return '{} {} ({})'.format( + user['last_name'], user['first_name'], + extract_screen_name_or_uid(user)) if __name__ == '__main__': import argparse - parser = argparse.ArgumentParser() + 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='+', help='user IDs or "screen names"') args = parser.parse_args() + users = users_get(args.user_ids) user_ids = map(lambda user: user['uid'], users) friend_lists = map(frozenset, map(friends_get, user_ids)) -- cgit v1.2.3