Skip to content

Commit

Permalink
Removed scafolding code. It's starting to look cleaner.
Browse files Browse the repository at this point in the history
  • Loading branch information
Toben Archer committed Apr 10, 2015
1 parent 1ff5561 commit 2842225
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 40 deletions.
9 changes: 9 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# file GENERATED by distutils, do NOT edit
setup.py
O365/__init__.py
O365/attachment.py
O365/cal.py
O365/event.py
O365/inbox.py
O365/message.py
O365/schedule.py
1 change: 1 addition & 0 deletions O365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
from message import Message
from schedule import Schedule

#To the King!
12 changes: 12 additions & 0 deletions O365/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def __init__(self,json=None):
else:
self.json = {}

def isType(self,typeString):
'''
This function lets you know what type the file is.
'''
return '.'+typeString.lower() in self.json['Name'].lower()

def getType(self):
'''
returns the file extension
'''
return self.json['Name'][self.json['Name'].rindex('.'):]

def save(self,location):
'''
Location: path to where the file is to be saved.
Expand Down
20 changes: 19 additions & 1 deletion O365/cal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 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
Expand Down Expand Up @@ -49,22 +62,27 @@ def getEvents(self,start=None,end=None):
Pulls events in for this calendar. default range is today to a year now.
'''

#If no start time has been supplied, it is assumed you want to start as of now.
if not start:
start = time.strftime(self.time_string)

#If no end time has been supplied, it is assumed you want the end time to be a year
#from what ever the start date was.
if not end:
end = time.time()
end += 3600*24*365.25
end = time.gmtime(end)
end = time.strftime(self.time_string,end)

#This is where the actual call to Office365 happens.
response = requests.get(self.events_url.format(self.json['Id'],start,end),auth=self.auth)
log.info('Response from O365: %s', str(response))

#This takes that response and then parses it into individual calendar events.
for event in response.json()['value']:
try:
log.debug('appended event: %s',event['Subject'])
self.events.append(Event(event,self.auth,self))
log.debug('appended event: %s',event['Subject'])
except Exception as e:
log.info('failed to append calendar: %',str(e))

Expand Down
20 changes: 15 additions & 5 deletions O365/event.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 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 logging
import json
import requests
Expand All @@ -8,6 +21,7 @@
log = logging.getLogger(__name__)

class Event( object ):
#Formated time string for translation to and from json.
time_string = '%Y-%m-%dT%H:%M:%SZ'
#takes a calendar ID
create_url = 'https://outlook.office365.com/api/v1.0/me/calendars/{0}/events'
Expand Down Expand Up @@ -100,8 +114,8 @@ def update(self,calendar=None):
data = json.dumps(req)

try:
log.debug('sending patch request now')
response = requests.patch(self.update_url.format(self.Id),data,headers=headers,auth=self.auth)
log.debug('sending patch request now')
except:
log.debug('response to event creation: %s',str(response))
return False
Expand Down Expand Up @@ -152,10 +166,6 @@ def fullcalendarioJson(self):
ret['driverEmail'] = self.json['Organizer']['EmailAddress']['Address']
ret['start'] = self.json['Start']
ret['end'] = self.json['End']

#ret['start'] = time.strftime(self.time_string,self.start)
#ret['end'] = time.strftime(self.time_string,self.end)

ret['IsAllDay'] = self.json['IsAllDay']
return ret

Expand Down
22 changes: 18 additions & 4 deletions O365/inbox.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 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.

from O365.message import Message
import logging
import json
Expand All @@ -8,14 +21,15 @@
log = logging.getLogger(__name__)

class Inbox( object ):
#inbox_url = 'https://outlook.office365.com/EWS/OData/Me/Messages?$filter=IsRead eq {0}'
#url for fetching emails. Takes a flag for whether they are read or not.
inbox_url = 'https://outlook.office365.com/api/v1.0/me/messages?$filter=IsRead eq {0}'

def __init__(self, email, password):
def __init__(self, email, password,getNow=True):
log.debug('creating inbox for the email %s',email)
self.auth = (email,password)
self.messages = []
self.getMessages()
if getNow:
self.getMessages()


def getMessages(self,IsRead=False):
Expand All @@ -27,8 +41,8 @@ def getMessages(self,IsRead=False):
IsRead: Set this as True if you want to include messages that have been read.
'''

log.debug('fetching messages.')
print self.inbox_url.format(str(IsRead).lower())
response = requests.get(self.inbox_url.format(str(IsRead).lower()),auth=self.auth)
log.info('Response from O365: %s', str(response))

Expand Down
39 changes: 22 additions & 17 deletions O365/message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 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.

from O365 import Attachment
import logging
import json
Expand Down Expand Up @@ -28,14 +41,6 @@ def __init__(self, json=None, auth=None):
self.attachments = []
self.reciever = None

# if json:
# log.debug('translating message information into local variables.')
# self.messageId = json['Id']
# self.sender = json['Sender']['EmailAddress']['Name']
# self.address = json['Sender']['EmailAddress']['Address']
# self.subject = json['Subject']
# self.body = json['Body']['Content']


def fetchAttachments(self):
if not self.hasAttachments:
Expand All @@ -47,11 +52,11 @@ def fetchAttachments(self):
json = response.json()

for att in json['value']:
# try:
self.attachments.append(Attachment(att))
# log.debug('successfully downloaded attachment for: %s.',self.auth[0])
# except Exception as e:
# log.info('failed to download attachment for: %s', self.auth[0])
try:
self.attachments.append(Attachment(att))
log.debug('successfully downloaded attachment for: %s.',self.auth[0])
except Exception as e:
log.info('failed to download attachment for: %s', self.auth[0])

return len(self.attachments)

Expand Down Expand Up @@ -79,10 +84,10 @@ def sendMessage(self):
def markAsRead(self):
read = '{"IsRead":true}'
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
# try:
response = requests.patch(self.update_url.format(self.json['Id']),read,headers=headers,auth=self.auth)
# except:
# return False
try:
response = requests.patch(self.update_url.format(self.json['Id']),read,headers=headers,auth=self.auth)
except:
return False
print response
return True

Expand Down
15 changes: 14 additions & 1 deletion O365/schedule.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 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.

from O365 import Calendar
import logging
import json
Expand All @@ -23,8 +36,8 @@ def getCalendars(self):

for calendar in response.json()['value']:
try:
log.debug('appended calendar: %s',calendar['Name'])
self.calendars.append(Calendar(calendar,self.auth))
log.debug('appended calendar: %s',calendar['Name'])
except Exception as e:
log.info('failed to append calendar: %',str(e))

Expand Down
12 changes: 0 additions & 12 deletions examples/setup.py

This file was deleted.

0 comments on commit 2842225

Please sign in to comment.