You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.8KB

  1. from O365 import Account, MSOffice365Protocol, FileSystemTokenBackend, Connection
  2. import configparser
  3. from datetime import datetime, timedelta
  4. def read_config():
  5. """
  6. Reads config file and sets up variables foo
  7. :return: credentials: ('client_id', 'client_secret') and tenant: str
  8. """
  9. config = configparser.ConfigParser()
  10. config.read("./config")
  11. credentials = (config["client"]["id"], config["client"]["secret"])
  12. tenant = config["client"]["tenant"]
  13. return credentials, tenant
  14. def create_session():
  15. """
  16. Create a session with the API and save the token for later use.
  17. :return: Account class
  18. """
  19. credentials, tenant = read_config()
  20. my_protocol = MSOffice365Protocol(api_version='v2.0')
  21. token_backend = FileSystemTokenBackend(token_filename='access_token.txt')
  22. return Account(credentials, protocol=my_protocol, tenant_id=tenant,
  23. token_backend=token_backend) # the default protocol will be Microsoft Graph
  24. def authenticate_session(session: Account):
  25. """
  26. Authenticates account session object with oauth. Uses the default auth flow that comes with the library
  27. It could be merged with oauth wrapper but this works too.
  28. :param session: Account object
  29. :return:
  30. """
  31. try:
  32. session.con.oauth_request("https://outlook.office.com/api/v2.0/me/calendar", "get")
  33. except RuntimeError: # Not authenticated. Need to ask user for url
  34. session.authenticate(scopes=['basic', 'address_book', 'users', 'calendar_shared'])
  35. session.con.oauth_request("https://outlook.office.com/api/v2.0/me/calendar", "get")
  36. def oauth_request(connection: Connection, url: str):
  37. """
  38. Wrapper for Connection.oauth_request to provide some error handling
  39. :param connection:
  40. :param url:
  41. :return:
  42. """
  43. request = connection.oauth_request(url, "get")
  44. if request.status_code != 200:
  45. print(f"Request failed: GET request to {url} failed with {request.status_code} error")
  46. else:
  47. return request
  48. def get_week_datetime():
  49. """
  50. Gets the current week's Monday and Friday to be used to filter a calendar.
  51. If this script is ran during the week, it will be the current week. If it is ran on the weekend, it will generate for next week.
  52. :return: Monday and Friday: Datetime object
  53. """
  54. today = datetime.now()
  55. weekday = today.weekday()
  56. # If this script is run during the week
  57. if weekday <= 4: # 0 = Monday, 6 = Sunday
  58. monday = today - timedelta(days=weekday) # Monday = 0-0, Friday = 4-4
  59. friday = today + timedelta(days=4 - weekday) # Fri to Fri 4 + (4 - 4), Tues to Fri = 2 + (4 - 2)
  60. return monday, friday
  61. else:
  62. monday = today - timedelta(days=weekday) + timedelta(days=7) # Monday = 0-0, Friday = 4-4
  63. friday = today + timedelta(days=4 - weekday) + timedelta(
  64. days=7) # Fri to Fri 4 + (4 - 4), Tues to Fri = 2 + (4 - 2)
  65. return monday, friday
  66. def main():
  67. """
  68. Main function that is ran on start up. Script is ran from here.
  69. """
  70. session = create_session()
  71. authenticate_session(session)
  72. r = oauth_request(session.con, "https://outlook.office.com/api/v2.0/users/EMAIL/")
  73. # request = account.con.oauth_request("https://outlook.office.com/api/v2.0/users/EMAIL/calendar/events?$filter=Start/DateTime ge '2019-09-04T08:00' AND End/Datetime le '2019-09-05T08:00'&$top=50", "get")
  74. print(r.text)
  75. # setup csv
  76. # For loop over all emails
  77. # API request for user info of email
  78. # Get display name
  79. # API request for events this week
  80. # Find sign of being in or not being in
  81. # insert name in csv for the days they are in and at what seat (if regular)
  82. monday, friday = get_week_datetime()
  83. print(f"Monday is {monday}, Friday is {friday}")
  84. if __name__ == "__main__":
  85. main()