def fetch_next(self, count=1): """ Fetch the next n messages after the previous fetch, where n is the specified count :param count: no.of messages to fetch """ skip_count = self.fetched_count if self._search: params = { '$filter': self._filter, '$top': count, '$search': '"{}"'.format(self._search) } else: params = { '$filter': self._filter, '$top': count, '$skip': skip_count } response = Connection.get_response(self.url, verify=self.verify, params=params) self.fetched_count += count messages = [] for message in response: messages.append(Message(message, Connection().auth)) return messages
def from_folder(self, folder_name): """ Configure to use this folder for fetching the mails :param folder_name: name of the outlook folder """ self._reset() response = Connection.get_response(FluentInbox._get_url('folders'), verify=self.verify, params={'$top': 100}) folder_id = None all_folders = [] for folder in response: if folder['displayName'] == folder_name: folder_id = folder['id'] break all_folders.append(folder['displayName']) if not folder_id: raise RuntimeError('Folder "{}" is not found, available folders ' 'are {}'.format(folder_name, all_folders)) self.url = FluentInbox._get_url('folder').format(folder_id=folder_id) return self
def list_folders(self, parent_id=None, user_id=None): """ :param parent_id: Id of parent folder to list. Default to top folder :return: List of all folder data """ if parent_id and user_id: folders_url = FluentInbox._get_url('user_child_folders').format( folder_id=parent_id, user_id=user_id) elif parent_id: folders_url = FluentInbox._get_url('child_folders').format( folder_id=parent_id) elif user_id: folders_url = FluentInbox._get_url('user_folders').format( user_id=user_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 getEvents(self, start=None, end=None, eventCount=10): ''' Pulls events in for this calendar. default range is today to a year now. Keyword Arguments: start -- The starting date from where you want to begin requesting events. The expected type is a struct_time. Default is today. end -- The ending date to where you want to end requesting events. The expected type is a struct_time. Default is a year from start. ''' # 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 end = time.gmtime(end) end = time.strftime(self.time_string, end) connection = Connection() # Change URL if we use Oauth if connection.is_valid() and connection.oauth != None: self.events_url = self.events_url.replace( "outlook.office365.com/api", "graph.microsoft.com") # This is where the actual call to Office365 happens. response = connection.get_response(self.events_url.format( self.json['Id'], start, end, eventCount), auth=self.auth, verify=self.verify) log.info('Response from O365: %s', str(response)) #This takes that response and then parses it into individual calendar events. for event in response: try: duplicate = False # checks to see if the event is a duplicate. if it is local changes are clobbered. for i, e in enumerate(self.events): if e.json['Id'] == event['id']: self.events[i] = Event(event, self.auth, self) duplicate = True break if not duplicate: 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)) log.debug('all events retrieved and put in to the list.') return True
def get_folder(self, value, by='Id', parent_id=None, user_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) :param user_id: user id the folder belongs to (shared mailboxes) :returns: Single folder data """ if parent_id and user_id: folders_url = FluentInbox._get_url('user_child_folders').format( folder_id=parent_id, user_id=user_id) elif parent_id: folders_url = FluentInbox._get_url('child_folders').format( folder_id=parent_id) elif user_id: folders_url = FluentInbox._get_url('user_folders').format( user_id=user_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 getCalendars(self): '''Begin the process of downloading calendar metadata.''' connection = Connection() # Change URL if we use Oauth if connection.is_valid() and connection.oauth != None: self.cal_url = self.cal_url.replace("outlook.office365.com/api", "graph.microsoft.com") log.debug('fetching calendars.') response = connection.get_response(self.cal_url, auth=self.auth, verify=self.verify) log.info('response from O365 for retriving message attachments: %s', str(response)) for calendar in response: try: duplicate = False log.debug('Got a calendar with name: {0} and id: {1}'.format( calendar['Name'], calendar['Id'])) for i, c in enumerate(self.calendars): if c.json['id'] == calendar['Id']: c.json = calendar c.name = calendar['Name'] c.calendarid = calendar['Id'] duplicate = True log.debug('Calendar: {0} is a duplicate', calendar['Name']) break if not duplicate: self.calendars.append(Calendar(calendar, self.auth)) log.debug('appended calendar: %s', calendar['Name']) log.debug('Finished with calendar {0} moving on.'.format( calendar['Name'])) except Exception as e: log.info('failed to append calendar: {0}'.format(str(e))) log.debug('all calendars retrieved and put in to the list.') return True