Skip to content

Instantly share code, notes, and snippets.

@sergray
Created September 1, 2011 15:46
Show Gist options
  • Save sergray/1186457 to your computer and use it in GitHub Desktop.
Save sergray/1186457 to your computer and use it in GitHub Desktop.
Python console application which calculates hours spent on task given in argument
import sys
import odesk
from odesk import Q
from datetime import datetime, timedelta
import shelve
import os
# add your desktop app keys
public_key = ''
secret_key = ''
storage_path = os.path.join(os.environ['HOME'], '.task_hours.shelve')
storage = shelve.open(storage_path)
def get_client():
client = odesk.Client(public_key, secret_key)
auth_token = storage.get('auth_token')
if auth_token is None:
# perform auth process
frob = storage.get('frob')
if frob is None:
frob = client.auth.get_frob()
storage['frob'] = frob
print "Please, authorize application using the URL:\n%s" % \
client.auth.auth_url(frob)
sys.exit(1)
try:
auth_token, user = client.auth.get_token(frob)
except Exception, e:
print "Could not get auth token: %s" % e
else:
storage['auth_token'] = auth_token
storage['user'] = user
finally:
del storage['frob']
if auth_token is None:
sys.exit(1)
client = odesk.Client(public_key, secret_key, auth_token)
return client
if __name__=='__main__':
task = sys.argv[1]
try:
worked_from = sys.argv[2]
except IndexError:
# set week back default
worked_from = datetime.now().date() - timedelta(days=7)
client = get_client()
report = client.time_reports.get_provider_report(
'sergray',
odesk.Query(
select=odesk.Query.DEFAULT_TIMEREPORT_FIELDS,
where=(Q('task')==task) & (Q('worked_on') > worked_from)
)
)
storage.close()
print sum(map(lambda r: float(r['c'][-1]['v']), report['table']['rows']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment