-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmtg-wallpapers
executable file
·94 lines (80 loc) · 2.15 KB
/
mtg-wallpapers
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
#!/bin/bash
readonly program="$(basename "${0}")"
readonly valid_sizes=('2560x1600' '1920x1080' '1280x960' 'iphone' 'tablet' 'facebook')
readonly default_size="${valid_sizes[0]}"
function is_string_in_array {
local string="${1}"
for value in "${@:2}"; do
[[ "${string}" == "${value}" ]] && return 0
done
return 1
}
function usage {
echo "
Download Magic: The Gathering wallpapers.
Usage:
${program} [options]
Options:
-d, --directory Directory to save wallpapers to. Defaults to the current one.
-s, --size Wallpaper download size. Must be one of: ${valid_sizes[*]}. Defaults to ${default_size}.
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Options
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-d | --directory)
readonly given_directory="${2}"
shift
;;
-s | --size)
readonly given_wallpaper_size="${2}"
shift
;;
--)
shift
args+=("${@}")
break
;;
-*)
echo "Unrecognised option: ${1}"
exit 1
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
# Set wallpaper size to download
readonly wallpaper_size="$(
if [[ -n "${given_wallpaper_size}" ]]; then
echo -n "${given_wallpaper_size}"
else
echo -n "${default_size}"
fi
)"
# Abort if wallpaper size is invalid
if ! is_string_in_array "${wallpaper_size}" "${valid_sizes[@]}"; then
echo "Wallpaper size needs to be one of: ${valid_sizes[*]}" >&2
exit 1
fi
# Move to download directory
if [[ -n "${given_directory}" && ! -d "${given_directory}" ]]; then
echo "${given_directory} is not a valid directory." >&2
exit 1
fi
cd "${given_directory}" || exit 1
# Download wallpapers
readonly wallpaper_urls="$(curl --silent 'https://magic.wizards.com/en/news' | grep --extended-regexp "href=\"//.*?${wallpaper_size}.jpg" | sed -E "s/.*href=\"([^\"]*${wallpaper_size}.jpg)\".*/\1/" | uniq)"
# Download new wallpapers
for wallpaper_url in ${wallpaper_urls}; do
echo Downloading "$(basename "${wallpaper_url}")…"
curl --silent --remote-name "http:${wallpaper_url}"
done