-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodoist.py
More file actions
201 lines (181 loc) · 8.59 KB
/
todoist.py
File metadata and controls
201 lines (181 loc) · 8.59 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3 # This is the default python shebang.
#!/Users/ranger/.pyenv/shims/python3 # <-- This is my shebang actual path. Check with "which python" in terminal, then replace.
# Created by Ranger
import os
import webbrowser
import sys
import platform
from todoist_api_python.api import TodoistAPI
from dotenv import load_dotenv
from colorama import init, Fore, Style
init(autoreset=True) # Initialize colorama for colored terminal text
# Load environment variables
load_dotenv() # Load the .env file
api_key = os.getenv("TODOIST_API_KEY") # Get the API key
if not api_key:
print(f"{Fore.RED}Error: TODOIST_API_KEY not found in .env file. Please set it.")
sys.exit(1)
api = TodoistAPI(api_key) # Use the API key
def get_todoist_tasks(api):
"""Fetches and prints Todoist tasks."""
try:
tasks = api.get_tasks()
print(f"{Fore.CYAN}Your Todoist tasks:")
for task in tasks:
print(f"{Style.BRIGHT}- {task.content}")
except Exception as e:
print(f"{Fore.RED}Error fetching tasks: {e}")
sys.exit(1)
def detect_os():
current_os = platform.system()
if current_os == "Windows":
print(f"{Fore.GREEN}Detected OS: Windows")
# Windows-specific code here (if needed)
elif current_os == "Darwin": # Darwin is the system name for macOS
print(f"{Fore.GREEN}Detected OS: macOS")
# macOS-specific code here (if needed)
elif current_os == "Linux":
print(f"{Fore.GREEN}Detected OS: Linux")
# Linux-specific code here (if needed)
else:
print(f"{Fore.YELLOW}Unsupported OS: {current_os}")
sys.exit(1)
def print_projects(projects):
for project in projects:
print(f"{Fore.CYAN}Project ID: {project.id}, Name: {project.name}")
def print_tasks(tasks):
for task in tasks:
print(f"{Fore.YELLOW}Task ID: {task.id}, Content: {task.content}")
def get_user_input(prompt):
try:
return input(prompt)
except KeyboardInterrupt:
print(f"\n{Fore.RED}User interrupted with Ctrl+C. Exiting back to main menu.")
return "back"
def print_banner():
print(f"\n{Fore.GREEN}{'='*30}")
print("Welcome to the Todoist CLI!")
print(f"{Fore.GREEN}{'='*30}\n")
def print_goodbye():
message = f"Thank you for using the {Fore.YELLOW}Todoist CLI{Fore.RESET}. {Fore.GREEN}Goodbye!"
separator = "=" * len(message) # calculates here.
print(f"\n{Fore.GREEN}{separator}")
print(message)
print(f"{Fore.GREEN}{separator}\n")
def main():
print_banner()
detect_os()
print("\n")
get_todoist_tasks(api)
print("\n")
while True:
print(f"\n{Fore.GREEN}{'='*30}")
print(f"{Fore.CYAN} 1. {Fore.RESET}List projects")
print(f"{Fore.CYAN} 2. {Fore.RESET}Add project")
print(f"{Fore.CYAN} 3. {Fore.RESET}Delete project")
print(f"{Fore.CYAN} 4. {Fore.RESET}List tasks")
print(f"{Fore.CYAN} 5. {Fore.RESET}Add task")
print(f"{Fore.CYAN} 6. {Fore.RESET}Update task")
print(f"{Fore.CYAN} 7. {Fore.RESET}Close task")
print(f"{Fore.CYAN} 8. {Fore.RESET}Reopen task")
print(f"{Fore.CYAN} 9. {Fore.RESET}Delete task")
print(f"{Fore.CYAN}10. {Fore.RESET}List comments")
print(f"{Fore.CYAN}11. {Fore.RESET}Add comment")
print(f"{Fore.CYAN}12. {Fore.RESET}Update comment")
print(f"{Fore.CYAN}13. {Fore.RESET}Delete comment")
print(f"{Fore.CYAN}14. {Fore.RESET}View today's tasks online")
print(f"{Fore.CYAN}15. {Fore.RESET}View projects online")
print(f"{Fore.CYAN} 0. {Fore.RESET}Exit")
print(f"{Fore.GREEN}{'='*30}\n")
choice = get_user_input("Choose an option (or type 'back' to return to menu): ")
if choice.lower() == 'exit':
print_goodbye()
sys.exit(0)
elif choice.lower() == 'back':
continue
if choice == "1":
projects = api.get_projects()
print_projects(projects)
elif choice == "2":
project_name = get_user_input("Enter project name (or 'back' to return): ")
if project_name.lower() != 'back':
project = api.add_project(name=project_name)
print(f"{Fore.GREEN}Added project {project.name} with ID {project.id}")
elif choice == "3":
project_id = get_user_input("Enter project ID to delete (or 'back' to return): ")
if project_id.lower() != 'back':
is_success = api.delete_project(project_id=project_id)
print(f"Deleted project {project_id}: {is_success}")
elif choice == "4":
tasks = api.get_tasks()
print_tasks(tasks)
elif choice == "5":
content = get_user_input("Enter task content (or 'back' to return): ")
if content.lower() != 'back':
due_string = get_user_input("Enter due date (or 'back' to return): ")
if due_string.lower() != 'back':
priority = get_user_input("Enter priority (1-4) (or 'back' to return): ")
if priority.lower() != 'back':
task = api.add_task(content=content, due_string=due_string, due_lang="en", priority=int(priority))
print(f"{Fore.GREEN}Added task {task.content} with ID {task.id}")
elif choice in ["6", "7", "8", "9"]:
tasks = api.get_tasks()
print_tasks(tasks)
task_id = get_user_input("Enter task ID (or 'back' to return): ")
if task_id.lower() != 'back':
if choice == "6":
content = get_user_input("Enter new content (or 'back' to return): ")
if content.lower() != 'back':
is_success = api.update_task(task_id=task_id, content=content)
print(f"Updated task {task_id}: {is_success}")
elif choice == "7":
is_success = api.close_task(task_id=task_id)
print(f"Closed task {task_id}: {is_success}")
elif choice == "8":
is_success = api.reopen_task(task_id=task_id)
print(f"Reopened task {task_id}: {is_success}")
elif choice == "9":
is_success = api.delete_task(task_id=task_id)
print(f"Deleted task {task_id}: {is_success}")
elif choice in ["10", "11", "12", "13"]:
tasks = api.get_tasks()
print_tasks(tasks)
task_id = get_user_input("Enter task ID (or 'back' to return): ")
if task_id.lower() != 'back':
if choice == "10":
comments = api.get_comments(task_id=task_id)
if comments:
for comment in comments:
print(f"{Fore.BLUE}Comment ID: {comment.id}, Content: {comment.content}")
else:
print("No comments found for this task.")
elif choice == "11":
content = get_user_input("Enter comment content (or 'back' to return): ")
if content.lower() != 'back':
comment = api.add_comment(content=content, task_id=task_id)
print(f"Added comment {comment.content} with ID {comment.id} to task {task_id}")
elif choice == "12":
comment_id = get_user_input("Enter comment ID to update (or 'back' to return): ")
if comment_id.lower() != 'back':
content = get_user_input("Enter new content (or 'back' to return): ")
if content.lower() != 'back':
is_success = api.update_comment(comment_id=comment_id, content=content)
print(f"Updated comment {comment_id}: {is_success}")
elif choice == "13":
comment_id = get_user_input("Enter comment ID to delete (or 'back' to return): ")
if comment_id.lower() != 'back':
is_success = api.delete_comment(comment_id=comment_id)
print(f"Deleted comment {comment_id}: {is_success}")
elif choice == "14":
webbrowser.open("https://app.todoist.com/app/today")
elif choice == "15":
webbrowser.open("https://app.todoist.com/app/projects/active")
elif choice == "0":
print_goodbye()
break
else:
print(f"{Fore.RED}Invalid choice. Please choose a number from 1 to 16 or type 'back'/'exit'.")
if __name__ == "__main__":
main()
get_todoist_tasks(api)
print_goodbye()