Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resurrect the project with the Sync API v9 #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,658 changes: 1,278 additions & 1,380 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plotly-calplot = "^0.1.7"
vulture = "^2.3"
oauthlib = "^3.2.0"
requests = "^2.27.1"
pyyaml = "6.0.1"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
Expand Down
24 changes: 9 additions & 15 deletions todoist_analytics/backend/data_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import numpy as np
import pandas as pd
import todoist

from todoist_analytics.frontend.colorscale import color_code_to_hex

from todoist_analytics.backend.todoist import TodoistAPI
from todoist_analytics.frontend.colorscale import color_name_to_hex

class DataCollector:
def __init__(self, token):
self.token = token
self.items = pd.DataFrame()
self.projects = pd.DataFrame()
self.api = todoist.TodoistAPI(self.token)
self.api = TodoistAPI(token)
self.api.sync()
self.current_offset = 0

Expand All @@ -23,7 +21,7 @@ def _collect_active_tasks(self):
pass

def _collect_completed_tasks(self, limit, offset):
data = self.api.completed.get_all(limit=limit, offset=offset)
data = self.api.get_all_completed(limit=limit, offset=offset)
if data == "Service Unavailable\n":
time.sleep(3)
data = self._collect_completed_tasks(limit, offset)
Expand Down Expand Up @@ -58,13 +56,8 @@ def _collect_all_completed_tasks(self, limit=10000):
self.current_offset = new_shape
stop_collecting = True

def _state_to_dataframe(self, state, key):
f = [d.data for d in state[str(key)]]
f = pd.DataFrame(f)
return f

def _collect_active_tasks(self):
self.active_tasks = self._state_to_dataframe(self.api.state, "items")
self.active_tasks = pd.DataFrame(self.api.state["items"])
keep_columns = [
"checked",
"content",
Expand All @@ -74,7 +67,7 @@ def _collect_active_tasks(self):
"labels",
"priority",
"project_id",
"date_added",
"added_at",
"id",
]
self.active_tasks = self.active_tasks[keep_columns]
Expand All @@ -85,7 +78,7 @@ def _preprocess_completed_tasks(self, completed_tasks, projects):
projects = projects.rename({"id": "project_id"}, axis=1)

completed_tasks["datehour_completed"] = pd.to_datetime(
completed_tasks["completed_date"]
completed_tasks["completed_at"]
)

self.get_user_timezone()
Expand Down Expand Up @@ -121,9 +114,10 @@ def _preprocess_completed_tasks(self, completed_tasks, projects):
)

completed_tasks["hex_color"] = completed_tasks["color"].apply(
lambda x: color_code_to_hex[int(x)]["hex"]
lambda x: color_name_to_hex[x]
)

completed_tasks.drop("notes", axis=1, inplace=True)
completed_tasks = completed_tasks.drop_duplicates().reset_index(drop=True)

return completed_tasks, projects
21 changes: 21 additions & 0 deletions todoist_analytics/backend/todoist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import requests

class TodoistAPI:
def __init__(self, token):
self.token = token
self.endpoint = "https://api.todoist.com/sync/v9"
self.session = requests.Session()
self.session.headers["Authorization"] = f"Bearer {self.token}"

def sync(self):
response = self.session.post(f"{self.endpoint}/sync", data={
"sync_token": "*",
"resource_types": '["all"]'
})
self.state = response.json()

def get_all_completed(self, limit, offset):
return self.session.get(
f"{self.endpoint}/completed/get_all",
params={ "limit": limit, "offset": offset }
).json()
2 changes: 0 additions & 2 deletions todoist_analytics/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from pandas.core.frame import DataFrame

from todoist_analytics.backend.data_collector import DataCollector
from todoist_analytics.frontend.colorscale import color_code_to_hex


def create_color_palette(completed_tasks: DataFrame):
project_id_color = pd.Series(
Expand Down
42 changes: 21 additions & 21 deletions todoist_analytics/frontend/colorscale.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
color_code_to_hex = {
30: {"color_name": "berry_red", "hex": "#b8256f"},
31: {"color_name": "red", "hex": "#db4035"},
32: {"color_name": "orange", "hex": "#ff9933"},
33: {"color_name": "yellow", "hex": "#fad000"},
34: {"color_name": "olive_green", "hex": "#afb83b"},
35: {"color_name": "lime_green", "hex": "#7ecc49"},
36: {"color_name": "green", "hex": "#299438"},
37: {"color_name": "mint_green", "hex": "#6accbc"},
38: {"color_name": "teal", "hex": "#158fad"},
39: {"color_name": "sky_blue", "hex": "#14aaf5"},
40: {"color_name": "light_blue", "hex": "#96c3eb"},
41: {"color_name": "blue", "hex": "#4073ff"},
42: {"color_name": "grape", "hex": "#884dff"},
43: {"color_name": "violet", "hex": "#af38eb"},
44: {"color_name": "lavender", "hex": "#eb96eb"},
45: {"color_name": "magenta", "hex": "#e05194"},
46: {"color_name": "salmon", "hex": "#ff8d85"},
47: {"color_name": "charcoal", "hex": "#808080"},
48: {"color_name": "grey", "hex": "#b8b8b8"},
49: {"color_name": "taupe", "hex": "#ccac93"},
color_name_to_hex = {
"berry_red": " #b8256f",
"light_blue": " #96c3eb",
"red": " #db4035",
"blue": " #4073ff",
"orange": " #ff9933",
"grape": " #884dff",
"yellow": " #fad000",
"violet": " #af38eb",
"olive_green": " #afb83b",
"lavender": " #eb96eb",
"lime_green": " #7ecc49",
"magenta": " #e05194",
"green": " #299438",
"salmon": " #ff8d85",
"mint_green": " #6accbc",
"charcoal": " #808080",
"teal": " #158fad",
"grey": " #b8b8b8",
"sky_blue": " #14aaf5",
"taupe": " #ccac93"
}