Created
December 21, 2024 02:51
-
-
Save Shinichi-Ohki/94f525db36704c45f0823bc11009f015 to your computer and use it in GitHub Desktop.
Todoistの「買い物リスト」プロジェクトにタスクを追加するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.path.append('/opt/homebrew/lib/python3.9/site-packages') | |
# todoist_api_pythonがPython 3.9までしか動作確認してないからこれ以上新しいのには入らないっぼい | |
import datetime | |
from todoist_api_python.api import TodoistAPI | |
# 今日の日付を文字列に変換(そのままだとJSONに入らないので) | |
str_today = str(datetime.date.today()) | |
# Todoist APIトークン | |
API_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
# 引数が足りない場合のエラーメッセージ | |
if len(sys.argv) < 2: | |
print("Usage: python script.py <タスク名>") | |
sys.exit(1) | |
task_content = sys.argv[1] | |
# TodoistAPI オブジェクト生成 | |
api = TodoistAPI(API_TOKEN) | |
# プロジェクト一覧を取得 | |
try: | |
projects = api.get_projects() | |
except Exception as error: | |
print(error) | |
sys.exit(1) | |
# プロジェクト一覧から特定のプロジェクト名を探して ID を取得 | |
project_ID_Buy = None | |
for project in projects: | |
if project.name == "買い物リスト": | |
project_ID_Buy = project.id | |
break | |
# 万が一、対象プロジェクトが見つからなかった場合のエラーハンドリング | |
if not project_ID_Buy: | |
print("指定のプロジェクト '買い物リスト' が見つかりませんでした。") | |
sys.exit(1) | |
# タスクを追加 | |
try: | |
task = api.add_task( | |
content=task_content, | |
project_id=project_ID_Buy, | |
priority = 4, # 優先度は最高が4、最低が1 | |
due_date = str_today # 今日の日付 | |
) | |
print(task) | |
except Exception as error: | |
print(error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment