毎日の壁紙スクリプトのwgetイメージの問題

毎日の壁紙スクリプトのwgetイメージの問題

wget特定のウェブサイトの画像を処理し、壁紙を設定するスクリプト(Nuno作成)があります。ただし、wgetサイトから画像を取得できるように変更することはできないようです。Chromecast

ソースの画像には次のURLがあります。ビデオ

grep欲が多いため、使用されたURLを分離できないようです。私はgrep -Pそれが動作することに気づきました。端末に手動で入力すると機能します。ちなみに、スクリプトから変数を代入しても動作しません。を使用して変数を設定できますが、grepを使用して設定することはできません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

ついています。

ベストアンサー1

スクリプトを少し変更する必要があります。

この行の後の2行は、#Set image url, name and locationさまざまな理由で破損しています(たとえば、誤ったgrep構文、誤った割り当て)。また、スクリプトはファイル名に二重引用符を使用しないため、状況によってはスクリプトが破損する可能性があります。

より安定して動作するために、スクリプトのコアを次のように書き直しました。

#!/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

さて、素敵な写真ですね!

おすすめ記事