-
Notifications
You must be signed in to change notification settings - Fork 27
/
rescuetime.bash
77 lines (63 loc) · 1.99 KB
/
rescuetime.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
RESCUETIME_ENDPOINT="https://www.rescuetime.com/anapi/data?key=${RESCUETIME_API_KEY}&format=csv&rs=day&rk=productivity"
segments::rescuetime_fetch_changes() {
result=$(curl -s "$RESCUETIME_ENDPOINT" | grep -v '^Rank')
exit_code=$?
if [[ $exit_code -gt 0 ]]; then
debug::log "Could not reach rescuetime RESCUETIME_ENDPOINT"
return 0
fi
echo "$result"
}
segments::rescuetime_refresh() {
refresh_rate="${SEGMENTS_RESCUETIME_REFRESH_RATE:-600}"
if [[ ! -f $SEGMENT_CACHE ]]; then
debug::log "No cache folder"
fi
if [[ -f $SEGMENT_CACHE ]]; then
last_update=$(stat -f "%m" "$SEGMENT_CACHE")
else
last_update=0
fi
local current_time
_sbp_get_current_time current_time
time_since_update=$((current_time - last_update))
if [[ $time_since_update -lt $refresh_rate ]]; then
return 0
fi
if [[ -z $RESCUETIME_API_KEY ]]; then
debug::log "RESCUETIME_API_KEY not set"
return 1
fi
result="$(segments::rescuetime_fetch_changes)"
if [[ -z $result ]]; then
# No data, so no logging of time today
command rm -f "$SEGMENT_CACHE"
return 0
fi
for line in $result; do
seconds=$(cut -d ',' -f 2 <<<"$line")
total_seconds=$((seconds + total_seconds))
value=$(cut -d ',' -f 4 <<<"$line")
productivity_value=$((value + 2))
score=$((seconds * productivity_value))
productive_score=$((score + productive_score))
done
max_score=$((total_seconds * 4))
pulse="$((productive_score * 100 / max_score))%"
hours=$((total_seconds / 60 / 60))
hour_seconds=$((hours * 60 * 60))
remaining_seconds=$((total_seconds - hour_seconds))
minutes=$((remaining_seconds / 60))
time="${hours}h:${minutes}m"
printf '%s;%s' "$pulse" "$time" >"$SEGMENT_CACHE"
}
segments::rescuetime() {
if [[ -f $SEGMENT_CACHE ]]; then
read -r cache <"$SEGMENT_CACHE"
pulse="${cache/;*/}"
time="${cache/*;/}"
print_themed_segment 'normal' "$pulse" "$time"
fi
execute::execute_nohup_function segments::rescuetime_refresh
}