ビング・壁紙


マイクロソフトのビングが検索エンジンの多くの人々の最初の選択でないかもしれない間.それは常に1つのことを行っている:毎日のホームページの壁紙.
しかし、あなたは彼らの壁紙のためだけにビングを使用しない場合はどうですか?私たちが正直であるなら、それは検索エンジンに固執する奇妙な理由です.それをあなたのブラウザーのホームページにすることに決めてください.
または多分、私のように、あなたはあまりにも怠惰と想像力のない自分の壁紙コレクションをキュレーションするしています.すべてのそれらの写真の保管を維持しないでください.そして、今日の壁紙とは何かを知らないから来ている生活の中で小さなスパイスのような?

The reason I wanted to find a way of getting these wallpapers, was to create my own simple browser homepage.


いずれかの方法は、私は見つけると、最新のビング毎日の壁紙をダウンロードする小さなスクリプトを通過されるように読んでください.その後、あなたはそれを好きなものを行うことができます.そしてうまくいけば、シェルスクリプトから達成することができますクールなもののいくつかについてもう少し学びます!

脚本


#!/bin/sh

bing="http://www.bing.com"
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-WW"

saveDir=$XDG_CACHE_HOME'/bing-wp/'
mkdir -p $saveDir
picName="bing.jpg"

picURL=$bing$(echo $(curl -s $xmlURL) | grep -oP "<url>(.*)</url>" | cut -d">" -f 2 | cut -d "<" -f 1)

curl -s -o $saveDir$picName $picURL

exit

Requirements: curl


破壊する


このシェルは、スクリプトを実行する際に使用するインタプリタを親シェルに伝えるだけです.
#!/bin/sh

Tells the interpreter to use your system's shell. Typically this will be either dash or bash.


ここでは、最新の壁紙のURLを作成するために使用する変数を設定しています.
bing="http://www.bing.com"
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-WW"
その後、我々はどこに画像が保存される場所を定義し、一度ダウンロードします.を使用してmkdir ユーティリティを使用すると、$saveDir . The -p オプションのフラグをここで使用する.bing-wp/ も作成されます.そして最後に変数picName が指定された場合は、イメージファイルを生成する.
saveDir=$XDG_CACHE_HOME'/bing-wp/'
mkdir -p $saveDir
picName="bing.jpg"

As every time this script is ran, it will write over the current image. $XDG_CACHE_HOME is used to denote the appropriate location for a temporary file, using the XDG Base Directory Specification. It merely represents in most *nix systems, the ~/.cache/ directory.


このネストされたパイプ変数の割り当てをより理解できる部分に分解できます.
picURL=$bing$(echo $(curl -s $xmlURL) | grep -oP "<url>(.*)</url>" | cut -d ">" -f 2 | cut -d "<" -f 1)
以前と同様に、これは単にBingのベースURLを変数に再割り当てします.
picURL=$bing
# picURL=http://www.bing.com
The curl 静かにコマンドを実行します.-s - 進行状況やエラー報告なしの意味xmlURL , コンテンツを出力します.
$(curl -s $xmlURL)
# <?xml version="1.0" encoding="utf-8" ?><images><image><startdate>20210817</startdate><fullstartdate>202108170000</fullstartdate><enddate>20210818</enddate><url>/th?id=OHR.PochuckValley_ROW7250263541_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg&amp;pid=hp</url><urlBase>/th?id=OHR.PochuckValley_ROW7250263541</urlBase><copyright>Stairway to Heaven trail, Wawayanda State Park, New Jersey, USA (© Leembe/Getty Images)</copyright><copyrightlink>https://www.bing.com/search?q=wawayanda+state+park+new+jersey&amp;form=hpcapt</copyrightlink><headline>Info</headline><drk>1</drk><top>1</top><bot>1</bot><hotspots></hotspots></image><tooltips><loadMessage><message>Loading...</message></loadMessage><previousImage><text>Previous image</text></previousImage><nextImage><text>Next image</text></nextImage><play><text>Play video</text></play><pause><text>Pause video</text></pause></tooltips></images>

As an example, this is what that resulting XML looks like, as of writing this article. We'll use it as an example to explain the rest of the script.


XMLのこの長い行の中に<url> and </url> タグ.日のビング壁紙URLパラメータを含むこと.この情報をcurl コマンド出力.その結果として生じる文字列をgrep .
grep -oP "<url>(.*)</url>"
# <url>/th?id=OHR.PochuckValley_ROW7250263541_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg&amp;pid=hp</url>

Using the -P flag to perform the pattern matching with Perl-compatible regular expressions. Combined with the -o option to only return the matching pattern, and not just highlight it.

(.*) just creates a capturing group of everything between the tags.


今、私たちは<url> and </url> リソース名とイメージアドレスのパラメータを囲むタグ.これは、このテキストをcut コマンド.
cut -d ">" -f 2
# /th?id=OHR.PochuckValley_ROW7250263541_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg&amp;pid=hp</url
cut -d "<" -f 1
# /th?id=OHR.PochuckValley_ROW7250263541_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg&amp;pid=hp

Using the delimiters, -d, ">" and "<" to separate the tags from the URL segment. And then selecting the delimiter separated fields, -f, to capture the URL.


イメージURLのこの断片は、それから連結されます$bing 変数使用echo .
picURL=$bing$(echo ...)

# picURL=http://www.bing.com/th?id=OHR.PochuckValley_ROW7250263541_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg&amp;pid=hp
最後に、この全体を置くと、我々は呼び出すことができますcurl 黙って-s 画像を取得する$picURL , 現在、日のイメージに向かっている.出力を指示する-o として保存するbing.jpg , インサイド$XDG_CACHE_HOME/bing-wp/ ディレクトリ.
curl -s -o $saveDir$picName $picURL

Remembering to exit the script at the bottom of the file.


スクリプトの実行オプション


私はここでそれを行う方法を得ることはできませんが、ちょうどあなたが今このスクリプトを実行し、最新のビングの壁紙を得ることができる方法についてのポインタとして.スクリプトを毎日実行する方法についていくつかのオプションがあります.
  • .xinitrc
  • システムタイマー
  • クロンジョブ
  • うまくいけば、ビングの最新の毎日の壁紙を得るためにこのチュートリアルを読んだ後、あなたは方法に沿ってシェルスクリプトについて何か新しいことを学んだ.最後まで読んでくれてありがとう、何か質問があれば下に聞いてください!
    もともと公開xhalford.com