pasholnahuy

Untitled

Nov 10th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. from todoist_api_python.api import TodoistAPI
  2. import json
  3. from datetime import date
  4. import datetime
  5.  
  6. CLIENT_ID = '01fd238f1dca45c0b6a2ffdb3ff9d601'
  7. CLIENT_SECRET = '51376de7dfe946418eff3c149f11cc54'
  8. TEST_TOKEN = '38847922db0efe60a38e91fe73ec190f19f2ecfb'
  9.  
  10. api = TodoistAPI(TEST_TOKEN)
  11.  
  12.  
  13. def get_project_id(project_name):
  14.     projects = api.get_projects()
  15.     myprojects = []
  16.     for project in projects:
  17.         if (project.name == project_name):
  18.             myprojects.append(project.id)
  19.     if len(myprojects) <= 0:
  20.         #Not Found
  21.         return None
  22.     if len(myprojects) > 1:
  23.         #too many objects
  24.         return None
  25.     return myprojects[0]
  26.  
  27. def delete_project(name):
  28.     id = get_project_id(name)
  29.     try:
  30.         s = api.delete_project(project_id=id)
  31.     except Exception as error:
  32.         return error
  33.  
  34. delete_project('Test 2')
  35. # получение существующих проектов
  36. # можно вернуть с ссылкой на проект
  37. def get_projects_names(url=False)->list:
  38.     try:
  39.         projects = api.get_projects()
  40.         if (url):
  41.             projects_names = [[project.name, project.url] for project in projects]
  42.         else:
  43.             projects_names = [project.name for project in projects]
  44.         #print(projects_names)
  45.         return projects_names
  46.     except Exception as error:
  47.         return error
  48.  
  49.  
  50. #добавить проект
  51. #можно сделать дочерний проект и выбрать view_style
  52. # view_style - два варианта - list или on_board
  53. #можно сделать дочерний проект и выбрать view_style
  54. # view_style - два варианта - list или on_board
  55. def add_project(name, parent_name=None, view_style='list', color= "charcoal"):
  56.     try:
  57.         res = get_projects_names()
  58.         if len(res) >= 7:
  59.             return 'Переполнение. Купите тариф Про.'
  60.         parent_id = get_project_id(parent_name)
  61.         project = api.add_project(name=name, parent_id=parent_id, view_style=view_style, color=color)
  62.         return project.id
  63.     except Exception as error:
  64.         return(error)
  65.  
  66.  
  67.  
  68. def rename_project(old_name, new_name):
  69.     try:
  70.         project_id = get_project_id(old_name)
  71.         project = api.update_project(project_id=project_id, name=new_name)
  72.         return project.url
  73.     except Exception as error:
  74.         return error
  75.  
  76. def style_project(name, color="charcoal", favourite=False, view_style='list'):
  77.     try:
  78.         project_id = get_project_id(name)
  79.         project = api.update_project(project_id=project_id, name=name, color=color, favourite=favourite, view_style=view_style)
  80.         return project.url
  81.     except Exception as error:
  82.         return error
  83.  
  84. def get_tasks(project_name=None):
  85.     project_id = get_project_id(project_name)
  86.     tasks = api.get_tasks(project_id=project_id)
  87.     return tasks
  88.  
  89. def tasks_today(project_name=None):
  90.     project_id = get_project_id(project_name)
  91.     tasks = api.get_tasks(project_id=project_id)
  92.     current_date = date.today()
  93.     today_tasks = []
  94.     print(current_date)
  95.     for task in tasks:
  96.         res = task.due
  97.         if (res != None):
  98.             if (task.due.date == str(current_date)):
  99.                 today_tasks.append(task)
  100.     return today_tasks
  101. tasks_today()
  102.  
  103. def get_task_id(content, project_name):
  104.     project_id = get_project_id(project_name)
  105.     tasks = api.get_tasks(project_id=project_id)
  106.     res = []
  107.     for task in tasks:
  108.         if task.content == content:
  109.             res.append(task.id)
  110.     if len(res) == 1:
  111.         return res[0]
  112.     else:
  113.         return None
  114.  
  115.  
  116. def close_task(content, project_name):
  117.     task_id = get_task_id(content, project_name)
  118.     try:
  119.         is_success = api.close_task(task_id=task_id)
  120.     except Exception as error:
  121.         return error
  122. #print(close_task('num2', 'Test 1'))
  123.  
  124. #priority 1 to 4(urgent)
  125. #due - when
  126. # description: "",
  127. #     due: {
  128. #         date: "2016-09-01",
  129. #         is_recurring: false,
  130. #         datetime: "2016-09-01T12:00:00.000000Z",
  131. #         string: "tomorrow at 12",
  132. #         timezone: "Europe/Moscow"
  133. #     },
  134.  
  135. def add_task(content, project_name, due_date=None, desription=None, priority=1):
  136.     try:
  137.         project_id = get_project_id(project_name)
  138.         task = api.add_task(content=content, project_id=project_id, due_date=due_date, desription=desription, priority=1)
  139.         return task
  140.     except Exception as error:
  141.         return error
  142.  
  143. def update_task(old_content, project_name, due_date=None, desription=None, priority=1, new_content=None):
  144.     try:
  145.         project_id = get_project_id(project_name)
  146.         print(1)
  147.         task_id = get_task_id(old_content, project_name)
  148.         print(task_id)
  149.         if (new_content != None):
  150.             task = api.update_task(task_id=task_id, content=new_content, project_id=project_id, due_date=due_date, desription=desription, priority=1)
  151.         else:
  152.             task = api.update_task(task_id=task_id, content=old_content, project_id=project_id, due_date=due_date, desription=desription, priority=1)
  153.         print(task)
  154.     except Exception as error:
  155.         return error
  156.  
  157. #add_task('num88', 'Test 1',  '2023-10-11', 'be free', 3)
  158.  
  159. #задания - сдавать сегодня
  160.  
Add Comment
Please, Sign In to add comment