|
|
|
|
|
|
|
|
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from O365 import Account, MSOffice365Protocol, FileSystemTokenBackend, Connection |
|
|
from O365 import Account, MSOffice365Protocol, FileSystemTokenBackend, Connection |
|
|
import configparser |
|
|
import configparser |
|
|
from datetime import datetime, timedelta |
|
|
from datetime import datetime, timedelta |
|
|
|
|
|
import argparse |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(prog='seatingplan', description="Script to generate a seating plan via Office365 Calendars") |
|
|
|
|
|
parser.add_argument("-c", "--client-id", type=str, required=True, dest='client_id', |
|
|
|
|
|
help='Client ID for registered azure application') |
|
|
|
|
|
parser.add_argument("-s", "--client-secret", type=str, required=True, dest='client_secret', |
|
|
|
|
|
help='Client secret for registered azure application') |
|
|
|
|
|
parser.add_argument("-t", "--tenant-id", type=str, required=True, dest='tenant_id', |
|
|
|
|
|
help='Tenant ID for registered azure application') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_config(): |
|
|
def read_config(): |
|
|
|
|
|
|
|
|
Reads config file and sets up variables foo |
|
|
Reads config file and sets up variables foo |
|
|
:return: credentials: ('client_id', 'client_secret') and tenant: str |
|
|
:return: credentials: ('client_id', 'client_secret') and tenant: str |
|
|
""" |
|
|
""" |
|
|
|
|
|
# TODO: Deprecate this file so we use an arg version instead |
|
|
config = configparser.ConfigParser() |
|
|
config = configparser.ConfigParser() |
|
|
config.read("./config") |
|
|
config.read("./config") |
|
|
|
|
|
|
|
|
credentials = (config["client"]["id"], config["client"]["secret"]) |
|
|
credentials = (config["client"]["id"], config["client"]["secret"]) |
|
|
tenant = config["client"]["tenant"] |
|
|
tenant = config["client"]["tenant"] |
|
|
|
|
|
email = config["client"]["out_of_office_email"] |
|
|
|
|
|
|
|
|
return credentials, tenant |
|
|
|
|
|
|
|
|
return credentials, tenant, email |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_session(): |
|
|
def create_session(): |
|
|
""" |
|
|
""" |
|
|
Create a session with the API and save the token for later use. |
|
|
Create a session with the API and save the token for later use. |
|
|
:return: Account class |
|
|
|
|
|
|
|
|
:return: Account class and email: str |
|
|
""" |
|
|
""" |
|
|
credentials, tenant = read_config() |
|
|
|
|
|
my_protocol = MSOffice365Protocol(api_version='v2.0') |
|
|
|
|
|
|
|
|
config = parser.parse_args() |
|
|
|
|
|
credentials = (config.client_id, config.client_secret) |
|
|
|
|
|
tenant = config.tenant_id |
|
|
|
|
|
my_protocol = MSOffice365Protocol(api_version='v2.0') |
|
|
token_backend = FileSystemTokenBackend(token_filename='access_token.txt') |
|
|
token_backend = FileSystemTokenBackend(token_filename='access_token.txt') |
|
|
return Account(credentials, protocol=my_protocol, tenant_id=tenant, |
|
|
|
|
|
token_backend=token_backend) # the default protocol will be Microsoft Graph |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Account( |
|
|
|
|
|
credentials, |
|
|
|
|
|
protocol=my_protocol, |
|
|
|
|
|
tenant_id=tenant, |
|
|
|
|
|
token_backend=token_backend |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
def authenticate_session(session: Account): |
|
|
def authenticate_session(session: Account): |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
""" |
|
|
""" |
|
|
Gets the current week's Monday and Friday to be used to filter a calendar. |
|
|
Gets the current week's Monday and Friday to be used to filter a calendar. |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
If this script is ran during the work week (Monday-Friday), it will be the current week. If it is ran on the weekend, it will generate for next week. |
|
|
:return: Monday and Friday: Datetime object |
|
|
:return: Monday and Friday: Datetime object |
|
|
""" |
|
|
""" |
|
|
today = datetime.now() |
|
|
today = datetime.now() |
|
|
|
|
|
|
|
|
session = create_session() |
|
|
session = create_session() |
|
|
authenticate_session(session) |
|
|
authenticate_session(session) |
|
|
|
|
|
|
|
|
r = oauth_request(session.con, "https://outlook.office.com/api/v2.0/users/EMAIL/") |
|
|
|
|
|
|
|
|
r = session.con.oauth_request("https://outlook.office.com/api/v2.0/users/{email}/calendar/events", "get") |
|
|
|
|
|
|
|
|
# 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") |
|
|
# 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") |
|
|
print(r.text) |
|
|
print(r.text) |
|
|
|
|
|
|
|
|
# setup csv |
|
|
# setup csv |
|
|
# For loop over all emails |
|
|
|
|
|
# API request for user info of email |
|
|
|
|
|
# Get display name |
|
|
|
|
|
# API request for events this week |
|
|
|
|
|
# Find sign of being in or not being in |
|
|
|
|
|
# insert name in csv for the days they are in and at what seat (if regular) |
|
|
|
|
|
|
|
|
# API request events from out of office email |
|
|
|
|
|
# loop over events |
|
|
|
|
|
# find out who the event is talking about |
|
|
|
|
|
# insert into csv |
|
|
|
|
|
# output csv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
monday, friday = get_week_datetime() |
|
|
monday, friday = get_week_datetime() |
|
|
print(f"Monday is {monday}, Friday is {friday}") |
|
|
print(f"Monday is {monday}, Friday is {friday}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
main() |