Guest User

Untitled

a guest
Jan 11th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. from todoist_api_python.api import TodoistAPI
  2.  
  3. from dotenv import load_dotenv
  4. import os
  5. import logging
  6.  
  7. # Doc SDK: https://developer.todoist.com/rest/v1/?python#python-sdk
  8.  
  9. load_dotenv()
  10.  
  11. logging.basicConfig(filename='/var/log/todoist-shopping.log', level=logging.INFO, format='%(asctime)s -  %(levelname)s -  %(message)s')
  12. logging.info('====================')
  13. logging.info('Inicio del proceso')
  14.  
  15. # Fetch tasks synchronously
  16.  
  17. source_project = os.getenv('SOURCE_PROJECT_ID')
  18. dest_project = os.getenv('DEST_PROJECT_ID')
  19. query=os.getenv('QUERY')
  20. api = TodoistAPI(os.getenv('API_TOKEN'))
  21. keywords=os.getenv('KEYWORD').split(',')
  22. logging.debug(keywords)
  23.  
  24. # Get candidate tasks
  25. try:
  26.     tasks = api.get_tasks(project_id=source_project,filter=query)
  27.     logging.debug(tasks)
  28. except Exception as error:
  29.     logging.error(error)
  30.  
  31. for task in tasks:
  32.  
  33.     logging.debug(task)
  34.  
  35.     for keyword in keywords:
  36.  
  37.         # If the task has any keyword
  38.         if (task.content.lower().startswith(keyword.lower())):
  39.             logging.info("Task `" + task.content + "` matches with keyword " + keyword)
  40.  
  41.             # Copy the task to the desired destination, minus the keyword
  42.             # (Todoist API doesn't allow to move a task)
  43.             logging.info("Copying the task")
  44.             try:
  45.                 new_task = api.add_task(
  46.                     content=task.content.lower().replace(keyword.lower(),''),
  47.                     due_lang='es',
  48.                     project_id=dest_project
  49.                 )
  50.                 logging.info(new_task)
  51.             except Exception as error:
  52.                 logging.error(error)
  53.      
  54.             # Before deleting the original task, remove date so it won't show in Google Calendar
  55.             try:
  56.                 logging.info("Updating original task")
  57.                 is_success_update = api.update_task(task_id=task.id, due_string='no due date')
  58.                 logging.info(is_success_update)
  59.             except Exception as error:
  60.                 logging.error(error)
  61.            
  62.             # Delete the original task
  63.             try:
  64.                 logging.info("Deleting original task")
  65.                 is_success_delete = api.delete_task(task_id=task.id)
  66.                 logging.info(is_success_delete)
  67.             except Exception as error:
  68.                 logging.error(error)
  69.  
  70. logging.info('Final del proceso')
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment