Permalink
Browse files

moved Yahoo import to use OAuth

  • Loading branch information...
brosner committed Apr 16, 2010
1 parent 3a4e357 commit 24d929b276870d2f28fe91c9400fea9b6ea1b5a6
Showing with 50 additions and 31 deletions.
  1. +45 −25 contacts_import/backends/importers.py
  2. +5 −5 contacts_import/views.py
  3. +0 −1 requirements.txt
@@ -1,6 +1,5 @@
import httplib2
import vobject
-import ybrowserauth
from django.conf import settings
from django.utils import simplejson as json
@@ -17,6 +16,7 @@
class BaseImporter(Task):
+
def run(self, credentials, persistance):
status = None
for contact in self.get_contacts(credentials):
@@ -25,6 +25,7 @@ def run(self, credentials, persistance):
class VcardImporter(BaseImporter):
+
def get_contacts(self, credentials):
for card in vobject.readComponents(credentials["stream"]):
try:
@@ -38,38 +39,57 @@ def get_contacts(self, credentials):
class YahooImporter(BaseImporter):
+
def get_contacts(self, credentials):
- ybbauth = ybrowserauth.YBrowserAuth(settings.BBAUTH_APP_ID,
- settings.BBAUTH_SHARED_SECRET)
- ybbauth.token = credentials["bbauth_token"]
- address_book_json = ybbauth.makeAuthWSgetCall("http://address.yahooapis.com/v1/searchContacts?format=json&email.present=1&fields=name,email")
- address_book = json.loads(address_book_json)
-
- for contact in address_book["contacts"]:
- email = contact["fields"][0]["data"]
+ from oauth_access.access import OAuthAccess
+ yahoo_token = credentials["yahoo_token"]
+ access = OAuthAccess("yahoo")
+ guid = access.make_api_call(
+ "json",
+ "http://social.yahooapis.com/v1/me/guid?format=json",
+ yahoo_token
+ )["guid"]["value"]
+ address_book = access.make_api_call(
+ "json",
+ "http://social.yahooapis.com/v1/user/%s/contacts?format=json&count=max&view=tinyusercard" % guid,
+ yahoo_token,
+ )
+ for contact in address_book["contacts"]["contact"]:
+ # e-mail (if not found skip contact)
try:
- first_name = contact["fields"][1]["first"]
- except (KeyError, IndexError):
- first_name = None
-
+ email = self.get_field_value(contact, "email")
+ except KeyError:
+ continue
+ # name (first and last comes together)
try:
- last_name = contact["fields"][1]["last"]
- except (KeyError, IndexError):
- last_name = None
-
- if first_name and last_name:
- name = "%s %s" % (first_name, last_name)
- elif first_name:
- name = first_name
- elif last_name:
- name = last_name
- else:
+ name = self.get_field_value(contact, "name")
+ except KeyError:
name = ""
-
+ if name:
+ first_name = name["givenName"]
+ last_name = name["familyName"]
+ if first_name and last_name:
+ name = "%s %s" % (first_name, last_name)
+ elif first_name:
+ name = first_name
+ elif last_name:
+ name = last_name
+ else:
+ name = ""
yield {
"email": email,
"name": name,
}
+
+ def get_field_value(self, contact, kind):
+ try:
+ for field in contact["fields"]:
+ if field["type"] == kind:
+ return field["value"]
+ except KeyError:
+ raise Exception("Yahoo data format changed")
+ else:
+ raise KeyError(kind)
GOOGLE_CONTACTS_URI = "http://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=1000"
View
@@ -89,11 +89,11 @@ def import_contacts(request, template_name="contacts_import/import_contacts.html
form = VcardImportForm()
if action == "import_yahoo":
- bbauth_token = request.session.pop("bbauth_token", None)
- if bbauth_token:
+ yahoo_token = request.session.pop("yahoo_token", None)
+ if yahoo_token:
runner = runner_class(YahooImporter,
user = request.user,
- bbauth_token = bbauth_token
+ yahoo_token = yahoo_token
)
results = runner.import_contacts()
return _import_success(request, results)
@@ -106,13 +106,13 @@ def import_contacts(request, template_name="contacts_import/import_contacts.html
authsub_token = authsub_token
)
results = runner.import_contacts()
- return _import_success(request, results)
+ return _import_success(request, results)
else:
form = VcardImportForm()
ctx = {
"form": form,
- "bbauth_token": request.session.get("bbauth_token"),
+ "yahoo_token": request.session.get("yahoo_token"),
"authsub_token": request.session.get("authsub_token"),
"page": page,
"task_id": request.session.pop("import_contacts_task_id", None),
View
@@ -1,5 +1,4 @@
celery==0.8.2
httplib2==0.6.0
gdata==1.3.3
-ybrowserauth==1.2
vobject==0.8.1c

0 comments on commit 24d929b

Please sign in to comment.