This example shows how to create a timesheet from time tracking data generated by the command line tool Timewarrior and upload it to Google Sheets.
notebook.ipynb explains the code step-by-step and allows you to interact and modify the dataflow.upload_timesheet.py contains the same dataflow but as a script. It can be used as a Timewarrior extension directly, see below.Create a virtual environment and activate it
python -m venv venv && . venv/bin/active
Install requirements
pip install -r requirements.txt
Adding a Timewarrior extension means you can trigger arbitrary scripts with timew report SCRIPT_FILE_NAME. Even better, you can skip the report command and only type the SCRIPT_FILE_NAME partially if it's unambiguous. For example, you can trigger upload_timesheet.py with timew upload. See the (notebook)[./notebook.ipynb] for further context and explanations.
To enable this, follow these steps:
Identify your Timewarrior installation directory (we'll assume the default: ~/.timewarrior)
Copy upload_timesheet.py from this example to ~/.timewarrior/extensions
Create a credentials.json with credentials for the Google Sheets API (see step-by-step guide) under ~/.timewarrior/extensions. It should look like this:
credentials = { "type": "service_account", "project_id": "...", "private_key_id": "...", "private_key": "-----BEGIN PRIVATE KEY----- ...", "client_email": "... .iam.gserviceaccount.com", "client_id": "...", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ ...", "universe_domain": "googleapis.com" }
Enable execution permissions with chmod +x upload_timesheet.py
Verify the extension is enabled with timew extensions. You should see
Extensions located in: ~/.timewarrior/extensions Extension Status credentials.json No executable upload_timesheet.py Active
Create a new Google Sheets spreadsheet and get its spreadsheet_id from the Google Sheets URL https://docs.google.com/spreadsheets/d/$SPREADSHEET_ID/edit?gid=0#gid=0
Use the Share button to give access to the Google API service account that you created at Step 3
Add the spreadsheet_id as a Timewarrior config point via timew config spreadsheet_id $SPREADSHEET_ID
Use the command timew report upload_timesheet.py to test it out! You should be able to shorten it to timew upload
[!NOTE] If you run
python upload_timesheet.pywill hang because it will try to read values fromsys.stdinwhich should be provided by the Timewarrior CLI. That's why you need to run it viatimew report upload_timesheet.py