Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses jq to parse json output from Bing.com #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions Tools/com.ideasftw.bing-wallpaper.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
<array>
<string>/bin/bash</string>
<string>/Users/username/Pictures/bing-wallpaper.sh</string>
<string>-s</string>
<string>-w</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>LowPriorityIO</key>
<true/>
<key>Nice</key>
Expand All @@ -20,5 +27,9 @@
<key>Minute</key>
<integer>0</integer>
</dict>
<key>StandardErrorPath</key>
<string>/tmp/bing-wallpaper.err</string>
<key>StandardOutPath</key>
<string>/tmp/bing-wallpaper.out</string>
</dict>
</plist>
33 changes: 19 additions & 14 deletions bing-wallpaper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ print_message() {
}

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

# Option parsing
BOOST=1
while [[ $# -gt 0 ]]; do
key="$1"

Expand Down Expand Up @@ -96,38 +96,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
jq -r '.images | reverse | .[] | .url' | \
sed -e "s/[[:digit:]]\{1,\}x[[:digit:]]\{1,\}/$RESOLUTION/" | \
# FQDN the image urls
sed -e "s/\(.*\)/${PROTO}\:\/\/www.bing.com\1/" | \
tr "\n" " ")
sed -e "s/\(.*\)/$PROTO:\/\/bing.com\1/")

for pic in "${urls[@]}"; do
for p in "${urls[@]}"; do
if [ -z "$FILENAME" ]; then
filename=$(echo "$pic" | sed -e 's/.*[?&;]id=\([^&]*\).*/\1/' | grep -oe '[^\.]*\.[^\.]*$')
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..."
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