Created
January 5, 2022 22:36
-
-
Save josephsdavid/a934787726c3471800399e92875d1ccc to your computer and use it in GitHub Desktop.
clickup backend WIP
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
from clickupython import client | |
import datetime | |
import itertools as it | |
import logging | |
from rich.logging import RichHandler | |
FORMAT = "%(message)s" | |
logging.basicConfig( | |
level="INFO", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()] | |
) | |
logger = logging.getLogger("rich") | |
class Backend(object): | |
def __init__(self, key, name): | |
self.API_KEY = key | |
self.username = name | |
self.teams = self._teams() | |
self.spaces = self._spaces() | |
self.folders = self._folders() | |
self.lists = self._lists() | |
self.tasks = self._tasks() | |
self.my_tasks = self.find_your_tasks() | |
def client(self): | |
return client.ClickUpClient(self.API_KEY) | |
def _teams(self): | |
logger.info("Getting teams!") | |
return self.client().get_teams().teams | |
def _spaces(self): | |
logger.info("Getting workspaces!") | |
cli = self.client() | |
out = [] | |
for t in self.teams: | |
try: | |
out.append(cli.get_spaces(t.id).spaces) | |
except: | |
pass | |
return list(it.chain(*out)) | |
def _folders(self): | |
logger.info("Getting folders!") | |
cli = self.client() | |
return list(it.chain(*[cli.get_folders(s.id).folders for s in self.spaces])) | |
def _lists(self): | |
logger.info("Getting lists!") | |
cli = self.client() | |
return list(it.chain(*[cli.get_lists(f.id).lists for f in self.folders])) | |
def _tasks(self, **kwargs): | |
logger.info("Getting tasks!") | |
cli = self.client() | |
return list(it.chain(*[cli.get_tasks(l.id, **kwargs).tasks for l in self.lists])) | |
def find_your_tasks(self): | |
out = [] | |
for t in self.tasks: | |
if len(t.assignees) == 0: | |
continue | |
ids = [a.username for a in t.assignees] | |
if self.username in ids: | |
out.append(t) | |
return out | |
def humanize_time(timestamp): | |
try: | |
return datetime.datetime.fromtimestamp(int(timestamp)/1000) | |
except: | |
return datetime.datetime.utcnow() | |
def task_to_norg(cli, task): | |
start = humanize_time(task.start_date) | |
due = humanize_time(task.due_date) | |
lists = [l for l in cli.lists if l.id == task.list.id][0].name | |
folders = [f for f in cli.folders if f.id == task.folder.id][0].name | |
contexts = " ".join([lists, folders]) | |
status = task.status.status | |
priority = task.priority | |
result = [] | |
result.append(f" #time.due {due.strftime('%Y-%m-%d')}") | |
result.append(f" #time.start {start.strftime('%Y-%m-%d')}") | |
result.append(f" #contexts {contexts}") | |
result.append(f" #folder {task.folder.id}") | |
result.append(f" #list {task.list.id}") | |
result.append | |
marker_dict = { | |
"completed": " - [x]", | |
"open":" - [ ]", | |
"in progress":" - [-]", | |
"blocked": " - [=]", | |
"rejected":" - [_]", | |
"accepted":" - [-]", | |
"in review":" - [?]"} | |
todo = marker_dict[status] + " " + task.name | |
result.append(todo) | |
if task.text_content is not None: | |
info = " " + "\n ".join(task.text_content.splitlines()) | |
result.append(info) | |
return result | |
def norg_tasks(cli, tasks): | |
out = ["* Click Up Tasks"] | |
for i, task in enumerate(tasks): | |
logger.info("parsing task {}".format(i)) | |
out.extend(task_to_norg(cli, task)) | |
out.append("\n") | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment