-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
134 lines (99 loc) · 4.21 KB
/
driver.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
from __future__ import print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/tasks']
def main():
calendar_id = input("Enter calendar ID of Google Calendar you would like to move to Tasks: ")
creds = None
if os.path.exists('tokenCal.pickle'):
with open('tokenCal.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentialsCal.json', SCOPES[0])
creds = flow.run_local_server(port=0)
with open('tokenCal.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting all future events from this calendar...')
events_result = service.events().list(calendarId=calendar_id, timeMin=now,
singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
# prints out calendar events
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
if len(events) == 0:
print("There are no calendar events to move to Tasks")
else:
cal_to_tasks(events)
def cal_to_tasks(events):
creds = None
if os.path.exists('tokenTask.pickle'):
with open('tokenTask.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentialsTask.json', SCOPES[1])
creds = flow.run_local_server(port=0)
with open('tokenTask.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('tasks', 'v1', credentials=creds)
results = service.tasklists().list(maxResults=10).execute()
task_lists = results.get('items', [])
task_list_index = 0
if (len(task_lists) > 1):
for i, task_list in enumerate(task_lists):
print(str(i) + ": " + task_list['title'])
task_list_index = int(input("Enter the number next to the tasklist you'd like to copy your events to: "))
# input error checking
while (task_list_index >= len(task_lists) or task_list_index < 0):
task_list_index = int(input("Enter the number next to the tasklist you'd like to copy your events to: "))
prev_copied = []
copied_over = ""
count = 0
if os.path.exists('prev_copied.txt'):
with open('prev_copied.txt') as file:
prev_copied = [line.strip() for line in file]
# take data from prev_copied and see if any of the events is in
# the list, if it is, skip over it. store id's in the text file
for event in events:
if event['id'] in prev_copied:
continue
title = event['summary']
description = ""
if "description" in event:
description = event['description']
due = event['start'].get('dateTime')
if due == None:
due = event['start']['date'] + "T09:00:00-05:00"
request_body = {
'title': title,
'notes': description,
'due': due,
'deleted': False,
'status': 'needsAction'
}
service.tasks().insert(
tasklist=task_lists[task_list_index]['id'],
body=request_body
).execute()
copied_over += event['id'] + "\n"
count += 1
with open("prev_copied.txt", "a") as file:
file.write(copied_over)
print("Created " + str(count) + " new tasks")
if __name__ == '__main__':
main()