Skip to content

Commit

Permalink
Merge pull request O365#83 from drewstinnett/move_folders
Browse files Browse the repository at this point in the history
Adding in operations to do more with folders
  • Loading branch information
Narcolapser authored Feb 26, 2018
2 parents eb2de75 + 62f45d0 commit 44b9d14
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
67 changes: 67 additions & 0 deletions O365/fluent_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class FluentInbox(object):
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders/{folder_id}/messages',
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders/{folder_id}/messages',
},
'child_folders': {
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders/{folder_id}/childfolders',
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders/{folder_id}/childfolders',
},
}

def __init__(self, verify=True):
Expand Down Expand Up @@ -64,6 +68,69 @@ def from_folder(self, folder_name):

return self

def get_folder(self, value, by='Id', parent_id=None):
"""
Return a folder by a given attribute. If multiple folders exist by
this attribute, only the first will be returned
Example:
get_folder(by='DisplayName', value='Inbox')
or
get_folder(by='Id', value='AAKrWFG...')
Would both return the requested folder attributes
:param value: Value that we are searching for
:param by: Search on this key (default: Id)
:returns: Single folder data
"""
if parent_id:
folders_url = FluentInbox._get_url('child_folders').format(
folder_id=parent_id)
else:
folders_url = FluentInbox._get_url('folders')

response = Connection.get_response(folders_url,
verify=self.verify,
params={'$top': 100})

folder_id = None
all_folders = []

for folder in response:
if folder[by] == value:
return(folder)

all_folders.append(folder['displayName'])

if not folder_id:
raise RuntimeError(
'Folder "{}" is not found by "{}", available folders '
'are {}'.format(value, by, all_folders))

def list_folders(self, parent_id=None):
"""
:param parent_id: Id of parent folder to list. Default to top folder
:return: List of all folder data
"""
if parent_id:
folders_url = FluentInbox._get_url('child_folders').format(
folder_id=parent_id)
else:
folders_url = FluentInbox._get_url('folders')

response = Connection.get_response(folders_url,
verify=self.verify,
params={'$top': 100})

folders = []
for folder in response:
folders.append(folder)

return folders

def filter(self, filter_string):
""" Set the value of a filter. More information on what filters are available can be found here:
https://msdn.microsoft.com/office/office365/APi/complex-types-for-mail-contacts-calendar#RESTAPIResourcesMessage
Expand Down
20 changes: 20 additions & 0 deletions O365/fluent_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ def markAsRead(self):
return False
return True

def moveToFolder(self, folder_id):
"""
Move the message to a given folder
:param folder_id: Folder ID to move this message to
:returns: True on success
"""
move_url = 'https://outlook.office365.com/api/v1.0/me/messages/{0}/move'
headers = {'Content-type': 'application/json',
'Accept': 'application/json'}
post_data = {"DestinationId": folder_id}
try:
response = requests.post(move_url.format(self.json['Id']),
json=post_data, headers=headers,
auth=self.auth, verify=self.verify)
except:
return False

return True

def getSender(self):
'''get all available information for the sender of the email.'''
return self.json['Sender']
Expand Down
23 changes: 23 additions & 0 deletions examples/FolderOperations/get_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
import getpass
from O365 import Connection, FluentInbox


def main():
if len(sys.argv) == 1:
sys.stderr.write("Usage: %s BY VALUE\n" % sys.argv[0])
return 1

username = input("Username: ")
password = getpass.getpass("Password: ")
authentication = (username, password)
Connection.login(*authentication)
inbox = FluentInbox()

print(inbox.get_folder(by=sys.argv[1], value=sys.argv[2]))

return 0


if __name__ == "__main__":
sys.exit(main())
25 changes: 25 additions & 0 deletions examples/FolderOperations/list_folders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
import getpass
from O365 import Connection, FluentInbox


def main():
username = input("Username: ")
password = getpass.getpass("Password: ")
authentication = (username, password)
Connection.login(*authentication)
inbox = FluentInbox()

# If given arguments, treat them as folder_ids to use as parents
if len(sys.argv) > 1:
for folder_id in sys.argv[1:]:
for folder in inbox.list_folders(parent_id=folder_id):
print(folder['Id'], folder['DisplayName'])
else:
for folder in inbox.list_folders():
print(folder['Id'], folder['DisplayName'])
return 0


if __name__ == "__main__":
sys.exit(main())
26 changes: 26 additions & 0 deletions examples/FolderOperations/trash_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
import getpass
from O365 import Connection, FluentInbox


def main():
if len(sys.argv) == 1:
sys.stderr.write("Usage: %s 'subject to search for'\n" % sys.argv[0])
return 1

username = input("Username: ")
password = getpass.getpass("Password: ")
authentication = (username, password)
Connection.login(*authentication)
inbox = FluentInbox()

trash_folder = inbox.get_folder(by='DisplayName', value='Trash')

for message in inbox.search("Subject:%s" % sys.argv[1]).fetch(count=1):
print(message.moveToFolder(trash_folder['Id']))

return 0


if __name__ == "__main__":
sys.exit(main())

0 comments on commit 44b9d14

Please sign in to comment.