def add(self):
        """
        Add an address from From: field of a mail. This assumes a single mail file is supplied through stdin. . 
        """

        fromLine = ""
        for l in sys.stdin:
            if l.startswith("From: "):
                fromLine = l
                break
        if fromLine == "":
            print "Not a valid mail file!"
            sys.exit(2)
        #In a line like
        #From: John Doe <*****@*****.**>
        els = fromLine.split()
        #Drop "From: "
        del els[0]
        #get the last element as mail
        mailaddr = els[-1]
        if mailaddr.startswith("<"):
            mailaddr = mailaddr[1:]
        if mailaddr.endswith(">"):
            mailaddr = mailaddr[:-1]
        #and the rest as name
        name = " ".join(els[:-1])
        #save to contacts
        client = ContactsService()
        client.ClientLogin(self.username, self.password)
        new_contact = ContactEntry(title=atom.Title(text=name))
        new_contact.email.append(Email(address=mailaddr, primary='true'))
        contact_entry = client.CreateContact(new_contact)
        print contact_entry
Exemple #2
0
def sync(info, entries, test=True):
    # setup client
    cli = ContactsService()
    cli.email = info['email']
    cli.password = info['password']
    cli.source = 'sas-contactsImport'
    cli.ProgrammaticLogin()

    # go through and find new groups
    groups = set("(Palm) " + c.category for c in entries)
    existing = [e.title.text for e in cli.GetGroupsFeed().entry]
    new_groups = groups.difference(existing)

    # create new groups
    for grp in new_groups:
        cli.CreateGroup(contacts.GroupEntry(title=atom.Title(text=grp)))

    # build group lookup dictionary
    feed = cli.GetGroupsFeed()
    groups_href = {}
    for entry in feed.entry:
        groups_href[str(entry.title.text)] = str(entry.id.text)

    # loop through contacts
    for contact in entries:
        if (test):
            contact.pprint()
            print ""
            continue

        # create basic info
        info = {}
        if contact.name:
            info['title'] = atom.Title(text=contact.name)

        # company info
        if contact.company or contact.title:
            org = contacts.Organization()
            org.org_name = contacts.OrgName(text=contact.company)
            org.org_title = contacts.OrgTitle(text=contact.title)
            info['organization'] = org

        # emails
        if contact.emails:
            info['email'] = [contacts.Email(address=s) for s in contact.emails]

        # phones
        if contact.phones:
            info['phone_number'] = [
                contacts.PhoneNumber(text=num.text, rel=num.rel)
                for num in contact.phones
            ]

        # addresses
        if contact.addresses:
            info['postal_address'] = [
                contacts.PostalAddress(text=addr.text, rel=addr.rel)
                for addr in contact.addresses
            ]

        # notes
        if contact.notes:
            info['content'] = atom.Content(text=contact.notes)

        # groups
        if contact.category:
            info['group_membership_info'] = [
                contacts.GroupMembershipInfo(
                    href=groups_href["(Palm) " + contact.category])
            ]

        # save contact
        try:
            entry = contacts.ContactEntry(**info)
            cli.CreateContact(entry)
        except Exception as ex:
            print "\nTrouble with contact:"
            contact.pprint()
            print "----------------------------"
            traceback.print_exc()
            sys.exit()