Skip to content

Instantly share code, notes, and snippets.

@LozanoMatheus
Created September 18, 2019 08:02
Show Gist options
  • Save LozanoMatheus/581c7277c2456f753533dd68cd659383 to your computer and use it in GitHub Desktop.
Save LozanoMatheus/581c7277c2456f753533dd68cd659383 to your computer and use it in GitHub Desktop.
Bash script to post recurrent time entries on Toogl (e.g.: dailies)
#!/usr/bin/env bash
declare -r MONTH="${1:-"$(date +'%m')"}"
declare -r MY_TOGGL_TOKEN="${MY_TOGGL_TOKEN?"Variable MY_TOGGL_TOKEN not declared"}"
declare -r UTC_NEW="$(TZ=UTC date +'%I')"
declare -ri MY_TIME=$(date +'%H')
declare -ri TZ_DIFF="$(( ${MY_TIME/0} - ${UTC_NEW/0} ))"
## How-to get the Project ID (wid): https://github.com/toggl/toggl_api_docs/blob/master/chapters/projects.md
declare -r PROJECT_ID="${PROJECT_ID?"Variable PROJECT_ID not declared"}"
## How-to get the Workspace ID (wid): https://github.com/toggl/toggl_api_docs/blob/master/chapters/workspaces.md
declare -r WORKSPACE_ID="${WORKSPACE_ID?"Variable WORKSPACE_ID not declared"}"
## Must to be in seconds
declare -r ENTRY_DURATION="${ENTRY_DURATION?"Variable ENTRY_DURATION not declared"}"
## Diff between user timezone and UTC
[[ "${TZ_DIFF}" == \-* ]] && declare -r TOGGLE_TZ="-0${TZ_DIFF//-/}" || declare -r TOGGLE_TZ="+0${TZ_DIFF}"
## It will POST the entries with days between 9 and 13, 16 and 20
##
for i in {9..13} {16..20} ; do
[[ "${#i}" -eq 1 ]] && declare DAY="0${i}" || declare DAY="${i}"
curl -su "${MY_TOGGL_TOKEN}:api_token" \
-H "Content-Type: application/json" \
-d "{
\"time_entry\": {
\"description\": \"Daily Team X\",
\"billable\": false,
\"duration\": ${ENTRY_DURATION},
\"start\": \"2019-${MONTH}-${DAY}T10:00:00${TOGGLE_TZ}:00\",
\"pid\": ${PROJECT_ID},
\"wid\": ${ENTRY_DURATION},
\"created_with\": \"curl\"
}
}" -X POST https://www.toggl.com/api/v8/time_entries | jq
sleep 1s
curl -su "${MY_TOGGL_TOKEN}:api_token" \
-H "Content-Type: application/json" \
-d "{
\"time_entry\": {
\"description\": \"Daily Team Y\",
\"billable\": false,
\"duration\": ${ENTRY_DURATION},
\"start\": \"2019-${MONTH}-${DAY}T09:30:00${TOGGLE_TZ}:00\",
\"pid\": ${PROJECT_ID},
\"wid\": ${ENTRY_DURATION},
\"created_with\": \"curl\"
}
}" -X POST https://www.toggl.com/api/v8/time_entries | jq
sleep 1s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment