-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbee
More file actions
executable file
·91 lines (80 loc) · 2.11 KB
/
Copy pathbee
File metadata and controls
executable file
·91 lines (80 loc) · 2.11 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /bin/bash
base='https://www.beeminder.com/api/v1/'
# user=''
# key=''
goal=$1
date=$2
data=$3
comment=$4
# check whether global variable $data is a number and exit if not
function check_data() {
if [[ ! $data =~ ^-?[0-9]+(\.[0-9]+)?$ ]]
then
echo "data must be a number" >&2
exit 3
fi
}
# generate unix time $timestamp from $date
# if date command cannot parse $date, exit
function process_date() {
# get current date
let day=$(date +%-d)
let month=$(date +%-m)
if [[ $date == "^" ]]
then
# today
timestamp=$(date -d "${month}/${day}" +%s)
else
if [[ $date -gt $day ]]
then
# last month
let month-=1
fi
timestamp=$(date -d "${month}/${date}" +%s)
if [[ $? != 0 ]]
then
# invalid date
exit 2
fi
fi
}
# check if goal exists and exit if not
function check_goal() {
goals=$(curl -s "${base}users/${user}.json?auth_token=$key" |sed 's/^.*"goals":\[\(.*\)\].*$/\1/')
if [[ ! $goals =~ \"${goal}\" ]]
then
echo "Goal '$goal' not found" >&2
exit 1
fi
}
function submit() {
# submit data and display status parameter from returned datapoint object
curl -sd "timestamp=${timestamp}&value=${data}&comment=${comment}&auth_token=${key}" "${base}users/${user}/goals/${goal}/datapoints.json" |sed 's/^.*\"status\":\"\([^"]*\)\".*$/\1\n/'
# this is for debugging
# echo "timestamp=${timestamp}&value=${data}&comment=${comment}&auth_token=${key}" "${base}users/${user}/goals/${goal}/datapoints.json" |sed 's/^.*\"status\":\"\([^"]*\)\".*$/\1\n/'
}
function goal_status() {
# extract and display 'headsum' parameter from returned json Goal object
# see https://www.beeminder.com/api#goal
curl -s "${base}users/${user}/goals/${goal}.json?auth_token=${key}" |sed 's/^.*\"headsum\":\"\([^"]*\)\".*$/\1\n/'
}
case $# in
3|4 )
# submit data point
check_data
process_date
check_goal
submit
exit 0
;;
1 )
# get goal status
check_goal
goal_status
;;
* )
# wrong number of arguments
echo "Usage: $(basename $0) <goal> <day> <datum> [comment]" >&2
exit 4
;;
esac