Exemple #1
0
 def __fetch_contacts(self):
     client = gdata.contacts.client.ContactsClient(source=self.USER_AGENT)
     query = gdata.contacts.client.ContactsQuery(
         max_results=self.num_contacts)
     self.auth_token.authorize(client)
     feed = client.GetContacts(q=query)
     return feed
 def get_contacts(self, session, callback, response):
     client = gdata.contacts.client.ContactsClient(source='pulp')
     client.auth_token = session['access_token']
     query = gdata.contacts.client.ContactsQuery()
     query.max_results = 999999999
     feed = client.GetContacts(q=query)
     for contact in feed.entry:
         contact_name = contact.name.full_name.text if contact.name else ''
         for email in contact.email:
             print 'Contact: %s, name: %s' % (email.address, contact_name)
Exemple #3
0
    def GetContactsThread(self):
        self.contacts = list()
        self.specialList = list()
        client = gdata.contacts.client.ContactsClient(source='email assistant')
        client.ClientLogin(self.user, self.passwd, client.source)

        if re.search(developerEmail, self.user, re.I):
            limit = 20
        else:
            limit = -1
        feed = client.GetGroups()
        for i, entry in enumerate(feed.entry):
            if entry.title.text == specialList:
                specialListid = entry.id.text
        feed = client.GetContacts()
        cntr = 1
        while feed:
            if cntr == limit: break
            for entry in feed.entry:
                cntr += 1
                if cntr == limit: break
                afam = False
                for group in entry.group_membership_info:
                    if group.href == specialListid:
                        afam = True
                for contactEmail in entry.email:
                    if contactEmail.primary:
                        if entry.title.text is not None:
                            fmted = email.utils.formataddr((entry.title.text, \
                                                              contactEmail.address))
                        else:
                            fmted = contactEmail.address
                        self.contacts.append(fmted)
                        if afam:
                            self.specialList.append(fmted)
            nextfeed = feed.GetNextLink()
            feed = None
            if nextfeed:
                feed = client.GetContacts(uri=nextfeed.href)
        feed = None
        client = None

        self.callbackContacts(self.contacts, self.specialList)
def redirect_app():
    """Redirect to my app after gmail"""

    key = request.args.get("code")
    
    if key:
        user_id = flask.session['user_id']
        user = User.query.get(user_id)
        token = pickle.load(open('token' + str(user_id) + '.p', 'rb'))
        token.get_access_token(key)
        client = gdata.contacts.client.ContactsClient(source='appname')
        client = token.authorize(client)
        feed = client.GetContacts()
        contacts = gmail_contacts.parse_contacts(user, str(feed))

        return redirect("/users/%s" % user.user_id)
Exemple #5
0
def getcontacts():
	a = gdata.contacts.client.ContactsQuery(max_results=10000)
	contacts_feed = client.GetContacts(q=a)

	return_value = [('','')]
	for i,entry in enumerate(contacts_feed.entry):
		name = ''
		phone = ''
		entry_str = ''
		if entry.name:
			name = entry.name.full_name.text
		for ph in entry.phone_number:
			if ph:
				phone = ph.text
		if len(name)>0:
			if len(phone)>0:
				if i == 0:
					return_value[0] = (name,phone)
				else:
					return_value += [(name,phone)]
	return return_value
Exemple #6
0
    def update(self, client, **kwargs):
        """Search for Person.query on Google Contacts and set email and phone number from first match.
        
        Will only sync once per instance."""
        phone_type_preference = ShiftCalendar.default_phone_type_preference
        if 'phone_type_preference' in kwargs:
            phone_type_preference = kwargs['phone_type_preference']
        if not self.have_synced:
            query = gdata.contacts.client.ContactsQuery()
            query.text_query = self.query
            feed = client.GetContacts(q=query)
            entry = None
            if len(feed.entry) == 1:
                entry = feed.entry[0]
            elif len(feed.entry) > 1:
                entry = feed.entry[0]
                logging.warning(
                    "Calendar title '%s' is too broad, matches %d contacts.",
                    self.query, len(feed.entry))
            if entry is None:
                logging.error(
                    "Current shift does not match any contact! Query was: '%s'",
                    self.query)
                sys.exit(os.EX_DATAERR)
            person = {'email': None, 'phone': None}
            for email in entry.email:
                if email.primary and email.primary == 'true':
                    person['email'] = email.address
            phone_numbers = {}
            for phone in entry.phone_number:
                # rel example: http://schemas.google.com/g/2005#mobile
                rel = phone.rel.split("#").pop()
                phone_numbers[rel] = phone.text
            for rel in phone_type_preference:
                if rel in phone_numbers:
                    person['phone'] = phone_numbers[rel]

            self.email = person['email']
            self.phone = person['phone']
            self.have_synced = True
