From 050b77786789c7db7c2334d1c055ed8d961b4c4d Mon Sep 17 00:00:00 2001 From: Yuan Liu Date: Mon, 21 Dec 2020 14:27:40 -0800 Subject: [PATCH] Uses jq to parse json output from Bing.com 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. --- README.md | 5 ++++- bing-wallpaper.sh | 33 +++++++++++++++++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a4a16c7..1f4767e 100644 --- a/README.md +++ b/README.md @@ -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? @@ -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 -------------------- diff --git a/bing-wallpaper.sh b/bing-wallpaper.sh index f352efc..bef4c98 100755 --- a/bing-wallpaper.sh +++ b/bing-wallpaper.sh @@ -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() { @@ -44,8 +44,9 @@ transform_urls() { } # Defaults -PICTURE_DIR="$HOME/Pictures/bing-wallpapers/" +PICTURE_DIR="$HOME/Pictures/bing-wallpapers" RESOLUTION="1920x1080" +BOOST="1" # Option parsing while [[ $# -gt 0 ]]; do @@ -71,7 +72,7 @@ while [[ $# -gt 0 ]]; do SSL=true ;; -b|--boost) - BOOST=$(($2-1)) + BOOST="$2" shift ;; -q|--quiet) @@ -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' @@ -105,19 +113,11 @@ 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 '[^\.]*\.[^\.]*$') @@ -125,7 +125,7 @@ for p in "${urls[@]}"; do 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..." @@ -133,7 +133,8 @@ for p in "${urls[@]}"; do done if [ -n "$SET_WALLPAPER" ]; then + print_message "Setting wallpaper to $PICTURE_DIR/$filename" /usr/bin/osascript<