-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_calendar_event.py
140 lines (119 loc) · 4.36 KB
/
create_calendar_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# coding=utf-8
"""
Auto ording room
python create_calendar_event.py -u [email protected] -s mail.domain.com -l "6F-F16-天箭座-14人" -m [email protected] -b"技术之夜 03-21" -d "2018-03-21 18:00" -a "90"
"""
from __future__ import unicode_literals, print_function
import os
import argparse
import logging.config
import getpass
import socket
from datetime import timedelta
import requests
from exchangelib import DELEGATE, Account, Credentials, Configuration, CalendarItem, EWSDateTime, \
EWSTimeZone, Attendee, Mailbox
from exchangelib.errors import UnauthorizedError
from exchangelib.items import SEND_ONLY_TO_ALL
from requests import ReadTimeout
from urllib3.exceptions import ReadTimeoutError
from dateutil import parser
DEFAULT_SOCKET_TIMEOUT = 10
DSN_TOKEN = os.environ.get('EXCHANGE_ORDER_DSN')
ENV_PASSWORD = 'EXCHANGE_ORDER_PASSWORD'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'defaultFormatter': {
'format': '%(levelname)s %(asctime)s %(module)s:%(lineno)d %(message)s ',
'datefmt': '%m-%d %H:%M:%S',
}
},
'handlers': {
'defaultHandler': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'defaultFormatter',
'filename': 'exchange-order.log',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 5,
},
'sentryHandler': {
'level': 'ERROR',
'class': 'raven.handlers.logging.SentryHandler',
'dsn': DSN_TOKEN,
},
'consoleHandler': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'': {
'handlers': ['defaultHandler', 'sentryHandler'],
'level': 'INFO',
# 'level': 'DEBUG',
},
'console': {
'handlers': ['consoleHandler'],
'level': 'INFO',
# 'level': 'DEBUG',
'propagate': False,
},
},
}
logging.config.dictConfig(LOGGING)
logger = logging.getLogger(__name__)
console = logging.getLogger('console')
socket.setdefaulttimeout(DEFAULT_SOCKET_TIMEOUT)
tz = EWSTimeZone.timezone('Asia/Shanghai')
def create_event(account, subject, location, location_mail, date, duration):
start = parser.parse(date)
start_ews = tz.localize(EWSDateTime(start.year, start.month, start.day, start.hour,
start.minute))
end = parser.parse(date) + timedelta(seconds=duration * 60)
end_ews = tz.localize(EWSDateTime(end.year, end.month, end.day, end.hour, end.minute))
item = CalendarItem(
folder=account.calendar,
subject=subject,
location=location,
start=start_ews,
end=end_ews,
required_attendees=[
Attendee(mailbox=Mailbox(email_address=location_mail),
response_type='Accept')
]
)
item.save(send_meeting_invitations=SEND_ONLY_TO_ALL)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--username', '-u', required=True)
parser.add_argument('--server', '-s', required=True)
parser.add_argument('--location', '-l', required=True)
parser.add_argument('--location-mail', '-m', required=True)
parser.add_argument('--subject', '-b', required=True)
parser.add_argument('--date', '-d', required=True)
parser.add_argument('--duration', '-a', required=True, type=int)
args = parser.parse_args()
password = os.environ.get(ENV_PASSWORD)
if password is None:
password = getpass.getpass('EXCHANGE password:')
# auth
credentials = Credentials(username=args.username, password=password)
try:
config = Configuration(server=args.server, credentials=credentials)
except UnauthorizedError as e:
logger.error('Login failed, message: %s' % e)
return
# main logic
try:
account = Account(primary_smtp_address=args.username, config=config, autodiscover=False,
access_type=DELEGATE)
# main_logic TODO
except (ConnectionResetError, requests.exceptions.ConnectionError, TimeoutError,
ReadTimeoutError, ReadTimeout) as e:
logger.debug(e)
return
create_event(account=account, subject=args.subject, location=args.location,
location_mail=args.location_mail, date=args.date, duration=args.duration)
if __name__ == '__main__':
main()