Last active
May 15, 2022 09:59
-
-
Save b00f/4561c9083622960f6b88bd2fc40a72d2 to your computer and use it in GitHub Desktop.
Download bing wallpaper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author: Whizzzkid ([email protected]) | |
# Base URL. | |
bing="http://www.bing.com" | |
# API end point. | |
api="/HPImageArchive.aspx?" | |
# Response Format (json|xml). | |
format="&format=js" | |
# For day (0=current; 1=yesterday... so on). | |
day="&idx=0" | |
# Market for image. | |
market="&mkt=en-US" | |
# API Constant (fetch how many). | |
const="&n=1" | |
# Image extension. | |
extn=".jpg" | |
# Size. | |
size="1920x1200" | |
# Collection Path. | |
path="$HOME/Pictures/Wallpapers/" | |
# Make it run just once (useful to run as a cron) | |
run_once=false | |
while getopts "1" opt; do | |
case $opt in | |
1 ) | |
run_once=true | |
;; | |
\? ) | |
echo "Invalid option! usage: \"$0 -1\", to run once and exit" | |
exit 1 | |
;; | |
esac | |
done | |
######################################################################## | |
#### DO NOT EDIT BELOW THIS LINE ####################################### | |
######################################################################## | |
# Required Image Uri. | |
reqImg=$bing$api$format$day$market$const | |
# Logging. | |
echo "Pinging Bing API..." | |
# Fetching API response. | |
apiResp=$(curl -s $reqImg) | |
if [ $? -gt 0 ]; then | |
echo "Ping failed!" | |
exit 1 | |
fi | |
# Default image URL in case the required is not available. | |
defImgURL=$bing$(echo $apiResp | grep -oP "url\":\"[^\"]*" | cut -d "\"" -f 3) | |
# Req image url (raw). | |
reqImgURL=$bing$(echo $apiResp | grep -oP "urlbase\":\"[^\"]*" | cut -d "\"" -f 3)"_"$size$extn | |
# Image copyright. | |
copyright=$(echo $apiResp | grep -oP "copyright\":\"[^\"]*" | cut -d "\"" -f 3) | |
wp=$(echo $apiResp | grep -oP "\"wp\":.*," | cut -d "," -f 1) | |
if [ "$wp" == "\"wp\":false" ] ; then | |
reqImgURL=$defImgURL | |
fi | |
# Checking if reqImgURL exists. | |
if ! wget --quiet --spider $reqImgURL; then | |
reqImgURL=$defImgURL | |
fi | |
# Logging. | |
echo "Bing Image of the day: $reqImgURL" | |
# Getting Image Name. | |
imgName=${reqImgURL##*/} | |
# Create Path Dir. | |
mkdir -p $path | |
# Saving Image to collection. | |
curl -s -o $path$imgName $reqImgURL | |
# Logging. | |
echo "Saving image to $path$imgName" | |
# Writing copyright. | |
echo "$copyright" > $path${imgName}".txt" | |
echo "$copyright" | |
if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ] | |
then | |
xres=($(echo $(xfconf-query --channel xfce4-desktop --list | grep last-image))) | |
for x in "${xres[@]}" | |
do | |
xfconf-query --channel xfce4-desktop --property $x --set $path$imgName | |
done | |
# Set the wallpaper for unity, gnome3, cinnamon. | |
elif gsettings set org.gnome.desktop.background picture-uri-dark "file://$path$imgName"; then | |
#Logging | |
# Set the view to zoom, | |
gsettings set org.gnome.desktop.background picture-options "zoom" | |
else | |
echo "$XDG_CURRENT_DESKTOP not supported." | |
break | |
fi | |
echo "New wallpaper set successfully for $XDG_CURRENT_DESKTOP." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use it like:
bash ./bing-wallpaper.sh