-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalAdd.py
49 lines (42 loc) · 1.53 KB
/
calAdd.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
from __future__ import print_function
import datetime
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# If modifying these scopes, delete the file token.json.
# SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
SCOPES = 'https://www.googleapis.com/auth/calendar.events'
def calAdd(posts):
# Load credentials.json
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('calendar', 'v3', http=creds.authorize(Http()))
# Call the Calendar API
for p in posts:
event = {
'summary': '[%s限] %s(%s, %s)' % (p['time'], p['course'], p['class'], p['teacher']),
'description': p['misc'],
'start': {
'date': p['day'],
'timeZone': 'Asia/Tokyo',
},
'end': {
'date': p['day'],
'timeZone': 'Asia/Tokyo',
},
}
CAL_ID = '[email protected]'
result = service.events().insert(calendarId=CAL_ID, body=event).execute()
print('Event created: %s' % (result.get('htmlLink')))
if __name__ == '__main__':
calAdd(posts = [{
'time': '2',
'course': 'SampleLecture',
'class': 'A',
'teacher': 'John Doe',
'misc': 'hello world',
'day': '2018-11-30'
}])