-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas_todoist.py
executable file
·92 lines (67 loc) · 2.42 KB
/
canvas_todoist.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
#!/usr/bin/python3
import todoist
from canvasapi import Canvas
from datetime import datetime
from dateutil import parser
from dateutil import tz
import time
def getCanvasToken():
canvas_token = open("Tokens/canvas.txt", "r")
return canvas_token.read().strip("\n")
def getTodoistToken():
canvas_token = open("Tokens/todoist.txt", "r")
return canvas_token.read().strip("\n")
def getCanvasEvents():
API_URL = "https://canvas.vt.edu"
canvas = Canvas(API_URL, getCanvasToken())
events = canvas.get_upcoming_events()
events_dict = {}
for item in events:
if 'assignment' in item:
date = parser.parse(item['assignment']['due_at'])
# formated_date = "{}-{}-{} {}:{}".format(date.year,
# date.month, date.day, date.hour, date.minute)
formated_date = "{}-{}-{} {}:{}:{}".format(
date.year, date.month, date.day, date.hour, date.minute, date.second)
formated_date = convertToUTC(date)
# formated_date = time.strftime("%Y-%m-%dT%H:%M:%SZ", date)
title = item['title']
if formated_date in events_dict:
events_dict[formated_date].append(title)
else:
events_dict[formated_date] = [title]
return events_dict
def convertToUTC(date):
# METHOD 2: Auto-detect zones:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
# Tell the datetime object that it's in UTC time zone since
# datetime objects are 'naive' by default
temp_date = date.replace(tzinfo=from_zone)
# Convert time zone
central = temp_date.astimezone(to_zone)
return central
def addToTodoist(assignments):
api = todoist.TodoistAPI(getTodoistToken())
api.sync()
items = api.state["items"]
for key, value in assignments.items():
keep_going = True
for i in value:
for item in items:
if "in_history" in item:
if item["content"] == i and item["in_history"] == 0 and item["date"] == key:
keep_going = False
break
else:
continue
if keep_going:
print("Added")
print(item["content"])
due = {"date": key}
api.items.add(i, due=due)
api.commit()
def main():
addToTodoist(getCanvasEvents())
if __name__ == '__main__':
main()