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 Feb 4, 2022
1 parent b13b919 commit 8bd762c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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
- Arch 2022.01.01

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
30 changes: 18 additions & 12 deletions bing-wallpaper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ print_message() {
}

# Defaults
PICTURE_DIR="$HOME/Pictures/bing-wallpapers/"
PICTURE_DIR="$HOME/Pictures/bing-wallpapers"
RESOLUTION="1920x1080"
BOOST="1"

# Option parsing
BOOST=1
Expand Down Expand Up @@ -96,38 +97,43 @@ 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'

# Create picture directory if it doesn't already exist
mkdir -p "${PICTURE_DIR}"

# Parse bing.com and acquire picture URL(s)
read -ra urls < <(curl -sL "$PROTO://www.bing.com/HPImageArchive.aspx?format=js&n=$BOOST" | \
# Extract the image urls from the JSON response
grep -Po '(?<=url":").*?(?=")' | \
# Set the image resolution
sed -e "s/[[:digit:]]\{1,\}x[[:digit:]]\{1,\}/$RESOLUTION/" | \
# FQDN the image urls
sed -e "s/\(.*\)/${PROTO}\:\/\/www.bing.com\1/" | \
tr "\n" " ")
jq -r '.images | reverse | .[] | .url' | \
sed -e "s/\(.*\)/$PROTO:\/\/bing.com\1/" | \
transform_urls)

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

Please sign in to comment.