2

i have a script (that Nuno wrote) to wget an image from one particular site and set my wallpaper. however i can't seem to modify it to be able to wget an image from this site: chromecast

images in the source have a url like this: image

i can't seem to isolate the url using grep since it is greedy. i realize grep -P can work, and it does if i enter it manually in the terminal. but in the script, when assigning a variable, it doesn't work. the variable can be set with grep but not grep -P.

#!/bin/bash
# * Name: earthwall.sh
# * Description: Downloads random image from earthview.withgoogle.com and sets as wallpaper on OSX
# * Author: Nuno Serro
# * Date: 09/07/2015 22:24:11 WEST
# * License: This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see      <http://www.gnu.org/licenses/>.
#
# * Copyright (c) 2015, Nuno Serro
#PID=$(pgrep gnome-session)
#PID=$(pgrep -f 'gnome-session' | head -n1)
#export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS     /proc/$PID/environ)

mkdir -p ~/Pictures/globewall
cd ~/Pictures/globewall

# Get page index
wget -q https://clients3.google.com/cast/chromecast/home -O ~/Pictures/globewall/.index.html 2> /dev/null
if [ $? -ne 0 ]; then
echo "Failed to get index from google chromecast"
exit 1
fi

# Set image url, name and location
image_url=`cat ~/Pictures/globewall/.index.html | grep lh3.googleusercontent.com | grep -shoP 'https:[\\][\/][\\][\/]lh3(.*?)-mv' -m 1 -o | head -1 | sed 's/\\//g' | sed 's/u003d/=/g'`
image_name= wallpaper.jpg

# Get image
wget -q $image_url -O ~/Pictures/globewall/$image_name 2> /dev/null
if [ $? -ne 0 ]; then
echo "Failed to get image from www.googleusercontent.com"
exit 1
fi

# Change wallpaper
sleep 1
/usr/bin/gsettings set org.gnome.desktop.background picture-options 'zoom'
/usr/bin/gsettings set org.gnome.desktop.background picture-uri "file://~/Pictures/globewall/$image_name"
/usr/bin/gsettings set org.gnome.desktop.screensaver picture-uri "file://~/Pictures/globewall/$image_name"

echo "Wallpaper changed to $image_name"
exit 0

i'm stuck.

1

Your script needs a bit of a makeover.

The two lines following the #Set image url, name and location line are broken for various reasons (e.g. incorrect grep syntax, incorrect assignment). In addition, your script doesn't double quote filenames which may break your script depending on the circumstances.

To make it work more reliably, I rewrote the core of your script as follows:

#!/bin/bash
fn_basedir=~/Pictures/globewall/
fn_index='.index.html'
fn_image='wallpaper.jpg'

mkdir -p "$fn_basedir"

# Get page index
wget -q "https://clients3.google.com/cast/chromecast/home" -O "${fn_basedir}${fn_index}" 
if [ $? -ne 0 ]; then
  echo "Failed to get index from google chromecast"
  exit 1
fi

# Set image url
image_url=$(grep -oP 'https:\\/\\/lh3(.*?)-mv' "${fn_basedir}${fn_index}" | sed -e 's/\\//g' -e 's/u003d/=/g' | head -1)

# Get image
wget -q "$image_url" -O "${fn_basedir}${fn_image}" 
if [ $? -ne 0 ]; then
  echo "Failed to get image from www.googleusercontent.com"
  exit 1
fi

# Change wallpaper
sleep 1
/usr/bin/gsettings set org.gnome.desktop.background picture-options 'zoom'
/usr/bin/gsettings set org.gnome.desktop.background picture-uri "file://${fn_basedir}${fn_image}"
/usr/bin/gsettings set org.gnome.desktop.screensaver picture-uri "file://${fn_basedir}${fn_image}"

echo "Wallpaper changed to ${fn_image}"
exit 0

Awesome pictures btw!

  • thanks, @ozzy! that helps me learn the correct way to write my scripts. i appreciate it. the only thing i had to change was ~/Pictures/globewall to /home/myname/Pictures/globewall. i found it was creating a directory called ~ in the directory where my script was run from rather than interpreting it as my home directory. then gnome could not find the image when changing the wallpaper, it just became a black background. – ticotexas Jan 8 at 23:41
  • @ticotexas That’s my bad. A result of accidentally quoting the tilde, which prevents its expansion by the shell. – ozzy Jan 8 at 23:46
  • @ticotexas Oh, and as a matter of website etiquette, if you feel my answer actually answered your question/solved your problem, you may formally accept it, so as to sort of close it. – ozzy Jan 9 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.