aboutsummaryrefslogblamecommitdiffstatshomepage
path: root/bin/mutual_friends.py
blob: b72b31739aafc9f6de31b229687ac4b272a6398b (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                                           





                                   
                                
                             
 
                                                                        
 

                                                                





                                                                     
 


                                                          
 

                        
 





















                                                                             






                         
 







                                                                                

                          
 





                                          

                                                                                     
 
                                                              
                                                          

                                                                   

                                                     


                                                                                  
 
                              
 
                          
                                    
 
                                                                                   
                                                          


                                                                 
# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.

from collections import OrderedDict
import csv
from enum import Enum
import json
import sys

from vk.api import API, Language
from vk.user import UserField

OUTPUT_FIELDS = UserField.UID, UserField.FIRST_NAME, UserField.LAST_NAME

def query_friend_list(api, user):
    return api.friends_get(user.get_uid(), fields=OUTPUT_FIELDS)

def extract_output_fields(user):
    new_user = OrderedDict()
    for field in OUTPUT_FIELDS:
        new_user[str(field)] = user[field] if field in user else None
    return new_user

class OutputWriterCSV:
    def __init__(self, fd=sys.stdout):
        self._writer = csv.writer(fd, lineterminator='\n')

    def __enter__(self):
        return self

    def __exit__(self, *args):
        pass

    def write_mutual_friends(self, friend_list):
        for user in mutual_friends:
            user = extract_output_fields(user)
            self._writer.writerow(user.values())

class OutputWriterJSON:
    def __init__(self, fd=sys.stdout):
        self._fd = fd
        self._array = []

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self._fd.write(json.dumps(self._array, indent=3, ensure_ascii=False))

    def write_mutual_friends(self, friend_list):
        for user in friend_list:
            self._array.append(extract_output_fields(user))

class OutputFormat(Enum):
    CSV = 'csv'
    JSON = 'json'

    def __str__(self):
        return self.value

    def create_writer(self, fd=sys.stdout):
        if self is OutputFormat.CSV:
            return OutputWriterCSV(fd)
        elif self is OutputFormat.JSON:
            return OutputWriterJSON(fd)
        else:
            raise NotImplementedError('unsupported output format: ' + str(self))

if __name__ == '__main__':
    import argparse

    def output_format(s):
        try:
            return OutputFormat(s)
        except ValueError:
            raise argparse.ArgumentError()

    parser = argparse.ArgumentParser(
        description='Learn who your ex and her new boyfriend are both friends with.')

    parser.add_argument(metavar='UID', dest='uids', nargs='+',
                        help='user IDs or "screen names"')
    parser.add_argument('--output-format', type=output_format,
                        choices=tuple(fmt for fmt in OutputFormat),
                        default=OutputFormat.CSV,
                        help='specify output format')
    parser.add_argument('--output', type=argparse.FileType('w', encoding='utf-8'),
                        default=sys.stdout,
                        help='set output file path (standard output by default)')

    args = parser.parse_args()

    api = API(Language.EN)
    users = api.users_get(args.uids)

    friend_lists = map(lambda user: frozenset(query_friend_list(api, user)), users)
    mutual_friends = frozenset.intersection(*friend_lists)

    with args.output_format.create_writer(args.output) as writer:
        writer.write_mutual_friends(mutual_friends)