Skip to content

Commit

Permalink
Got support for contacts. It's minimal, but there. No support yet
Browse files Browse the repository at this point in the history
for creating or updating so this is far from what issue googleapis#7 wants.
But never the less it is there.

Most annoyingly this now means I need to create another pair of
unit test classes. ugh. I do not enjoy doing that. But I also don't
enjoy eating vegatables, but I must so I shall.
  • Loading branch information
Toben Archer committed May 15, 2015
1 parent efc267b commit e6799d9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
2 changes: 2 additions & 0 deletions O365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
from inbox import Inbox
from message import Message
from schedule import Schedule
from contact import Contact
from group import Group

#To the King!
84 changes: 84 additions & 0 deletions O365/contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2015 by Toben "Narcolapser" Archer. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its documentation for any purpose
# and without fee is hereby granted, provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in supporting documentation, and
# that the name of Toben Archer not be used in advertising or publicity pertaining to distribution of
# the software without specific, written prior permission. TOBEN ARCHER DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
# SHALL TOBEN ARCHER BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import requests
import base64
import json
import logging
import time

logging.basicConfig(filename='o365.log',level=logging.DEBUG)

log = logging.getLogger(__name__)

class Contact( object ):
'''
Contact manages lists of events on an associated contact on office365.
Methods:
getName - Returns the name of the contact.
getContactId - returns the GUID that identifies the contact on office365
getId - synonym of getContactId
getEvents - kicks off the process of fetching events.
fetchEvents - legacy duplicate of getEvents
Variable:
events_url - the url that is actually called to fetch events. takes an ID, start, and end.
time_string - used for converting between struct_time and json's time format.
'''
con_url = 'https://outlook.office365.com/api/v1.0/me/contacts'
time_string = '%Y-%m-%dT%H:%M:%SZ'

def __init__(self, json=None, auth=None):
'''
Wraps all the informaiton for managing contacts.
'''
self.json = json
self.auth = auth

if json:
log.debug('translating contact information into local variables.')
self.contactId = json['Id']
self.name = json['DisplayName']

def getName(self):
'''Get the contact's Name.'''
return self.json['DisplayName']

def getContactId(self):
'''Get contact's GUID for office 365. mostly used interally in this library.'''
return self.json['Id']

def getId(self):
'''Get contact's GUID for office 365. mostly used interally in this library.'''
return self.getContactId()

def getFirstEmailAddress(self):
'''Get the contact's first Email address. returns just the email address.'''
return self.json['EmailAddresses'][0]['Address']

def getEmailAdresses(self):
'''Get's all the contacts email addresses. returns a list of strings.'''
ret = []
for e in self.json['EmailAddresses']:
ret.append(e['Address'])

def getFirstEmailInfo(self):
'''gets an email address and it's associated date for the first email address.'''
return self.json['EmailAddresses'][0]

def getAllEmailInfo(self):
'''Gets email addresses and any data that goes with it such as name, returns dict'''
return self.json['EmaillAddresses']

#To the King!
27 changes: 20 additions & 7 deletions O365/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from O365 import Contact
from contact import Contact
import logging
import json
import requests
Expand All @@ -29,9 +29,9 @@ class Group( object ):
getContactss -- begins the actual process of downloading contacts.
Variables:
cat_url -- the url that is requested for the retrival of the contacts.
con_url -- the url that is requested for the retrival of the contacts.
'''
cal_url = 'https://outlook.office365.com/api/v1.0/me/contacts'
con_url = 'https://outlook.office365.com/api/v1.0/me/contacts'

def __init__(self, email, password):
'''Creates a group class for managing all contacts associated with email+password.'''
Expand All @@ -43,11 +43,24 @@ def __init__(self, email, password):
def getContact(self):
'''Begin the process of downloading contact metadata.'''
log.debug('fetching contacts.')
response = requests.get(self.cal_url,auth=self.auth)
response = requests.get(self.con_url,auth=self.auth)
log.info('Response from O365: %s', str(response))

for calendar in response.json()['value']:
pass

for contact in response.json()['value']:
duplicate = False
log.debug('Got a contact Named: {0}'.format(contact['DisplayName']))
for existing in self.contacts:
if existing.json['Id'] == contact['Id']:
log.info('duplicate contact')
duplicate = True
break

if not duplicate:
self.contacts.append(Contact(contact,self.auth))

log.debug('Appended Contact.')


log.debug('all calendars retrieved and put in to the list.')
return True

Expand Down

0 comments on commit e6799d9

Please sign in to comment.