forked from unrblt/beets-bandcamp
-
Notifications
You must be signed in to change notification settings - Fork 13
/
url2json
executable file
·92 lines (87 loc) · 2.19 KB
/
url2json
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
#!/bin/bash
#
# Usage: url2json [-ahsu] [<bandcamp-url>]
#
#Parse given bandcamp url and print the resulting json. By default,
#it outputs an indented/prettified json string which is used by the tests.
#Use
#
#-a to show all fields,
#-h (human) to include colors and to page it
#-s to save the initial Bandcamp JSON to ./jsons folder in the repo root
#-u to update the json data in ./tests/json.
#Ensure you have curl, jq and git on your system.
#
# Examples
# · Save output to a file
# url2json https://hello.bandcamp.com/album/hi > ./tests/json/testcase.json
#
# · Include all fields and open with jq and less
# url2json -a -h https://hello.bandcamp.com/album/hi
#
# · Update all json test cases
# url2json -u
#
ALL_FIELDS=0
HUMAN=0
UPDATE=0
SAVE=0
get_json() {
jqargs=(--sort-keys)
((ALL_FIELDS)) || jqargs+=('del(.comment, .sponsor, .albumRelease[0].offers)')
curl -sL "${1//[\"\']/}" | sed 's/^[^{]*//; s/[^}]*$//; s/"/"/g' | grep -E '\{.*(dateModif|action=gift)' | {
if ((SAVE)); then
url=$1
save_path=$(realpath "$0")
save_path=$(dirname "$save_path")
save_path=$save_path/jsons/${1//\//_}.json
jq -cM >"$save_path"
[[ -s $save_path ]] || {
rm "$save_path"
exit 1
}
elif ((HUMAN)); then
jq -C "${jqargs[@]}" | less -R
else
jq "${jqargs[@]}"
fi
}
}
update_test_jsons() {
files=(./tests/json/*.json)
for file in "${files[@]}"; do
url=$(jq -r '.["@id"]' "$file")
printf '%-50s' "$file"
get_json "$url" |
jq 'del(
(if .track then .track.itemListElement[].item else . end)
| .additionalProperty[]
| select(.name | test("mp3"))
)' >"$file"
if git diff --quiet "$file" &>/dev/null; then
echo -e ' \e[1;32mNo changes\e[0m'
else
echo
git diff -U0 --color-words='[^ ]|[0-9]+' "$file"
fi
done
git diff --compact-summary "${files[@]}"
}
if [[ -z "$1" ]]; then
sed -n '2,$s/^#/ /p' "$0"
else
for arg in "$@"; do
case $arg in
-a) ALL_FIELDS=1 ;;
-h) HUMAN=1 ;;
-s) SAVE=1 ;;
-u) UPDATE=1 ;;
*) url=$arg ;;
esac
done
if ((UPDATE)); then
update_test_jsons
else
get_json "$url"
fi
fi