Skip to content

Commit

Permalink
Uses jq to parse json output from Bing.com
Browse files Browse the repository at this point in the history
This commit makes the script compatible with current Bing.com as of 2020-12-21
and macOS 10.15. It includes the following changes:

- Use jq to parse json output from Bing.com
- Merge the latest and boost code paths
- Change wallpaper on macOS 10.15 with proper applescript
- Use https for all Bing.com requests if `-s/--ssl` is specified

Backward compatibility is _not_ tested with older macOS versions.
  • Loading branch information
Lyncredible committed Dec 21, 2020
1 parent 60beaa7 commit 4531c40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ it to a directory.

The script was tested on:

- Mac OS X 10.8 - 10.12
- Mac OS X 10.8 - 10.12, 10.15
- Ubuntu 12.04 - 16.04

How to use?
Expand Down Expand Up @@ -43,6 +43,9 @@ Options:
-h --help Show this screen.
--version Show version.
```
Dependencies
------------
The script uses [jq](https://stedolan.github.io/jq/) to parse json output from Bing.com. Installation instructions can be found on [jq's download page](https://stedolan.github.io/jq/download/).

Configuration on Mac
--------------------
Expand Down
31 changes: 16 additions & 15 deletions bing-wallpaper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# shellcheck disable=SC1117

readonly SCRIPT=$(basename "$0")
readonly VERSION='0.4.0'
readonly VERSION='0.5.0'
readonly RESOLUTIONS=(1920x1200 1920x1080 800x480 400x240)

usage() {
Expand Down Expand Up @@ -46,6 +46,7 @@ transform_urls() {
# Defaults
PICTURE_DIR="$HOME/Pictures/bing-wallpapers/"
RESOLUTION="1920x1080"
BOOST="1"

# Option parsing
while [[ $# -gt 0 ]]; do
Expand All @@ -71,7 +72,7 @@ while [[ $# -gt 0 ]]; do
SSL=true
;;
-b|--boost)
BOOST=$(($2-1))
BOOST="$2"
shift
;;
-q|--quiet)
Expand All @@ -97,6 +98,13 @@ while [[ $# -gt 0 ]]; do
shift
done

# Test for existence of the command jq
if ! command -v jq &> /dev/null
then
echo "The command jq could not be found. Refer to the readme for how to install jq."
exit
fi

# Set options
[ -n "$QUIET" ] && CURL_QUIET='-s'
[ -n "$SSL" ] && PROTO='https' || PROTO='http'
Expand All @@ -105,35 +113,28 @@ done
mkdir -p "${PICTURE_DIR}"

# Parse bing.com and acquire picture URL(s)
read -ra urls < <(curl -sL $PROTO://www.bing.com | \
grep -Eo "url\(.*?\)" | \
sed -e "s/url(\([^']*\)).*/http:\/\/bing.com\1/" | \
read -ra urls < <(curl -sL "$PROTO://www.bing.com/HPImageArchive.aspx?format=js&n=$BOOST" | \
jq -r '.images | reverse | .[] | .url' | \
sed -e "s/\(.*\)/$PROTO:\/\/bing.com\1/" | \
transform_urls)

if [ -n "$BOOST" ]; then
read -ra archiveUrls < <(curl -sL "$PROTO://www.bing.com/HPImageArchive.aspx?format=js&n=$BOOST" | \
grep -Eo "url\(.*?\)" | \
sed -e "s/url(\([^']*\)).*/http:\/\/bing.com\1/" | \
transform_urls)
urls=( "${urls[@]}" "${archiveUrls[@]}" )
fi

for p in "${urls[@]}"; do
if [ -z "$FILENAME" ]; then
filename=$(echo "$p" | sed -e 's/.*[?&;]id=\([^&]*\).*/\1/' | grep -oe '[^\.]*\.[^\.]*$')
else
filename="$FILENAME"
fi
if [ -n "$FORCE" ] || [ ! -f "$PICTURE_DIR/$filename" ]; then
print_message "Downloading: $filename..."
print_message "Downloading: $filename from $p..."
curl $CURL_QUIET -Lo "$PICTURE_DIR/$filename" "$p"
else
print_message "Skipping: $filename..."
fi
done

if [ -n "$SET_WALLPAPER" ]; then
print_message "Setting wallpaper to $PICTURE_DIR$filename"
/usr/bin/osascript<<END
tell application "System Events" to set picture of every desktop to ("$PICTURE_DIR/$filename" as POSIX file as alias)
tell application "System Events" to tell every desktop to set picture to "$PICTURE_DIR$filename"
END
fi

0 comments on commit 4531c40

Please sign in to comment.