Welcome, guest | Sign In | My Account | Store | Cart

READ BEFORE YOU USE THE CODE*

Requirement

  1. Mac OS with python obj-c wrapper built-in. Typically, Mac OS Snow leopard, or Lion comes with python wrapper of objc.
  2. If you want to also sync to your iphone, simply enable iCloud feature provided by apple, and run this script on a macbook with iCloud enabled.
  3. Need fbconsole module, which can be downloaded at https://github.com/facebook/fbconsole

This script will download the profile pictures, first name, last name of your friends, and insert them into your address book if it does not exist.

Python, 139 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
   Copyright 2011 Shao-Chuan Wang <shaochuan.wang AT gmail.com>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
"""


from AddressBook import *
from Foundation import *
import objc
import fbconsole
import urllib
import glob
import os

fbconsole.AUTH_SCOPE = ['friends_location', 'read_friendlists']

def retrieve_friends():
    fbconsole.authenticate()
    friends = fbconsole.get('/me/friends', {'fields':'id,first_name,last_name'})
    next_url = friends.get('paging')
    friends = friends['data']
    while next_url:
        p = fbconsole.urlparse(next_url['next'])
        more_friends = fbconsole.get(p.path+'?'+p.query)
        next_url = more_friends.get('paging')
        more_friends = more_friends['data']
        if not more_friends:
            break
        friends.extend(more_friends)
    return friends

def download_profile_pics(friends):
    print 'downloading profile pictures...'
    for f in friends:
        picture_url = '/%s/picture' % (f['id'],)
        profile_pic = fbconsole.graph_url(picture_url)
        img_path = u'/tmp/%s.jpg' % (f['id'],)
        if os.path.exists(img_path):
            f['img_path'] = img_path
            continue
        urllib.urlretrieve(profile_pic, img_path)
        print f['first_name'], f['last_name'], f['id']
        f['img_path'] = img_path
    return friends

def clean_tmp_pictures():
    for j in glob.glob('/tmp/*.jpg'):
        try:
            os.remove(j)
        except OSError, e:
            print j, e

class SearchConflictException(Exception):
    pass

def searchABBook(first, last):
    s = ABPerson.searchElementForProperty_label_key_value_comparison_(u'First', 
            None, None, first, kABEqualCaseInsensitive)
    ab = ABAddressBook.sharedAddressBook()
    entries = ab.recordsMatchingSearchElement_(s)
    ret = []
    for e in entries:
        if e.valueForProperty_(u'Last').lower() == last.lower():
            ret.append(e)
    if len(ret) > 1:
        raise SearchConflictException(
                "There are multiple %s %s, please resolve the conflict." % (
                    first, last))
    if ret:
        return ret[0]
    else:
        return None

# You can sync only for the people at certain location or for some friend list
def sync_address_book(friends, location, friend_list=u''):
    ab = ABAddressBook.sharedAddressBook()
    for f in friends:
        # filter out the location is not in new york
        try:
            user_entry = fbconsole.get('/%s' % (f['id']))
        except:
            continue
        if location:
            location_dict = user_entry.get('location')
            if not location_dict or not location_dict.get('name'):
                continue
            if not location in location_dict.get('name'):
                continue

        # create record only if it does not exists
        try:
            p = searchABBook(f['first_name'], f['last_name'])
        except SearchConflictException:
            # you can decide what to do if there is exactly the same first
            # name and last name in your existing address book
            continue
        if not p:
            p = ABPerson.alloc().init()
        img_data = NSData.dataWithContentsOfFile_(f['img_path'])
        p.setImageData_(img_data)
        p.setValue_forProperty_(f['first_name'], u'First')
        p.setValue_forProperty_(f['last_name'], u'Last')
        ab.addRecord_(p)
        print f['first_name'], f['last_name']
    ab.save()

def readABBook():
    ab = ABAddressBook.sharedAddressBook()
    ppl = ab.people()
    for p in ppl:
        if not p.valueForProperty_(u"First"):
            continue
        print p.valueForProperty_(u"First"), p.valueForProperty_(u"Last")

if __name__=='__main__':
    friends = retrieve_friends()
    friends = download_profile_pics(friends)

    sync_address_book(friends, u'Seattle')

    # you can clean the temporary files
    #clean_tmp_pictures()

1 comment

Denis Mayer 8 years, 10 months ago  # | flag

I have been trying to find a python code sample on how to add both a work and a home email address to the AddressBook. I believe you would need add a multivalue object but I dont know how to do this in pyobjc. Do you know how to do this?