Skip to content

Instantly share code, notes, and snippets.

@gilbertbw
Created June 11, 2013 22:54
Show Gist options
  • Save gilbertbw/5761489 to your computer and use it in GitHub Desktop.
Save gilbertbw/5761489 to your computer and use it in GitHub Desktop.
Prints to console list of all your asana tasks for a workspace/org, ordered by due date then creation date. Cashes tasks for offline use. for use for GEEKTOOL
import urllib2, base64, io, json
user = {}
# your api key
user['key'] = ""
# your workspace id, can be found from https://app.asana.com/api/1.0/workspaces when logged into webapp!
user['workspace'] = ""
# Absolute path for cashing file
bkup_loaction = "/users/username/tasks.json"
def fetch_data(user, type, taskid):
# Builds url depending on type!
url = "https://app.asana.com/api/1.0/"
if type == 'tasks':
url = url+ "tasks?workspace=" + user['workspace'] + "&assignee=me"
elif type == 'task':
url = url+ "tasks/" + str(taskid)
# Builds basic request using url
request = urllib2.Request(url)
# Takes the users key and encodes into base64
user['64key'] = base64.encodestring(user['key']+":")
# adds the basic authentication header to the request
request.add_header("Authorization", "Basic %s" % user['64key'])
try:
# Requests tasks using url and header, saves string into responce variable
responce = urllib2.urlopen(request).read()
except:
return None
# convert string into dict
# return ast.literal_eval(responce)
false = False
true = True
null = None
return eval(responce)
# tasktest = fetch_data(user, 'task', **{'taskid':'5560174413588'})
# print(tasktest)
# Uses fetch_data to get list of users tasks
tasks = fetch_data(user, 'tasks', None)
# if tasks:
# print(tasks)
# json.dump(tasks, open('tasks.json', 'w'))
# else:
# print("fetching")
# tasks = json.load(open('tasks.json'))
# lists for use in the following for loop
done_tasks = []
undone_tasks = []
undone_tasks_nodd = []
# For each task in the array of tasks find full_task and add to appropriot list
try:
for task in tasks["data"]:
# Find the due date on uncompleated tasks
full_task = fetch_data(user, 'task',**{"taskid":task["id"]})["data"]
# print(full_task)
if full_task["completed"] == True:
done_tasks.append(full_task)
if full_task["completed"] == False:
if full_task["due_on"] == None:
undone_tasks_nodd.append(full_task)
else:
undone_tasks.append(full_task)
# sort un_done tasks by due date
undone_tasks_nodd.sort(key = lambda task: task["created_at"])
undone_tasks.sort(key= lambda task: task["due_on"])
# Produce bkup json var
bkup = {}
bkup['done_tasks'] = done_tasks
bkup['undone_tasks'] = undone_tasks
bkup['undone_tasks_nodd'] = undone_tasks_nodd
# Save bkup json var
json.dump(bkup, open(bkup_location, 'w'))
except:
bkup = json.load(open(bkup_location))
done_tasks = bkup['done_tasks']
undone_tasks =bkup['undone_tasks']
undone_tasks_nodd = bkup['undone_tasks_nodd']
print("To-do:")
for task in undone_tasks:
print(task["name"])
for task in undone_tasks_nodd:
print(task["name"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment