-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_calendar.py
62 lines (49 loc) · 1.79 KB
/
google_calendar.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
import os
import pickle
from datetime import datetime, timedelta
from google.auth.transport.requests import Request
from google.oauth2.service_account import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/calendar.events"]
def dt_to_rfc3339(_dt: datetime) -> str:
return _dt.strftime("%Y-%m-%dT%H:%M:%S+09:00")
def add(
title: str,
body: str,
begin_date: datetime,
end_date: datetime,
cal_id: str | None = os.environ.get("GOOGLE_CALENDAR_ID"),
) -> str:
assert cal_id is not None, "Fail to get GOOGLE_CALENDAR_ID"
assert len(cal_id) > 10, "Invalid cal_id"
credentials = Credentials.from_service_account_file('service-account.json')
scoped_credentials = credentials.with_scopes(SCOPES)
try:
service = build("calendar", "v3", credentials=scoped_credentials)
event = {
"summary": title,
"description": body,
"start": {
"dateTime": dt_to_rfc3339(begin_date),
"timeZone": "Asia/Tokyo", # 1985-04-12T23:20:50.52Z
},
"end": {
"dateTime": dt_to_rfc3339(end_date),
"timeZone": "Asia/Tokyo", # 1985-04-12T23:20:50.52Z
},
}
result = service.events().insert(calendarId=cal_id, body=event).execute()
return result.get("htmlLink")
except HttpError as err:
return str(err)
if __name__ == "__main__":
a = add(
title="Test schdule",
body="Test schedule body",
begin_date=datetime.now(),
end_date=datetime.now() + timedelta(days=1),
)
print(a)