Skip to content

Commit

Permalink
Completely remove dependence on todoist-python
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixellion committed Dec 7, 2022
1 parent d2698a4 commit b766f12
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
42 changes: 15 additions & 27 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
# TODO: Remove dependence on todoist python api, it's broken and not needed

import os
import yaml
import json
import todoist
from todoist_api import TodoistAPI
import string
import tempfile
import sys
# easywebdav python3 hack
import easywebdav.client
from datetime import date, datetime, timedelta, timezone
import pytz
import pprint
from datetime import date, datetime, timedelta

easywebdav.basestring = str
easywebdav.client.basestring = str
Expand All @@ -29,20 +24,19 @@
config = yaml.load(f.read())

# Fetch todoist items
api = todoist.TodoistAPI(config['todoist_token'])
api.sync()
api = TodoistAPI(config['todoist_token'])

labels = {}
for label in api.state['labels']:
for label in api.get_items("labels"):
labels[label['id']] = label['name']

projects = {}
for project in api.state['projects']:
for project in api.get_items("projects"):
projects[project['id']] = project['name']

if config['debug']:
with open(os.path.join(appdir, "debug.json"), "w+", encoding="utf-8") as f:
f.write(pprint.pformat(api.state, indent=4))
# if config['debug']:
# with open(os.path.join(appdir, "debug.json"), "w+", encoding="utf-8") as f:
# f.write(pprint.pformat(api.state, indent=4))

def debug(text):
if config['debug']:
Expand Down Expand Up @@ -106,13 +100,9 @@ def completed_today():
def get_project_id(name):
project_id = None
# Find required project
for key, value in api.state.items():
if key == 'projects':
# print (value)
for project in value:
if project['name'] == name:
project_id = project['id']
break
for project in api.get_items("projects"):
if project['name'] == name:
project_id = project['id']
break
return project_id

Expand All @@ -121,7 +111,7 @@ def get_project_items(project_name):
# Filter completed out
project_id = get_project_id(project_name)
items = []
for item in api.state['items']:
for item in api.get_items():
# print(item)
select = False
if project_name == "Today":
Expand Down Expand Up @@ -157,9 +147,7 @@ def get_project_items(project_name):
remember_task(item['content'])
if config['clean_up_completed_tasks']:
print(f"Deleting task '{item['content']}'")
task = api.items.get_by_id(item['id'])
task.delete()
api.commit()
api.delete_item(item)
elif item['checked'] != 1 and item['checked'] != 0:
print("Something's not right, did Todoist change API? item['checked'] is not 0 or 1:")
return items
Expand All @@ -181,10 +169,10 @@ def generate_output_text():
output_text = f'TODAY (Done: {completed_today()}):\n\n{output_text}\n\nINBOX:\n\n{inbox_text}'
return output_text

def get_archival_text(api):
def get_archival_text(api : TodoistAPI):
tasks = []

for item in api.state['items']:
for item in api.get_items():
tasks.append(todoist_item_to_txt(item))

archival_text = ''
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PyYaml
todoist-python
easywebdav
icalendar
requests
54 changes: 54 additions & 0 deletions todoist_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
import yaml
import requests
from uuid import uuid4

appdir = os.path.dirname(os.path.realpath(__file__)) # rp, realpath


class TodoistAPI():
def __init__(self, token) -> None:
self.headers = {
"Authorization": f"Bearer {token}",
"content-type": "application/json",
}

self._state_cache = {}

def get_items(self, items_type="items", force_update=False):
if items_type not in self._state_cache or force_update:
items = requests.post("https://api.todoist.com/sync/v9/sync", headers=self.headers, json={
"sync_token": "*",
"resource_types": [items_type]
}
).json()[items_type]
self._state_cache[items_type] = items
else:
items = self._state_cache[items_type]
return items

def delete_item(self, item):
response = requests.post("https://api.todoist.com/sync/v9/sync", headers=self.headers, json={
"commands": [
{
"type": "item_delete",
"uuid": str(uuid4),
"args": {"id": item['id'] if isinstance(item, dict) else item}
}
]
}
)
return response

def add_item(self, item_data):
task = requests.post("https://api.todoist.com/sync/v9/items/add", headers=self.headers, json=item_data
).text
return task


# def add_task(self,)
if __name__ == "__main__":
# Testing
with open(os.path.join(appdir, 'config.yaml'), 'r') as f:
config = yaml.load(f.read())
print(TodoistAPI(config['todoist_token']).get_items("projects"))

0 comments on commit b766f12

Please sign in to comment.