Exemple #7
0
def get_contacts(credentials):

    auth2token = gdata.gauth.OAuth2Token(
        client_id=credentials.client_id,
        client_secret=credentials.client_secret,
        scope=['https://www.google.com/m8/feeds'],
        access_token=credentials.access_token,
        refresh_token=credentials.refresh_token,
        user_agent='contact-sync',
    )
    client = gdata.contacts.client.ContactsClient()
    auth2token.authorize(client)
    contacts_query = gdata.contacts.client.ContactsQuery(max_results=3000)
    contacts_feed = client.GetContacts(query=contacts_query)

    google_contact_feed = contacts_feed.entry

    contacts = []
    for contact_entry in google_contact_feed:
        google_contact = GoogleContact.parse(contact_entry)
        if google_contact:
            contacts.append(google_contact)

    return contacts
def PrintContacts(client):
    print(('\nListing contacts for %s...' % client.auth_token.requestor_id))
    feed = client.GetContacts()
    for entry in feed.entry:
        print((entry.title.text))
Exemple #9
0
import gdata.contacts.data
import gdata.contacts.client
import vobject
import pprint

if __name__ == "__main__":
    password = getpass.getpass('Enter Google password: '******'charlespence.net-backup-v1')
    client.ClientLogin('*****@*****.**', password, client.source)
    
    fp = open('contacts.vcf', 'w')
    
    query = gdata.contacts.client.ContactsQuery()
    query.max_results = 9999
    feed = client.GetContacts(q=query)
        
    for i, entry in enumerate(feed.entry):
        if entry.deleted:
            continue
        
        vcard = vobject.vCard()
        
        if entry.name:
            if entry.name.family_name:
                family = entry.name.family_name.text
            else:
                family = ''
            if entry.name.given_name:
                given = entry.name.given_name.text
            else:
Exemple #10
0
def import_contacts_authorized(request):
    """ Import Google contacts, when authorized by user via Google WWW.

    Reference, as of 20140102:
        https://developers.google.com/google-apps/contacts/v3/
    """

    import gdata.contacts.client

    user = request.user.mongo
    dupes = 0
    imported = 0
    address_book = set(a.rsplit(u' ', 1)[1] for a in user.address_book)

    current_mails = address_book.copy()
    current_mails |= [f.email for f in user.friends]

    auth_token = gdata.gauth.OAuth2Token(
        client_id=settings.GOOGLE_OAUTH2_CLIENT_ID,
        client_secret=settings.GOOGLE_OAUTH2_CLIENT_SECRET,
        scope=settings.GOOGLE_OAUTH2_CONTACTS_SCOPE,
        user_agent=settings.DEFAULT_USER_AGENT)

    auth_token.redirect_uri = 'http://{0}{1}'.format(
        settings.SITE_DOMAIN, settings.GOOGLE_OAUTH2_CONTACTS_REDIRECT_URI)

    # Heads UP: as of 20130104, "service" seems to be broken.
    # Google invites to use "client" instead.
    # Ref: https://code.google.com/p/gdata-python-client/issues/detail?id=549
    # client = gdata.contacts.service.ContactsService(source='1flow.io')
    client = gdata.contacts.client.ContactsClient(source='1flow.io')

    auth_token.get_access_token(request.GET['code'])
    auth_token.authorize(client)

    query = gdata.contacts.client.ContactsQuery()
    # query.updated_min = '2008-01-01'
    query.max_results = 1000

    feed = client.GetContacts(q=query)

    # The "all contacts" way, but it only return a limited subset of them.
    # feed = client.GetContacts()

    for entry in feed.entry:

        full_name = first_name = last_name = u''

        try:
            full_name = u'{0} {1}'.format(entry.name.given_name.text,
                                          entry.name.family_name.text)

        except AttributeError:
            try:
                full_name = entry.name.full_name.text

            except:
                pass

            else:
                try:
                    first_name, last_name = full_name.split(u' ', 1)

                except:
                    pass
        else:
            first_name = entry.name.given_name.text
            last_name = entry.name.family_name.text

        for email in entry.email:

            if email.primary and email.primary == 'true':

                if email.address in current_mails:
                    if email.address in address_book:
                        user.ab_remove(email.address)

                        # NOTE: User is the Django User model here.
                        User.get_or_create_friend(email=email.address,
                                                  first_name=first_name,
                                                  last_name=last_name)

                    else:
                        dupes += 1

                else:
                    user.address_book.append(u'{0} {1}'.format(
                        full_name or email.address, email.address))
                    imported += 1

        # for group in entry.group_membership_info:
        #    print '    Member of group: %s' % (group.href)

    if imported:
        user.save()

    return render(request, 'import-contacts.html', {
        'imported': imported,
        'dupes': dupes
    })
