Last active
August 29, 2015 14:16
-
-
Save Equinox-/14b167c19d7fd28f5900 to your computer and use it in GitHub Desktop.
habit-rpg-productivity.sh
This file contains 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
YOUR_API_TOKEN=token | |
YOUR_USER_ID=id |
This file contains 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
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