Created
September 27, 2012 21:48
-
-
Save jclement/3796658 to your computer and use it in GitHub Desktop.
Robotize Google Contact Photos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# ============================================================ | |
# Configuration file: ~/.gdata | |
# [gdata] | |
# user: user@gmail.com | |
# password: password or per-app password | |
# ============================================================ | |
import sys | |
import getpass | |
import atom | |
import gdata.contacts.data | |
import gdata.contacts.client | |
import urllib2 | |
import urllib | |
import time | |
import ConfigParser | |
import os | |
import tempfile | |
class Robotizer: | |
def __init__(self, user, password): | |
self.__gd = gdata.contacts.client.ContactsClient(source='Robotize') | |
self.__gd.ClientLogin(user, password, self.__gd.source) | |
def run(self, replace_all): | |
# load all contacts | |
q = gdata.contacts.client.ContactsQuery() | |
q.max_results = 10000 | |
feed = self.__gd.GetContacts(q = q) | |
for i, entry in enumerate(feed.entry): | |
if entry.name and entry.name.full_name: | |
# pull display name and lower case (hash is case sensitive) | |
name = entry.name.full_name.text.lower() | |
# contact doesn't have a photo of eTag is null | |
hasPhoto = entry.GetPhotoLink().etag <> None | |
# determine if we should try updating the photo | |
if (not hasPhoto or replace_all) and name: | |
print "updating", name | |
# pull new photo from robohash for the name | |
fin = urllib2.urlopen('http://robohash.org/' + name + '.png') | |
fh, fn = tempfile.mkstemp(suffix='.png') | |
fout = open(fn, 'wb') | |
fout.write(fin.read()) | |
fout.close() | |
fin.close() | |
self.__gd.ChangePhoto(fn, entry, content_type='image/png') | |
os.unlink(fn) | |
def main(): | |
cfg = ConfigParser.ConfigParser() | |
cfg.read(os.path.expanduser('~/.gdata')) | |
r = Robotizer(cfg.get('gdata','user'), cfg.get('gdata','password')) | |
r.run(False) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment