forked from jtauber-archive/django-friends
/
importer.py
110 lines (95 loc) · 3.42 KB
/
importer.py
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
from django.conf import settings
from friends.models import Contact
import vobject
import ybrowserauth
import simplejson
import gdata.contacts.service
def import_vcards(stream, user):
"""
Imports the given vcard stream into the contacts of the given user.
Returns a tuple of (number imported, total number of cards).
"""
total = 0
imported = 0
for card in vobject.readComponents(stream):
total += 1
try:
name = card.fn.value
email = card.email.value
try:
Contact.objects.get(user=user, email=email)
except Contact.DoesNotExist:
Contact(user=user, name=name, email=email).save()
imported += 1
except AttributeError:
pass # missing value so don't add anything
return imported, total
def import_yahoo(bbauth_token, user):
"""
Uses the given BBAuth token to retrieve a Yahoo Address Book and
import the entries with an email address into the contacts of the
given user.
Returns a tuple of (number imported, total number of entries).
"""
ybbauth = ybrowserauth.YBrowserAuth(settings.BBAUTH_APP_ID, settings.BBAUTH_SHARED_SECRET)
ybbauth.token = bbauth_token
address_book_json = ybbauth.makeAuthWSgetCall("http://address.yahooapis.com/v1/searchContacts?format=json&email.present=1&fields=name,email")
address_book = simplejson.loads(address_book_json)
total = 0
imported = 0
for contact in address_book["contacts"]:
total += 1
email = contact['fields'][0]['data']
try:
first_name = contact['fields'][1]['first']
except (KeyError, IndexError):
first_name = None
try:
last_name = contact['fields'][1]['last']
except (KeyError, IndexError):
last_name = None
if first_name and last_name:
name = first_name + " " + last_name
elif first_name:
name = first_name
elif last_name:
name = last_name
else:
name = None
try:
Contact.objects.get(user=user, email=email)
except Contact.DoesNotExist:
Contact(user=user, name=name, email=email).save()
imported += 1
return imported, total
def import_google(authsub_token, user):
"""
Uses the given AuthSub token to retrieve Google Contacts and
import the entries with an email address into the contacts of the
given user.
Returns a tuple of (number imported, total number of entries).
"""
contacts_service = gdata.contacts.service.ContactsService()
contacts_service.auth_token = authsub_token
contacts_service.UpgradeToSessionToken()
entries = []
feed = contacts_service.GetContactsFeed()
entries.extend(feed.entry)
next_link = feed.GetNextLink()
while next_link:
feed = contacts_service.GetContactsFeed(uri=next_link.href)
entries.extend(feed.entry)
next_link = feed.GetNextLink()
total = 0
imported = 0
for entry in entries:
name = entry.title.text
for e in entry.email:
email = e.address
total += 1
try:
Contact.objects.get(user=user, email=email)
except Contact.DoesNotExist:
Contact(user=user, name=name, email=email).save()
imported += 1
return imported, total