Exemple #11
0
    def t3():
        import gdata.gauth
        # import webbrowser
        from selenium import webdriver
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By

        CLIENT_ID = '428607628598-dd19u5f3ma4rssk8sbm1ccjsbe3pq1ip.apps.googleusercontent.com'
        CLIENT_SECRET = 'fLOZ4Po28pAUB1wYA_WbsZql'
        SCOPE = 'https://www.google.com/m8/feeds/'
        USER_AGENT = 'Scarlett-AI'

        auth_token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,
                                             client_secret=CLIENT_SECRET,
                                             scope=SCOPE,
                                             user_agent=USER_AGENT)

        APPLICATION_REDIRECT_URI = 'http://localhost/oauth2callback'
        authorize_url = auth_token.generate_authorize_url(
            redirect_uri=APPLICATION_REDIRECT_URI)

        print(authorize_url)

        driver = webdriver.Chrome(
            executable_path="/usr/local/bin/chromedriver")
        driver.get(authorize_url)
        import time

        WebDriverWait(
            driver,
            5).until(lambda driver: driver.find_element(By.ID, "identifierId"))
        username = driver.find_element_by_id("identifierId")
        username.send_keys("d.kanhar")

        WebDriverWait(driver, 5).until(
            lambda driver: driver.find_element(By.ID, "identifierNext"))
        driver.find_element_by_id("identifierNext").click()
        # time.sleep(10)

        try:
            WebDriverWait(driver, 5).until(
                lambda driver: driver.find_element(By.ID, "skipChallenge"))
            driver.find_element_by_id("skipChallenge").click()

            try:
                WebDriverWait(driver, 5).until(
                    lambda driver: driver.find_element(By.ID, "password"))
                password = driver.find_element_by_name("password")
                password.send_keys("2NINitu!1")

                WebDriverWait(driver, 5).until(
                    lambda driver: driver.find_element(By.ID, "passwordNext"))
                driver.find_element_by_id("passwordNext").click()

            except:
                WebDriverWait(driver,
                              5).until(lambda driver: driver.find_element(
                                  By.LINK_TEXT, "Enter your password"))
                driver.find_element_by_link_text("Enter your password").click()

                WebDriverWait(driver, 5).until(
                    lambda driver: driver.find_element(By.ID, "password"))
                password = driver.find_element_by_name("password")
                password.send_keys("2NINitu!1")

                WebDriverWait(driver, 5).until(
                    lambda driver: driver.find_element(By.ID, "passwordNext"))
                driver.find_element_by_id("passwordNext").click()
        except:
            WebDriverWait(
                driver,
                5).until(lambda driver: driver.find_element(By.ID, "password"))
            password = driver.find_element_by_name("password")
            password.send_keys("2NINitu!1")

            WebDriverWait(driver, 5).until(
                lambda driver: driver.find_element(By.ID, "passwordNext"))
            driver.find_element_by_id("passwordNext").click()
        # time.sleep(10)

        WebDriverWait(driver, 5).until(
            lambda driver: driver.find_element(By.ID, "submit_approve_access"))
        driver.find_element_by_id("submit_approve_access").click()

        time.sleep(5)

        current_url = driver.current_url
        # print(current_url)
        import atom.http_core
        redirect_url = current_url
        url = atom.http_core.ParseUri(redirect_url)
        token = auth_token.get_access_token(url.query)
        # print(token)
        # print(auth_token)
        print(driver.current_url)

        import gdata.contacts.client

        client = gdata.contacts.client.ContactsClient(source='Scarlett-AI')
        query = gdata.contacts.client.ContactsQuery(max_results=10000)
        auth_token.authorize(client)
        feed = client.GetContacts(q=query)
        print(feed)
        # print(feed[0])

        from contactsFeedParser import GoogleContactsFeedParser
        parser = GoogleContactsFeedParser()
        print(100 * "-")
        parser.parse_(message=feed)

        driver.quit()

        pass