Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 89b2bcc288f2190a492c7484c53436e112b5ace8 0 parents
@drewcrawford authored
Showing with 106 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +105 −0 invoice.py
1  .gitignore
@@ -0,0 +1 @@
+timesheet.csv
105 invoice.py
@@ -0,0 +1,105 @@
+#This script exports odesk time records to a CSV. In a way that I can nicely copy and paste into invoices.
+DISTANT_PAST = '2010-05-01'
+
+try:
+ import odesk
+except:
+ raise Exception("You need python-odesk. Maybe pip install python-odesk?")
+
+import getpass
+import os
+from odesk.routers.timereport import TimeReport
+FN = os.path.expanduser("~/.odesk-invoice-settings")
+import json
+try:
+ f = open(FN)
+ data = json.load(f)
+
+ api_key = data["api-key"]
+ api_secret = data["api-secret"]
+ username = data["username"]
+except:
+ print "Don't have an odesk API key? https://www.odesk.com/services/api/keys"
+ data = {}
+ api_key = raw_input("api key ")
+ api_secret = getpass.getpass("api secret ")
+ username = raw_input("username ")
+ data["api-key"]=api_key
+ data["api-secret"]=api_secret
+ data["username"]=username
+ f = open(FN,"wb")
+ json.dump(data,f)
+ f.close()
+ print "Saved config to ~/.odesk-invoice-settings"
+client = odesk.Client(api_key,api_secret)
+url = "client.auth.get()"
+url = client.auth.auth_url()
+os.system("open '%s'" % url)
+print url
+print "After you authorize the application (or in the case that you've done this before, on the page that says 'You are not authorized to access this page', there will be a ?frob=XXXX parameter."
+print "?frob="
+frob = raw_input()
+
+
+auth_token,user = client.auth.get_token(frob)
+client = odesk.Client(api_key,api_secret,auth_token)
+print "authenticated user",auth_token
+from datetime import date
+timereport = TimeReport(client)
+FIELDS = ['memo','worked_on','hours']
+data= timereport.get_provider_report(username,
+ odesk.Query(select=FIELDS + ['worked_on'],
+ where=(odesk.Q('worked_on') <= date.today()) &\
+ (odesk.Q('worked_on') > DISTANT_PAST)))
+
+#this comes back in the world's most god-awful format
+table = data["table"]
+cols = map(lambda x: x["label"],table["cols"])
+rows = table["rows"]
+
+not_sucky_format = []
+for row in rows:
+ row = row['c']
+ obj = {}
+ print "processing row",row
+
+ for colname in cols:
+ print "processing col",colname
+ obj[colname] = row[cols.index(colname)]['v']
+ not_sucky_format.append(obj)
+print not_sucky_format
+
+
+
+#collect the unique dates
+unique_dates = []
+for row in not_sucky_format:
+ print "processing row %s" % row
+ date = row['worked_on']
+ if date not in unique_dates:
+ unique_dates.append(date)
+unique_dates.sort()
+
+#export to csv
+print_cols = ['memo','hours'] #as opposed to fetched columns
+import csv
+import datetime
+writer = csv.writer(open('timesheet.csv',"wb"))
+writer.writerow(print_cols)
+for date in unique_dates:
+ pretty_date = datetime.datetime.strptime(date,"%Y%m%d").strftime("%m/%d/%y")
+ writer.writerow([pretty_date])
+ for row in not_sucky_format:
+ if row["worked_on"]!= date: continue
+ l = []
+ for col in print_cols:
+ l.append(row[col])
+ print "writing row",l
+ writer.writerow(map(lambda x: x.encode('utf-8'),l))
+ writer.writerow([" "])
+
+
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.