Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mhgbrown/9d145ec2abd3f7cd8b6e030ea403e416 to your computer and use it in GitHub Desktop.
Save mhgbrown/9d145ec2abd3f7cd8b6e030ea403e416 to your computer and use it in GitHub Desktop.
Get public Google profile information from email addresses
# Some scratch code to get public profile information from Google
# based on a list of email addresses. Look at the People Search API
# for more details
import httplib2
import logging
from urllib.parse import urlparse, parse_qs
# via https://github.com/google/google-api-python-client
from apiclient.discovery import build
from django.conf import settings
# assume a contact model
from .models import Contact
logger = logging.getLogger('default')
def chunks(l, n):
"""
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
Yield successive n-sized chunks from l.
"""
for i in range(0, len(l), n):
yield l[i:i + n]
def on_contact_information(self, request_id, response, exception):
"""
Called on receiving Google profile information. Attempts to save a few fields
in the database via a Model named "Contact"
"""
if exception is not None:
# woops bai
print(exception)
return
url = response.get('selfLink')
parsed = urlparse(url)
email = parse_qs(parsed.query)['query']
contacts = Contact.objects.filter(email=email[0])
if not contacts.exists():
Contact.objects.create(email=email[0])
# refresh the queryset
# https://github.com/django/django/commit/6d5daa30cf29d0bada0586213ecb2959152566b4
contacts.all()
updates = {}
for item in response['items']:
if item.get('image') and item['image'].get('url'):
updates['avatar_url'] = item['image'].get('url')
if item.get('gender'):
updates['gender'] = item.get('gender')
if item.get('ageRange'):
age_range = item['ageRange']
updates['age_min'] = age_range.get('min')
updates['age_max'] = age_range.get('max')
if item.get('placesLived'):
places_lived = item['placesLived']
for place in places_lived:
updates['location'] = place.get('value')
# if we've found the primary one, get outta here
if place.get('primary'):
break
if updates:
logger.debug(updates)
contacts.update(**updates)
def discover_contact_information(self, email_addresses):
"""
Discover public Google profile information for the given email
addresses and call on_contact_information with the results
"""
limit = 100
http = httplib2.Http()
service = build('plus', 'v1', developerKey=settings.GOOGLE_API_KEY)
people_resource = service.people()
for chunk in chunks(email_addresses, limit):
batch = service.new_batch_http_request()
for email in chunk:
logger.debug('Queueing {}'.format(email))
batch.add(people_resource.search(query=email), callback=self.on_contact_information)
batch.execute(http=http)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment