-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphishin-like-track
More file actions
executable file
·150 lines (120 loc) · 3.32 KB
/
phishin-like-track
File metadata and controls
executable file
·150 lines (120 loc) · 3.32 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bash coreutils curl jq
# shellcheck shell=bash
# keep-sorted start skip_lines=1 prefix_order=type,,>,||
type \
curl \
jq \
phishin-auth-token \
>/dev/null \
|| exit 1
# keep-sorted end
set -euo pipefail
usage() {
# shellcheck disable=SC2059
[[ "$#" -eq 0 ]] || printf "$@" >&2
cat >&2 <<EOF
usage: ${0##*/} <show> [!][<set>:]<track>
Like tracks of a given show on Phish.in.
options:
<show> An ISO formatted date (ex. 1969-12-31)
[!]<track> The number of the track in question.
If prefixed with an exclamation point, the track
will be unliked instead.
see also: phishin-auth-token, phishin-list-shows.
Kylie McClain <kylie@somas.is>
EOF
[[ "$#" -eq 0 ]] || exit 1
exit 64 # EX_USAGE
}
phishin_auth() {
local -
set +x
: "${PHISHIN_USER_TOKEN:=$(phishin-auth-token)}" || exit 1
export PHISHIN_USER_TOKEN
}
curl() {
command curl \
--no-progress-meter \
--location \
--user-agent 'phish-cli <kylie@somas.is>' \
"$@"
}
curl_authed() {
# shellcheck disable=SC2016
[[ -v PHISHIN_USER_TOKEN ]] || usage 'error: no $PHISHIN_USER_TOKEN set\n'
curl \
--variable %PHISHIN_USER_TOKEN \
--expand-header 'X-Auth-Token: {{PHISHIN_USER_TOKEN}}' \
"${@?curl_authed(): no arguments given}"
}
[[ "${1:-}" == --help ]] && usage
[[ "$#" -gt 0 ]] || usage 'error: no arguments provided\n'
phishin_auth
errors=0
show_date="$1"
[[ "$show_date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] \
|| usage 'error: show argument is not an ISO format date: %s\n' "$show_date"
shift
attempt_unlike=false
track=
track_set=
for track; do
attempt_unlike=false
if [[ "${track:0:1}" == '!' ]]; then
attempt_unlike=true
fi
case "$track" in
*:*)
track_set=${track%%:*}
track=${track##*:}
;;
*)
track_set=
;;
esac
track=${track/#0/}
[[ -n "${track_set:-}" ]] && track_set=${track_set/#0/}
# Make sure they're numbers
: $((track))
: $((track_set))
# /likes requires the show ID be given, not a date
show_data=$(
curl --fail-with-body "https://phish.in/api/v2/shows/${show_date}"
)
track_id=$(
jq -r \
--argjson track "$((track - 1))" \
'.tracks[$track] | .id' \
<<<"$show_data"
)
track_liked=$(
jq -r \
--argjson track "$((track - 1))" \
'.tracks[$track].liked_by_user' \
<<<"$show_data"
)
if [[ "$track_liked" == true ]] && [[ "$attempt_unlike" == true ]]; then
method=DELETE
verb=unliking
else
method=POST
verb=liking
fi
result=$(
jq -nc --argjson track_id "${track_id}" \
'{"likable_type": "Track", "likable_id": $track_id}' \
| curl_authed -X "${method}" --json @- "https://phish.in/api/v2/likes"
)
if jq -cre --arg track "$track" '.id' <<<"$result" >/dev/null; then
printf '%s\n' "$track"
else
errors=$((errors + 1))
printf 'error: failed while %s track %s (id: %i, show: %s)\n' \
"$verb" "$track" "$track_id" "$show_date" >&2
fi
done
if [[ "$errors" -gt 0 ]]; then
exit 1
fi