|
|
- import pyperclip
- import mimetypes
- import os
- import urllib
-
- from adal import AuthenticationContext
-
- import requests
- import config
-
- from datetime import datetime
- from datetime import date
- from datetime import timedelta
- import pytz
-
- import oauth2ms
-
-
- def api_endpoint(url):
- """Convert a relative path such as /me/photo/$value to a full URI based
- on the current RESOURCE and API_VERSION settings in config.py.
- """
- if urllib.parse.urlparse(url).scheme in ['http', 'https']:
- return url # url is already complete
- return urllib.parse.urljoin(f'{config.RESOURCE}/v{config.API_VERSION}/',
- url.lstrip('/'))
-
-
- def parse_cal_date(dateStr):
- d = datetime.strptime(dateStr, "%Y-%m-%dT%H:%M:%S.0000000")
- exchangeTz = pytz.utc
- localTz = pytz.timezone('US/Central')
- return exchangeTz.localize(d).astimezone(localTz);
-
-
- def format_orgmode_date(dateObj):
- return dateObj.strftime("%Y-%m-%d %H:%M")
-
-
- def format_orgmode_time(dateObj):
- return dateObj.strftime("%H:%M")
-
-
- def get_calendar(token):
- """Get Calendar from O365
- """
- start = date.today() - timedelta(days=config.daysHistory)
- end = date.today() + timedelta(days=config.daysFuture)
- # print(api_endpoint("/"))
- cal = requests.get(
- api_endpoint(f'me/calendarview?startdatetime={start}T00:00:00.000Z&enddatetime={end}T23:59:59.999Z&$top={config.maxEntries}&$orderby=start/dateTime'),
- headers={'Authorization':f'Bearer {token}'}
- )
-
- calentries = cal.json()
-
- for appt in calentries['value']:
- apptstart = parse_cal_date(appt['start']['dateTime'])
- apptend = parse_cal_date(appt['end']['dateTime'])
- tags = ""
- if appt['categories']:
- tags = ":" + ":".join(appt['categories']) + ":"
- else:
- tags = ":WORK:"
-
- if apptstart.date() == apptend.date():
- dateStr = "<" + format_orgmode_date(apptstart) + "-" + format_orgmode_time(apptend) + ">"
- else:
- dateStr = "<" + format_orgmode_date(apptstart) + ">--<" + format_orgmode_date(apptend) + ">"
- body = appt['bodyPreview'].translate({ord('\r'): None})
-
- print(f'* {dateStr} {appt["subject"]} {tags}')
-
- print(":PROPERTIES:")
- if appt['location']['displayName'] is not None:
- print(":LOCATION: %s" % (appt['location']['displayName']))
-
- if appt['onlineMeeting'] is not None:
- print(f":JOINURL: {appt['onlineMeeting']['joinUrl']}")
-
- print(f":RESPONSE: {appt['responseStatus']['response']}")
- print(":END:")
-
- print(f"{body}")
-
- print("")
-
-
- if __name__ == '__main__':
- get_calendar(oauth2ms.token)
|