Skip to content

Instantly share code, notes, and snippets.

@Equinox-
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Equinox-/14b167c19d7fd28f5900 to your computer and use it in GitHub Desktop.
Save Equinox-/14b167c19d7fd28f5900 to your computer and use it in GitHub Desktop.
habit-rpg-productivity.sh
YOUR_API_TOKEN=token
YOUR_USER_ID=id
eval $(cat ~/.habitrc)
PRODUCTIVE=(".*eclipse.*" ".*urxvt.*" ".*xterm.*" ".*make.*" ".*gcc.*" ".*libreoffice.*" ".*sublime_text")
NOPRODUCT=()
PREV="null"
PREV_START=$(date +"%s")
declare -A timetable
function contains() {
local e
for e in "${@:2}"; do [[ "$e" =~ "$1" ]] && return 0; done
return 1
}
function getTime() {
result=${timetable["$1"]}
if [ -z "$result" ]; then result="0"; fi
return 0
}
function setTime() {
timetable["$1"]=$2
return 0
}
function doLog() {
getTime "$1"
setTime "$1" "$(($result + $2))"
getTime "$1"
echo "$1 now has: $result"
local change=""
if contains "$1" "${PRODUCTIVE[@]}"; then
change="up"
elif contains "$1" "${NOPRODUCT[@]}"; then
change="down"
fi
if [ "$result" -gt 900 -a -n "$change" ]; then
echo "Log $change for $1"
curl -X POST -H "x-api-key: $YOUR_API_TOKEN" -H "x-api-user: $YOUR_USER_ID"
https://habitrpg.com/api/v2/user/tasks/productivity/$change
setTime "$1" "0"
fi
}
if [ $(curl -s -o /dev/null -w "%{http_code}" -X GET -H "x-api-key: $YOUR_API_TOKEN" -H "x-api-user: $YOUR_USER_ID" https://habitrpg.com/api/v2/user) -ne 200 ]; then
echo "Invalid setup"
exit 1
fi
while :
do
ACTIVE=$(ps -ho args -p `xdotool getactivewindow getwindowpid 2>/dev/null` 2>/dev/null)
if [ -n "$ACTIVE" ]; then
ACTIVE=$(echo "$ACTIVE" | tr -cd "[[:alnum:]]")
if [ ! "$PREV" == "$ACTIVE" ]; then
CTIME=$(date +"%s")
DELTA=$(($CTIME-$PREV_START))
doLog $PREV $DELTA
PREV="$ACTIVE"
PREV_START=$CTIME
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment