Skip to content

Instantly share code, notes, and snippets.

@textarcana
Last active August 1, 2017 13:54
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 textarcana/fa198d40741f9bbcc6f180b2ec8077ca to your computer and use it in GitHub Desktop.
Save textarcana/fa198d40741f9bbcc6f180b2ec8077ca to your computer and use it in GitHub Desktop.
Script that tells you how long you spent online (according to RescueTime) since this morning.
#!/usr/bin/env bash
# Tells you how long you spent online (according to RescueTime) since
# this morning.
#
# Requires GNU date! Will not work with Mac OS / BSD date command!!!
# Rescuetime API Token
RT_KEY="YOUR API TOKEN GOES HERE"
outfile=$(mktemp "/tmp/$(basename $0).XXXXXXXX.json")
start_time=$(date -d today +%Y-%m-%d)
# Format ANY date in ANY format, consistently. Output will have the
# same form as produced by `date --iso-8601=seconds`
#
# Sample output: 2016-04-21T12:52:24-0400
#
# TODO: Does not handle fortnights.
date_of () {
result=$(echo "$@" | perl -lwne \
'
use Date::Manip;
use Date::Manip::Date;
my $stamp=ParseDateString("$_");
$date = new Date::Manip::Date($stamp);
print $date->printf("%O%z") . "\n";
')
if [ -z $result ]
then
echo "Couldn't parse: $@" \
| perl -ne 'print STDERR'
exit 1
fi
echo $result
}
# Given a list of integer durations in seconds, converts each number to a humanized time duration
humanize_duration () {
perl -wlne '
BEGIN{
use Time::Duration;
};
next if m{^(#.*|\s*)$};
print duration($_);
' $@
}
# Given a date string, tell me how long ago it was, in words.
# Eg: 7 hours ago.
humanize_how_long_ago () {
# Given two time stamps:
current_date=$(date -u +%s)
historical_date=$(date --date $(date_of $@) +%s)
# Subtract the two timestamps to get a duration in seconds.
duration_in_seconds=$((${current_date} - ${historical_date}))
how_long=$(
echo $duration_in_seconds \
| humanize_duration)
echo "${how_long}"
}
curl -s -o ${outfile} \
"https://www.rescuetime.com/anapi/data?key=${RT_KEY}&perspective=interval&restrict_kind=overview&interval=day&restrict_begin=${start_time}&format=json"
seconds_online=$(jq \
'
.rows |
map(select(.[3] |
test("Miscellaneous|Uncategorized|Shopping|Social Networking") |
not)) |
map(.[1]) |
add' \
${outfile})
first_active_hour=$(jq --raw-output '.rows | .[0][0]' ${outfile})
echo $(echo $seconds_online | humanize_duration) spent working since $(date -d $first_active_hour +%H):00 today.
echo $(humanize_how_long_ago $first_active_hour) since work began.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment