forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtgwallpaper
More file actions
executable file
·115 lines (95 loc) · 3.54 KB
/
Copy pathmtgwallpaper
File metadata and controls
executable file
·115 lines (95 loc) · 3.54 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
#!/bin/bash
readonly program="$(basename "${0}")"
readonly config_file="${HOME}/.config/mtgwallpaper"
readonly valid_dimensions=('2560x1600' '1920x1080' '1280x960' 'iPhone' 'Tablet' 'Facebook')
function error_message {
echo -e "$(tput setaf 1)${1}$(tput sgr0)" >&2
exit 1
}
function wrong_setting {
error_message "${1}\nRun \`${program} --init\` to create/overwrite the settings file with sensible defaults."
}
function syntax_error {
error_message "${program}: ${1}\nTry \`${program} --help\` for more information."
exit 1
}
function string_in_array? {
local string="${1}"
for value in "${@:2}"; do
[[ "${string}" == "${value}" ]] && return 0
done
return 1
}
function create_default_config {
echo "Creating a configuration with sensible defaults on ${config_file}" >&2
echo "wallpaper_directory=\"\${HOME}/Pictures/MTG_wallpapers\"
maximum_wallpaper_count=\"10\"
wallpaper_dimensions=\"2560x1600\" # Valid options (capitalisation matters): ${valid_dimensions[*]}
last_walpaper_url=\"\" # Do not touch, as it will be automatically updated" | sed -E 's/^ {4}//' > "${config_file}"
}
function usage {
echo "
Usage:
${program} [options]
Options:
-i, --init Create a settings file with sensible defaults (overwrites any existing one) and exit.
-e, --edit Open the configuration file in a text editor and exit.
-d, --download Run the script to get new wallpapers.
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Available flags
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-i | --init)
create_default_config
exit 0
;;
-e | --edit)
open -t "${config_file}"
exit 0
;;
-d | --download)
start_download='true'
;;
-*)
syntax_error "unrecognized option: ${1}"
;;
*)
break
;;
esac
shift
done
trap 'exit 1' SIGINT
[[ -n "${start_download}" ]] || error_message 'You need to give the `--download` flag to download wallpapers.'
# Make sure all settings are valid
[[ -f "${config_file}" ]] && source "${config_file}" || wrong_setting 'There is no settings file.'
[[ -n "${wallpaper_directory}" ]] || wrong_setting "wallpaper_directory in ${config_file} is missing."
[[ "${maximum_wallpaper_count}" =~ ^[0-9]+$ ]] || failure "maximum_wallpaper_count if ${config_file} needs to be a whole number."
string_in_array? "${wallpaper_dimensions}" "${valid_dimensions[@]}" || failure "wallpaper_dimensions in ${config_file} needs to be one of ${valid_dimensions[*]}."
if [[ ! -d "${wallpaper_directory}" ]]; then
if ! mkdir -p "${wallpaper_directory}"; then
error_message "You don’t have permissions to create ${wallpaper_directory}."
fi
fi
# Main section
cd "${wallpaper_directory}" || exit 1
recent_wallpaper_urls="$(curl --silent 'http://magic.wizards.com/en/articles/media/wallpapers' | grep "download.*${wallpaper_dimensions}" | sed -E 's/.*href="([^"]*).*/\1/')"
# Update latest wallpaper url
most_recent_wallpaper_url="$(head -1 <<< "${recent_wallpaper_urls}")"
sed -i '' '/last_walpaper_url/d' "${config_file}"
echo "last_walpaper_url=\"${most_recent_wallpaper_url}\" # Do not touch, as it will be automatically updated" >> "${config_file}"
# Download new wallpapers
for wallpaper_url in ${recent_wallpaper_urls}; do
[[ "${wallpaper_url}" == "${most_recent_wallpaper_url}" ]] && break
curl --silent --remote-name "${wallpaper_url}"
done
# Delete old wallpapers
while [[ "$(ls -1 | wc -l)" -gt "${maximum_wallpaper_count}" ]]; do
rm "$(ls -1t | tail -1)"
done