forked from thejandroman/bing-wallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing-wallpaper.sh
executable file
·103 lines (92 loc) · 2.53 KB
/
bing-wallpaper.sh
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
#!/usr/bin/env bash
readonly SCRIPT=$(basename "$0")
readonly VERSION='0.1.1'
usage() {
cat <<EOF
Usage:
$SCRIPT [options]
$SCRIPT -h | --help
$SCRIPT --version
Options:
-f --force Force download of picture. This will overwrite
the picture if the filename already exists.
-s --ssl Communicate with bing.com over SSL.
-q --quiet Do not display log messages.
-n --filename <file name> The name of the downloaded picture. Defaults to
the upstream name.
-p --picturedir <picture dir> The full path to the picture download dir.
Will be created if it does not exist.
[default: $HOME/Pictures/bing-wallpapers/]
-h --help Show this screen.
--version Show version.
EOF
}
print_message() {
if [ ! "$QUIET" ]; then
printf "%s\n" "${1}"
fi
}
# Defaults
PICTURE_DIR="$HOME/Pictures/bing-wallpapers/"
SSL=true
# Option parsing
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-p|--picturedir)
PICTURE_DIR="$2"
shift
;;
-n|--filename)
FILENAME="$2"
shift
;;
-f|--force)
FORCE=true
;;
-s|--ssl)
# SSL=true
SSL=$2
;;
-q|--quiet)
QUIET=true
;;
-h|--help)
usage
exit 0
;;
--version)
printf "%s\n" $VERSION
exit 0
;;
*)
(>&2 printf "Unknown parameter: %s\n" "$1")
usage
exit 1
;;
esac
shift
done
# Set options
[ $QUIET ] && CURL_QUIET='-s'
[ $SSL ] && PROTO='https' || PROTO='http'
# Create picture directory if it doesn't already exist
mkdir -p "${PICTURE_DIR}"
# Parse bing.com and acquire picture URL(s)
urls=( $(curl -sL $PROTO://www.bing.com | \
grep -Eo "url:'.*?'" | \
sed -e "s/url:'\([^']*\)'.*/$PROTO:\/\/bing.com\1/" | \
sed -e "s/\\\//g") )
for p in "${urls[@]}"; do
if [ -z "$FILENAME" ]; then
filename=$(echo "$p"|sed -e "s/.*\/\(.*\)/\1/")
else
filename="$FILENAME"
fi
if [ $FORCE ] || [ ! -f "$PICTURE_DIR/$filename" ]; then
print_message "Downloading: $filename..."
curl $CURL_QUIET -Lo "$PICTURE_DIR/$filename" "$p"
else
print_message "Skipping: $filename..."
fi
done