Last active
April 16, 2021 19:59
-
-
Save kwk/3c1d04a36f14bf26f2eb0cbc9005f81d to your computer and use it in GitHub Desktop.
Display amount of time spent in activity tracked by timeular for the current day
This file contains hidden or 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
#!/bin/bash | |
set -e | |
# Go here to create or refresh an API key and API secret and paste them: | |
# https://profile.timeular.com/#/app/account | |
# Put this in your ~/.timeular.config | |
# API_KEY="..." | |
# API_SECRET="..." | |
source ~/.timeular.config | |
# NO MODIFIATION REQUIRED BELOW | |
ACCEPT_HEADER="accept: application/json;charset=UTF-8" | |
BASE_URL=https://api.timeular.com/api/v2 | |
TODAY=$(date +'%Y-%m-%d') | |
API_TOKEN=$(curl -s -X POST "${BASE_URL}/developer/sign-in" -H ${ACCEPT_HEADER} -H "content-type: application/json" -d "{ \"apiKey\": \"${API_KEY}\", \"apiSecret\": \"${API_SECRET}\"}" | jq -j .token) | |
AUTHZ_HEADER="Authorization: Bearer ${API_TOKEN}" | |
ACTIVITIES=$(curl -s -X GET "${BASE_URL}/activities" -H "${ACCEPT_HEADER}" -H "${AUTHZ_HEADER}") | |
ENTRIES=$(curl -s -X GET "${BASE_URL}/time-entries/${TODAY}T00:00:00.000/${TODAY}T23:59:59.000" -H "${ACCEPT_HEADER}" -H "${AUTHZ_HEADER}") | |
# Calculate used activity IDs (this avoids division by zero further down the line) | |
ACTIVITY_IDS=$(echo $ENTRIES | jq -r '[.timeEntries[].activity.id] | unique | .[]') | |
# For each activity of the day, display the amount of hours spent in it | |
RESULTS=$(for ACTIVITY_ID in ${ACTIVITY_IDS}; do | |
# Print activity name | |
echo $ACTIVITIES | jq -j --arg activity_id $ACTIVITY_ID '.activities[] | select(.id==$activity_id) | .name' | |
echo -n ":" | |
# Print amount of hourse spent in current activity | |
echo $ENTRIES | jq --arg activity_id $ACTIVITY_ID ' | |
[ | |
.timeEntries[] | |
| select(.activity.id==$activity_id) | |
| .duration | |
| .diff = (.stoppedAt[:-4]+"Z" | fromdate) - (.startedAt[:-4]+"Z" | fromdate) | |
| .diff | |
] | add | . / 3600 ' \ | |
| awk '{printf "%.2f\n", $1}' | |
done) | |
echo -e "Activity:Hours\n--------:-----\n$RESULTS" | column -t -s':' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should print something similar to this, depending on what your activities are: