Skip to content

Commit

Permalink
Update and simplify script
Browse files Browse the repository at this point in the history
After moving to Arch I decided to give this forgotten script a try. It didn't work. It was also very complicated. URL parsing from JSON is a crazy thing to try to do in a bash script. I don't have a Mac to test these changes on but hopefully everything still works. There were a number of overlapping PRs over the years that made it into this revision. I've tried to add credit to the original PRs where applicable.

Summary of changes:

1. Merge latest and boost code paths (From @Lyncredible in #39)
2. Use https for all requests if -s (From @Lyncredible in #39)
3. Update URL patterns  (From @Lyncredible in #39, @microdog in #36, @nitrogear in #23)
4. Add UHD support (fixes #43)
5. Remove contributing.json as it wasn't used anymore
  • Loading branch information
thejandroman committed Jan 18, 2022
1 parent 60beaa7 commit fc6034d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 69 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[![Build Status](https://travis-ci.org/thejandroman/bing-wallpaper.svg?branch=travis)](https://travis-ci.org/thejandroman/bing-wallpaper)

Bing Wallpaper for Mac and Ubuntu
=================================

Expand All @@ -12,6 +10,7 @@ The script was tested on:

- Mac OS X 10.8 - 10.12
- Ubuntu 12.04 - 16.04
- Arch 2022.01.01

How to use?
-----------
Expand All @@ -38,7 +37,8 @@ Options:
Will be created if it does not exist.
[default: $HOME/Pictures/bing-wallpapers/]
-r --resolution <resolution> The resolution of the image to retrieve.
Supported resolutions: 1920x1200 1920x1080 800x480 400x240
Supported resolutions:
UHD 1920x1200 1920x1080 800x480 400x240
-w --set-wallpaper Set downloaded picture as wallpaper (Only mac support for now).
-h --help Show this screen.
--version Show version.
Expand Down
43 changes: 17 additions & 26 deletions bing-wallpaper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# shellcheck disable=SC1117

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

usage() {
cat <<EOF
Expand All @@ -24,7 +24,8 @@ Options:
Will be created if it does not exist.
[default: $HOME/Pictures/bing-wallpapers/]
-r --resolution <resolution> The resolution of the image to retrieve.
Supported resolutions: ${RESOLUTIONS[*]}
Supported resolutions:
${RESOLUTIONS[*]}
-w --set-wallpaper Set downloaded picture as wallpaper (Only mac support for now).
-h --help Show this screen.
--version Show version.
Expand All @@ -37,17 +38,12 @@ print_message() {
fi
}

transform_urls() {
sed -e "s/\\\//g" | \
sed -e "s/[[:digit:]]\{1,\}x[[:digit:]]\{1,\}/$RESOLUTION/" | \
tr "\n" " "
}

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

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

Expand All @@ -71,7 +67,7 @@ while [[ $# -gt 0 ]]; do
SSL=true
;;
-b|--boost)
BOOST=$(($2-1))
BOOST=$(($2))
shift
;;
-q|--quiet)
Expand Down Expand Up @@ -104,29 +100,24 @@ done
# 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 | \
grep -Eo "url\(.*?\)" | \
sed -e "s/url(\([^']*\)).*/http:\/\/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
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" " ")

for p in "${urls[@]}"; do
for pic in "${urls[@]}"; do
if [ -z "$FILENAME" ]; then
filename=$(echo "$p" | sed -e 's/.*[?&;]id=\([^&]*\).*/\1/' | grep -oe '[^\.]*\.[^\.]*$')
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" "$p"
curl $CURL_QUIET -Lo "$PICTURE_DIR/$filename" "$pic"
else
print_message "Skipping: $filename..."
fi
Expand Down
40 changes: 0 additions & 40 deletions contributing.json

This file was deleted.

0 comments on commit fc6034d

Please sign in to comment.