-
-
Save klebercode/c65be188747520e5e6486d2b3127e65e to your computer and use it in GitHub Desktop.
A command line tool for Todoist
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
## Got this script initially from https://gist.github.com/kbl/5970131 | |
########### | |
# IMPORTS # | |
########### | |
import ConfigParser | |
import argparse | |
import json | |
import os | |
import urllib | |
import urllib2 | |
################## | |
# MODULE GLOBALS # | |
################## | |
TOKEN = None | |
FORMAT = "%(id)s %(project_formatted)s @%(labels_formatted)s %(priority)s %(content)s" | |
############# | |
# FUNCTIONS # | |
############# | |
def getRemote(endpoint, params=dict()): | |
## Check params | |
if not params or params == dict(): | |
params = "" | |
else: | |
params = urllib.urlencode(params) | |
## Construct the url: | |
URL = ("https://todoist.com/API/" | |
"%(endpoint)s?" | |
"token=%(token)s&" | |
"%(params)s") % dict(endpoint=endpoint, | |
token=TOKEN, | |
params=params) | |
## Get the resource and return: | |
return json.load(urllib2.urlopen(URL)) | |
def getRemoteProjects(): | |
return getRemote("getProjects") | |
def getRemoteUncompleteTasksByProject(project_id): | |
return getRemote("getUncompletedItems", params=dict(project_id=project_id)) | |
def actionListTasks(args, arb): | |
projects = getRemoteProjects() | |
if args.project != None: | |
projects = [p for p in projects | |
if args.project.lower() in p["name"].lower()] | |
for project in projects: | |
print "Project: %(name)s" % project | |
items = getRemoteUncompleteTasksByProject(project["id"]) | |
items = sorted(items, key=lambda x: x["priority"], reverse=True) | |
for item in items: | |
print " [%(id)s] [%(priority)s] %(content)s" % item | |
def actionAddTask(args, arb): | |
params = dict(content=" ".join(arb), priority=1) | |
if args.project: | |
projects = getRemoteProjects() | |
if args.project != None: | |
try: | |
project = [p for p in projects | |
if args.project.lower() in p["name"].lower()][0] | |
params["project_id"] = project["id"] | |
except IndexError: | |
sys.stderr.write("No such project found: %s" % (args.project,)) | |
sys.exit(1) | |
if args.date: | |
params["date_string"] = args.date | |
getRemote("addItem", params=params) | |
def actionAddProject(args, arb): | |
getRemote("addProject", params=dict(name=" ".join(arb))) | |
def actionToday(args, arb): | |
labels = getRemote("getLabels") | |
labels = dict([(labels[l]["id"], labels[l]) for l in labels]) | |
projects = getRemoteProjects() | |
projects = dict([(p["id"], p) for p in projects]) | |
items = getRemote("query", params=dict(queries='["overdue", "today"]')) | |
for item in items[0]["data"] + items[1]["data"]: | |
item["labels"] = [item["labels"][0]] if item["labels"] else [] | |
item["labels_formatted"] = "%-8s" % (", ".join([labels[i]["name"]for i in item["labels"]]))[0:8] | |
item["project_formatted"] = "%-5s" % projects[item["project_id"]]["name"][0:5] | |
print FORMAT % item | |
############# | |
# MAIN LOOP # | |
############# | |
if __name__ == "__main__": | |
## Read the configuration: | |
config = ConfigParser.RawConfigParser() | |
config.read(os.environ["HOME"] + "/.todoist") | |
## Get the API Token: | |
TOKEN = config.get("Global", "API_token") | |
## Create and configure argument parser: | |
parser = argparse.ArgumentParser(prog="todoist", description="Todoist") | |
subparsers = parser.add_subparsers(help="sub-command help") | |
parser_list_tasks = subparsers.add_parser("list-tasks", | |
help="list tasks") | |
parser_list_tasks.add_argument("-p", "--project", help="the project") | |
parser_list_tasks.set_defaults(function=actionListTasks) | |
parser_list_create = subparsers.add_parser("add-task", | |
help="add new task") | |
parser_list_create.add_argument("-d", "--date", help="the date") | |
parser_list_create.add_argument("-p", "--project", help="the project") | |
parser_list_create.set_defaults(function=actionAddTask) | |
parser_list_create = subparsers.add_parser("add-project", | |
help="add new project") | |
parser_list_create.set_defaults(function=actionAddProject) | |
parser_list_today = subparsers.add_parser("today", | |
help="list today") | |
parser_list_today.set_defaults(function=actionToday) | |
## Parse arguments and invoke the action: | |
args = parser.parse_known_args() | |
args[0].function(args[0], args[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment