-
Notifications
You must be signed in to change notification settings - Fork 3
/
Setup
executable file
·358 lines (340 loc) · 11.6 KB
/
Setup
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
#
# Setup
#
# Setup the Custom Discogs vault with markdown from the user Discogs collection
# or a local music library using the Discogs API to retrieve artist/album info
URL="https://api.discogs.com"
AGE="ObsidianCustomDiscogs/1.0.2 +https://github.com/doctorfree/Obsidian-Custom-Discogs"
LOCAL=
UPD=
VAULT=
[ -x Tools/Discogs/mkdiscogs ] || {
echo "Tools/Discogs/mkdiscogs does not exist or is not executable."
echo "The Setup script must be run in the Obsidian-Custom-Discogs folder."
echo "Exiting without performing custom setup."
exit 1
}
# Get the Discogs username and a Discogs API token
[ -f "${HOME}/.config/mpprc" ] && . "${HOME}/.config/mpprc"
usage() {
printf "Usage: ./Setup [-L /path/to/library] [-A] [-f foldername] [-v vault] [-R] [-U] [-t token] [-u user] [-ehnqr]"
printf "\nWhere:"
printf "\n\t-A indicates add existing vault folder releases to a Discogs collection"
printf "\n\t\tVault folder is specified with '-v vault'"
printf "\n\t\tVault folder previously created with './Setup -L /path/to/library'"
printf "\n\t\tCan be used with '-f foldername' to specify collection folder"
printf "\n\t-R indicates remove items from specified Discogs collection folder"
printf "\n\t\tMust be accompanied by '-f foldername'"
printf "\n\t-L 'path' indicates use a local music library rather than Discogs collection"
printf "\n\t-U indicates perform an update of the Discogs collection"
printf "\n\t-f 'foldername' specifies the Discogs collection folder name to use."
printf "\n\t\tOnly used in conjunction with '-A' (add releases to Discogs collection)."
printf "\n\t\tIf no folder by this name exists, one will be created."
printf "\n\t\tDefault: Uncategorized"
printf "\n\t-e displays example usage and exits"
printf "\n\t-n indicates perform a dry run (only used in conjunction with '-A')"
printf "\n\t-q indicates quiet mode (only used in conjunction with '-A')"
printf "\n\t-r indicates remove intermediate JSON created during previous run"
printf "\n\t-t 'token' specifies the Discogs API token"
printf "\n\t-u 'user' specifies the Discogs username"
printf "\n\t-v 'vault' specifies the folder name for generated artist/album markdown"
printf "\n\t-h displays this usage message and exits\n"
exit 1
}
examples() {
printf "\nExample invocations:"
printf "\n\t# Retrieve Discogs collection"
printf "\n\t# Generated markdown in capitalized Discogs username folder"
printf "\n\t./Setup"
printf "\n\t# Generated markdown in 'Discogs' folder"
printf "\n\t./Setup -v Discogs"
printf "\n\t# Retrieve Discogs user 'foobar' collection"
printf "\n\t./Setup -u foobar"
printf "\n\t# Retrieve Discogs data for local music library in /u/audio"
printf "\n\t./Setup -L /u/audio -v Audio"
printf "\n\t# Provide Discogs username and API token on command line"
printf "\n\t./Setup -L ~/Music -u doctorfree -t xyzkdkslekjrelrkek"
printf "\n\t# Retrieve Discogs data for genre local music library in /u/jazz"
printf "\n\t./Setup -L /u/jazz -v Jazz"
printf "\n\t# Add existing vault releases to Discogs collection folder"
printf "\n\t# From a previously generated run of './Setup -L /path/to/library'"
printf "\n\t# Perform a dry run:"
printf "\n\t./Setup -n -A -f MyMusic -v Music_Library"
printf "\n\t# Add releases from Music_Library folder to Discogs collection MyMusic:"
printf "\n\t./Setup -A -f MyMusic -v Music_Library"
printf "\n\t# Delete releases in MyMusic Discogs collection folder:"
printf "\n\t./Setup -R -f MyMusic\n"
exit 1
}
check_username() {
curl --stderr /dev/null \
-A "${AGE}" "${URL}/users/$1" | \
grep "User does not exist" > /dev/null && {
DISCOGS_USER=
}
}
check_token() {
curl --stderr /dev/null \
-A "${AGE}" \
-H "Authorization: Discogs token=$1" \
"${URL}/users/${username}" | \
grep "Invalid consumer token" > /dev/null && {
DISCOGS_TOKEN=
}
}
get_username() {
numtries=1
while true
do
read -p "Discogs username: " username
case ${username} in
[?]* )
DISCOGS_USER="${username}"
check_username "${username}"
if [ "${DISCOGS_USER}" ]
then
break
else
numtries=$((numtries + 1))
[ ${numtries} -gt 3 ] && {
echo "Too many failed attempts to set Discogs username. Exiting."
exit 1
}
echo "Discogs user ${username} does not exist. Please try again."
fi
;;
* ) echo "Please enter a username."
;;
esac
done
}
get_token() {
numtries=1
while true
do
read -p "Discogs API token: " token
case ${token} in
[?]* )
DISCOGS_TOKEN="${token}"
check_token "${token}"
if [ "${DISCOGS_TOKEN}" ]
then
break
else
numtries=$((numtries + 1))
[ ${numtries} -gt 3 ] && {
echo "Too many failed attempts to set Discogs API token. Exiting."
exit 1
}
echo "Invalid Discogs API token: ${token}. Please try again."
fi
;;
* ) echo "Please enter a Discogs API token."
;;
esac
done
}
add2discogs=
cleanup=
dryrun=
foldername=
quiet=
remove=
while getopts "AL:RUf:t:u:v:ehnqr" flag; do
case $flag in
A)
add2discogs=1
;;
f)
foldername="${OPTARG}"
;;
n)
dryrun="-n"
;;
q)
quiet="-q"
;;
L)
LOCAL="${OPTARG}"
[ -d "${OPTARG}" ] || {
echo "Directory specified with '-L /path/to/library', ${OPTARG},"
echo "does not exist or is not a directory."
echo "Exiting without generating markdown."
usage
}
;;
r)
cleanup=1
;;
R)
remove=1
;;
U)
UPD="-U"
;;
t)
DISCOGS_TOKEN="${OPTARG}"
;;
u)
DISCOGS_USER="${OPTARG}"
;;
v)
VAULT="${OPTARG}"
;;
e)
examples
;;
h)
usage
;;
esac
done
shift $(( OPTIND - 1 ))
[ "${add2discogs}" ] && [ "${LOCAL}" ] && {
echo "-A and -L cannot be used together."
echo "-A (add to Discogs collection) is only supported after a previous run with -L"
echo "First perform \"./Setup -L ${LOCAL}\""
echo "After that succeeds then perform './Setup -A ...'"
echo "Exiting"
exit 1
}
[ "${add2discogs}" ] && [ "${remove}" ] && {
echo "-A and -R cannot be used together."
echo "-A indicates \"add to Discogs collection\", -R indicates remove from collection"
echo "Exiting"
exit 1
}
[ "${remove}" ] && [ "${LOCAL}" ] && {
echo "-R and -L cannot be used together."
echo "-R (remove from Discogs collection) is only supported after a previous run with -A"
echo "First perform \"./Setup -L ${LOCAL}\""
echo "After that succeeds then perform './Setup -A ...'"
echo "After that succeeds then perform './Setup -R ...' to remove items added with -A"
echo "Exiting"
exit 1
}
[ "${cleanup}" ] && {
echo "Cleaning up any previously created JSON used in generating markdown"
rm -rf Tools/Discogs/json
exit 0
}
have_curl=`type -p curl`
have_jq=`type -p jq`
have_wget=`type -p wget`
[ "${have_curl}" ] || {
printf "\nThe 'curl' utility is required but not found in this user's execution path."
printf "\nVerify that 'curl' is installed and available on this system, add it to PATH,"
printf "\nor install 'curl'. On most Linux systems 'curl' can be installed with either:"
printf "\n\tsudo apt install curl"
printf "\nor"
printf "\n\tsudo dnf install curl"
printf "\nOn Mac OS 'curl' can be installed with Homebrew:"
printf "\n\tbrew install curl\n"
}
[ "${have_jq}" ] || {
printf "\nThe 'jq' utility is required but not found in this user's execution path."
printf "\nVerify that 'jq' is installed and available on this system, add it to PATH,"
printf "\nor install 'jq'. On most Linux systems 'jq' can be installed with either:"
printf "\n\tsudo apt install jq"
printf "\nor"
printf "\n\tsudo dnf install jq"
printf "\nOn Mac OS 'jq' can be installed with Homebrew:"
printf "\n\tbrew install jq\n"
}
[ "${have_wget}" ] || {
printf "\nThe 'wget' utility is required but not found in this user's execution path."
printf "\nVerify that 'wget' is installed and available on this system, add it to PATH,"
printf "\nor install 'wget'. On most Linux systems 'wget' can be installed with either:"
printf "\n\tsudo apt install wget"
printf "\nor"
printf "\n\tsudo dnf install wget"
printf "\nOn Mac OS 'wget' can be installed with Homebrew:"
printf "\n\tbrew install wget\n"
}
[ "${have_jq}" ] && [ "${have_curl}" ] && [ "${have_wget}" ] || {
printf "\nSystem requirements not met. Exiting without performing setup.\n"
exit 1
}
if [ "${DISCOGS_USER}" ]
then
check_username "${DISCOGS_USER}"
[ "${DISCOGS_USER}" ] || {
printf "\nDiscogs username found in ~/.config/mpprc appears to be invalid.\n"
get_username
}
else
printf "\nDiscogs username required but none found in ~/.config/mpprc"
printf "\nThe Discogs username can be found by visiting discogs.com. Login then"
printf "\nuse the dropdown of your user icon in the upper right corner. Click on"
printf "\n'Profile'. Your Discogs username is the last component of the profile URL."
printf "\nPlease enter your Discogs username.\n"
get_username
fi
if [ "${DISCOGS_TOKEN}" ]
then
check_token "${DISCOGS_TOKEN}"
[ "${DISCOGS_TOKEN}" ] || {
printf "\nDiscogs API token found in ~/.config/mpprc appears to be invalid.\n"
get_token
}
else
printf "\nDiscogs API token not found in ~/.config/mpprc"
printf "\nLogin to discogs.com and visit:\n"
printf "\n\thttps://www.discogs.com/settings/developers\n"
printf "\nto find or generate a Discogs API token.\n"
printf "\nPlease enter a Discogs API token.\n"
get_token
fi
cd Tools/Discogs
if [ "${LOCAL}" ]
then
[ "${VAULT}" ] || VAULT="Music_Library"
if [ -x search-albums-markdown ]
then
./search-albums-markdown \
-t "${DISCOGS_TOKEN}" \
-u "${DISCOGS_USER}" \
-v "${VAULT}" "${LOCAL}"
else
echo "Tools/Discogs/search-albums-markdown is not found or not executable."
echo "Exiting without generating markdown."
exit 1
fi
if [ -x artists2markdown ]
then
./artists2markdown -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" -v "${VAULT}"
else
echo "Tools/Discogs/artists2markdown is not found or not executable."
echo "No artist markdown generated for artists in ${LOCAL}."
fi
./mkdiscogs -A -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" -v "${VAULT}"
./mkdiscogs -d -T -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" -v "${VAULT}"
else
if [ "${add2discogs}" ]
then
if [ "${VAULT}" ] && [ "${foldername}" ]
then
./albums2discogs -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" \
-v "${VAULT}" -f "${foldername}" ${dryrun} ${quiet}
else
if [ "${VAULT}" ]
then
./albums2discogs -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" \
-v "${VAULT}" ${dryrun} ${quiet}
else
./albums2discogs -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" \
${dryrun} ${quiet}
fi
fi
else
if [ "${remove}" ]
then
./albums2discogs -r -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" \
-f "${foldername}" ${dryrun} ${quiet}
else
[ "${VAULT}" ] || VAULT="Discogs"
./mkdiscogs -a -t "${DISCOGS_TOKEN}" -u "${DISCOGS_USER}" ${UPD} -v "${VAULT}"
fi
fi
fi