-
Notifications
You must be signed in to change notification settings - Fork 1
/
coffeestats-cli
executable file
·118 lines (106 loc) · 4.03 KB
/
coffeestats-cli
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
set -e
: ${HOME?"Required variable \$HOME is not set"}
conf=${HOME}/.coffeestatsrc
version="1.1.2"
useragent="coffeestats-cli/${version} $(curl --version | grep -Eo 'libcurl/[^ ]+')"
# find the path to the script itself, http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
usage() {
local scriptname=$(basename "$0")
echo "Usage: $scriptname ACTION" >&2
echo "ACTION may be one of the following:" >&2
echo " coffee" >&2
echo " Tell coffeestats.org that you just got yourself a nice hot cup of" >&2
echo " delicious coffee." >&2
echo " mate" >&2
echo " Tell coffeestats.org that you're replenishing your supply of" >&2
echo " caffeeine with a cold bottle of mate." >&2
echo "$scriptname is a command-line submission script for coffeestats.org" >&2
echo "by Clemens Lang. See https://github.com/neverpanic/coffeestats-cli" >&2
}
if [ ! -f "$conf" ]; then
# No config file, user seems to be using this for the first time
echo "Welcome to this coffeestats.org submission script. This seems to be"
echo "the first time you run this script. Let's start by configuring some"
echo "variables."
echo
echo "Please go to coffeestats.org, log in with your account, go to your"
echo "profile and click the \"on-the-run\" button. Copy the URL and paste it"
echo "here:"
# read the on-the-run URL
user=""
hash=""
while [ "$user" == "" ] || [ "$hash" == "" ]; do
echo
read -p "coffeestats.org on-the-run URL: " ontherunurl
# trim
ontherunurl=${ontherunurl## *}
ontherunurl=${ontherunurl%% *}
if [ "$ontherunurl" == "" ]; then
echo "The URL was empty, please try again." >&2
else
user=$(expr "${ontherunurl}" : '.*/ontherun/\([^/]*\)/' || true)
hash=$(expr "${ontherunurl}" : '.*/ontherun/[^/]*/\([^/]*\)' || true)
if [ "$user" == "" ]; then
echo "Couldn't extract user, please make sure you're using the correct URL and try again (or report a bug)." >&2
fi
if [ "$hash" == "" ]; then
echo "Couldn't extract hash, please make sure you're using the correct URL and try again (or report a bug)." >&2
fi
fi
done
echo
echo "Extracted"
echo " coffeestats.org username: $user"
echo " coffeestats.org on-the-run hash: $hash"
echo "$user" > "$conf"
echo "$hash" >> "$conf"
echo "$(basename "$0") is now ready to submit data to coffeestats.org."
echo
usage "$0"
exit 0
else
# no arguments?! show the usage info
if [ -z "$1" ]; then
usage
exit 1
fi
# parse arguments
beverage=""
time=$(date +'%F %H:%M:%S')
case "$1" in
"coffee" | "mate")
beverage="$1"
;;
*)
echo "Unknown action: $1" >&2
usage "$0"
exit 1
;;
esac
# read username and hash from the config file
exec 3< "$conf"
read user <&3
read hash <&3
exec 3>&-
# submit the data:
# -s: stay silent
# -S: but not so silent that errors won't be shown
# -L: follow redirects (the submit page will always redirect, but the target varies depending on successful authentication)
# -A: set the useragent
# --data-urlencode: the actual data to be sent
result=$(curl -s -S -L -A "$useragent" \
--data-urlencode "u=${user}" \
--data-urlencode "t=${hash}" \
--data-urlencode "beverage=${beverage}" \
--data-urlencode "time=${time}" "https://coffeestats.org/api/v1/add-drink")
return=$?
echo "${result}" | "${DIR}/.coffeestats_format_output.py" "${beverage}"
fi