使用bash随机更换壁纸
本文要做的事情是写一个脚本,用来随机地从 reddit
的 wallpapers
中挑选一张壁纸应用到卓面上。
所需工具
- wget
- 下载URL和壁纸图片
- grep
- 从URL中抽取指向壁纸的URL
- feh
- 设置壁纸到桌面上
- shuf
- 产生随机效果
壁纸的来源
为了简便处理,我们只从 https://www.reddit.com/r/wallpapers/ 的第一页中随机选取壁纸。
实现步骤
用浏览器打开 https://www.reddit.com/r/wallpapers/ 你就会发现它列出了最新产生的25个帖子。
点开帖子就会显示出具体的壁纸内容来
所以我们要做的其实就是下面几步:
- 从 https://www.reddit.com/r/wallpapers/ 中抽取出帖子的链接
- 随机选择一个帖子链接
- 找出帖子链接中图片的URL
- 下载并设置壁纸
从 https://www.reddit.com/r/wallpapers/ 中抽取出帖子的链接
首先让我们用 wget
下载 https://www.reddit.com/r/wallpapers/ 页面来分析一下该如何抽取出帖子的链接呢。
wget https://www.reddit.com/r/wallpapers -O -
这条命令会下载 https://www.reddit.com/r/wallpapers/ 页面然后将内容显示在标准输出中。
由于内容太多,这里就不列出具体的内容了。我们先把网页中所有的链接过滤出来看看都有哪些内容.
wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |sort |uniq
href="/" href="http://redditgifts.com/" href="http://s1.1zoom.me/b5555/619/Ireland_Castles_Trim_castle_Night_Shrubs_538153_2560x1440.jpg" href="https://about.reddit.com" href="https://about.reddit.com/advertise/" href="https://about.reddit.com/careers/" href="https://about.reddit.com/press/" href="https://i.imgur.com/0eSNAHv.jpg" href="https://i.imgur.com/3tPGErY.jpg" href="https://i.imgur.com/hs7OcfD.jpg" href="https://i.imgur.com/pIYPQQZ.jpg" href="https://imgur.com/a/qLnxOv1" href="https://imgur.com/a/zR2iLXL" href="https://reddit.com/message/compose?to=/r/wallpapers" href="https://www.reddit.com/gold/about" href="https://www.reddit.com/help/contentpolicy" href="https://www.reddit.com/help/healthycommunities/" href="https://www.reddit.com/help/privacypolicy" href="https://www.reddit.com/help/useragreement" href="https://www.reddit.com/login?redirect=https%3A%2F%2Fwww.reddit.com%2Fr%2Fwallpapers%2F" href="https://www.reddit.com/mobile/download" href="https://www.reddit.com?redirect=https%3A%2F%2Fwww.reddit.com%2Fr%2Fwallpapers%2F" href="https://www.reddit.com/register?redirect=https%3A%2F%2Fwww.reddit.com%2Fr%2Fwallpapers%2F" href="https://www.reddit.com/r/wallpapers/" href="https://www.reddit.com/r/wallpapers/?after=t3_8e2rq7" href="https://www.reddit.com/r/wallpapers/comments/8e2rq7/rush_downtown_thebadbadben_2732x1536/" href="https://www.reddit.com/r/wallpapers/comments/8e7k5i/ghost_in_the_shell/" href="https://www.reddit.com/r/wallpapers/comments/8eaglp/tripy_leaf_wallpaper_edited_from_an_unsplash_image/" href="https://www.reddit.com/r/wallpapers/comments/8eapkd/beautiful_purple_sunset_1920x180/" href="https://www.reddit.com/r/wallpapers/comments/8ebjj0/credit_to_whoever_took_this_picture_i_forget/" href="https://www.reddit.com/r/wallpapers/comments/8ebyo5/another_whale_seen_in_the_sky_2880x1800/" href="https://www.reddit.com/r/wallpapers/comments/8ebyvm/snowy_hills_3840_2160/" href="https://www.reddit.com/r/wallpapers/comments/8edl5j/vaporwave_hills_3840x2160/" href="https://www.reddit.com/r/wallpapers/comments/8efq4z/3d_city_glitch_1920x1080/" href="https://www.reddit.com/r/wallpapers/comments/8eica4/my_cat/" href="https://www.reddit.com/r/wallpapers/comments/8eiuhx/imperial_walkers_dawn_darek_zabrocki_3235x1820/" href="https://www.reddit.com/r/wallpapers/comments/8ej0ev/kratos_1366x768/" href="https://www.reddit.com/r/wallpapers/comments/8ej8np/in_the_woods_the_blue_gloom_m_yakovlev_4373x2460/" href="https://www.reddit.com/r/wallpapers/comments/8ek3ab/sr71_amidst_the_sunset_3072x1728/" href="https://www.reddit.com/r/wallpapers/comments/8ekaf2/tokyo_rose_vice_2412x1357/" href="https://www.reddit.com/r/wallpapers/comments/8ekb2n/another_personal_favorite_cerebrum_by_tomasz/" href="https://www.reddit.com/r/wallpapers/comments/8ekqe8/zettai_ryouiki_outrun_a_sr71_colonel_cbplayer/" href="https://www.reddit.com/r/wallpapers/comments/8ekwjg/boardwalk_1920_1080/" href="https://www.reddit.com/r/wallpapers/comments/8elgzr/venom_1688x1076/" href="https://www.reddit.com/r/wallpapers/comments/8enilg/lighthouse_2560x1440/" href="https://www.reddit.com/r/wallpapers/comments/8ep7de/trim_castle_ireland_2500_x_1440/" href="https://www.reddit.com/r/wallpapers/comments/8epjah/nekopara_vol_0_sfw_1920x1202/" href="https://www.reddit.com/r/wallpapers/comments/8er6uk/apollo_17_earths_moon_2560x1440/" href="https://www.reddit.com/r/wallpapers/comments/8ess6s/planet_surfaces/" href="https://www.reddit.com/r/wallpapers/comments/8et1sk/stars_space_galaxy_1920x1080/" href="https://www.reddithelp.com" href="https://www.redditstatic.com/desktop2x/Commons.a1e39474b980de9725ef.js" href="https://www.redditstatic.com/desktop2x/fonts/IBMPlexSans/Medium-c4b185e25a4dde85a29f902cd5ce5360.woff2" href="https://www.redditstatic.com/desktop2x/fonts/IBMPlexSans/Regular-116bb6d508f5307861d3b1269bc597e7.woff2" href="https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-114x114.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-120x120.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-144x144.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-152x152.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-180x180.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-57x57.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-60x60.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-72x72.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-76x76.png" href="https://www.redditstatic.com/desktop2x/img/favicon/favicon-16x16.png" href="https://www.redditstatic.com/desktop2x/img/favicon/favicon-32x32.png" href="https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png" href="https://www.redditstatic.com/desktop2x/img/favicon/manifest.json" href="https://www.redditstatic.com/desktop2x/overlap-2c2efe7e95ba8fc991854e8f01322243.575c42d246b39a3a03e6.js" href="https://www.redditstatic.com/desktop2x/overlap-44e3d536a182a3fe6c9eed5014e47b05.08362281f54b63e55856.js" href="https://www.redditstatic.com/desktop2x/overlap-617df21ac848fad21f5733296c4f91a1.e7da3e4bc28c59fb8ef1.js" href="https://www.redditstatic.com/desktop2x/overlap-9fc0f7a006faf347e6dc6a1670421fb0.6d15f53ff4128ba2c408.js" href="https://www.redditstatic.com/desktop2x/overlap-de8e488af5f659d52a5c321db16a89a5.f1cd898dc044a4d81f48.js" href="https://www.redditstatic.com/desktop2x/overlap-fc592e99099d1804afeb090c87b920c0.6a46ccff54a4bc09e93c.js" href="https://www.redditstatic.com/desktop2x/ProductionVendorAux.b93c16e76c66f5775359.js" href="https://www.redditstatic.com/desktop2x/ProductionVendor.f98cdff1b7e8856c5548.js" href="https://www.redditstatic.com/desktop2x/Reddit.3ae165fca313f57c16bb.js" href="http://www.redditblog.com/" href="/r/all" href="/r/popular" href="/r/wallpapers/" href="/r/wallpapers" href="/r/wallpapers/comments/8e2rq7/rush_downtown_thebadbadben_2732x1536/" href="/r/wallpapers/comments/8e7k5i/ghost_in_the_shell/" href="/r/wallpapers/comments/8eaglp/tripy_leaf_wallpaper_edited_from_an_unsplash_image/" href="/r/wallpapers/comments/8eapkd/beautiful_purple_sunset_1920x180/" href="/r/wallpapers/comments/8ebjj0/credit_to_whoever_took_this_picture_i_forget/" href="/r/wallpapers/comments/8ebyo5/another_whale_seen_in_the_sky_2880x1800/" href="/r/wallpapers/comments/8ebyvm/snowy_hills_3840_2160/" href="/r/wallpapers/comments/8edl5j/vaporwave_hills_3840x2160/" href="/r/wallpapers/comments/8efq4z/3d_city_glitch_1920x1080/" href="/r/wallpapers/comments/8eica4/my_cat/" href="/r/wallpapers/comments/8eiuhx/imperial_walkers_dawn_darek_zabrocki_3235x1820/" href="/r/wallpapers/comments/8ej0ev/kratos_1366x768/" href="/r/wallpapers/comments/8ej8np/in_the_woods_the_blue_gloom_m_yakovlev_4373x2460/" href="/r/wallpapers/comments/8ek3ab/sr71_amidst_the_sunset_3072x1728/" href="/r/wallpapers/comments/8ekaf2/tokyo_rose_vice_2412x1357/" href="/r/wallpapers/comments/8ekb2n/another_personal_favorite_cerebrum_by_tomasz/" href="/r/wallpapers/comments/8ekqe8/zettai_ryouiki_outrun_a_sr71_colonel_cbplayer/" href="/r/wallpapers/comments/8ekwjg/boardwalk_1920_1080/" href="/r/wallpapers/comments/8elgzr/venom_1688x1076/" href="/r/wallpapers/comments/8enilg/lighthouse_2560x1440/" href="/r/wallpapers/comments/8ep7de/trim_castle_ireland_2500_x_1440/" href="/r/wallpapers/comments/8epjah/nekopara_vol_0_sfw_1920x1202/" href="/r/wallpapers/comments/8er6uk/apollo_17_earths_moon_2560x1440/" href="/r/wallpapers/comments/8ess6s/planet_surfaces/" href="/r/wallpapers/comments/8et1sk/stars_space_galaxy_1920x1080/" href="/r/wallpapers/controversial/" href="/r/wallpapers/hot/" href="/r/wallpapers/new/" href="/r/wallpapers/rising/" href="/r/wallpapers/submit" href="/r/wallpapers/top/" href="/user/acoolrocket" href="/user/Aileos" href="/user/Arcade23" href="/user/BradFMB" href="/user/campymountain" href="/user/Clinicalyabrasiv" href="/user/CreamSodaFloat" href="/user/Donutsen" href="/user/douglasrcjames" href="/user/emem01" href="/user/hero0fwar" href="/user/KorvisKhan" href="/user/MrAwkwardCrotch" href="/user/nothingsaidalice" href="/user/pakaau" href="/user/PiekarnikCiepla" href="/user/pmaxm00" href="/user/sequence_string" href="/user/T3DS" href="/user/T-D-S" href="/user/ThatOnePhotog" href="/user/thmanwithnoname" href="/user/thmodwithnoname" href="/user/xypnox" href="/user/zaron5551"
你会发现结果中包含了很多乱七八糟的链接。我们还有必要做进一步的过滤。
通过分析这些帖子的URL,可以看出它们都是以 https://www.reddit.com/r/wallpapers/comments
开头的。
因此我们试着用 grep
将符合这个条件的URL过滤出来看看效果怎么样
wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/'
https://www.reddit.com/r/wallpapers/comments/8efq4z/3d_city_glitch_1920x1080/ https://www.reddit.com/r/wallpapers/comments/8e7k5i/ghost_in_the_shell/ https://www.reddit.com/r/wallpapers/comments/8ebjj0/credit_to_whoever_took_this_picture_i_forget/ https://www.reddit.com/r/wallpapers/comments/8ebyvm/snowy_hills_3840_2160/ https://www.reddit.com/r/wallpapers/comments/8eapkd/beautiful_purple_sunset_1920x180/ https://www.reddit.com/r/wallpapers/comments/8edl5j/vaporwave_hills_3840x2160/ https://www.reddit.com/r/wallpapers/comments/8ebyo5/another_whale_seen_in_the_sky_2880x1800/ https://www.reddit.com/r/wallpapers/comments/8eaglp/tripy_leaf_wallpaper_edited_from_an_unsplash_image/ https://www.reddit.com/r/wallpapers/comments/8e2rq7/rush_downtown_thebadbadben_2732x1536/ https://www.reddit.com/r/wallpapers/comments/8efhr9/all_our_gods_have_abandoned_us_architectsuk/ https://www.reddit.com/r/wallpapers/comments/8e3qbh/remember_this_always_loved_this_wallpaper_from/ https://www.reddit.com/r/wallpapers/comments/8e4ni5/long_past_civlization_4000_2349/ https://www.reddit.com/r/wallpapers/comments/8e6281/bus/ https://www.reddit.com/r/wallpapers/comments/8e2c57/fluid_mechanics_beeple_3840x2160/ https://www.reddit.com/r/wallpapers/comments/8e5j95/tulips_at_5_am_1920x1280/ https://www.reddit.com/r/wallpapers/comments/8e3r13/a_deer_in_the_forest_1920x1080/ https://www.reddit.com/r/wallpapers/comments/8e62jt/froggo/ https://www.reddit.com/r/wallpapers/comments/8e4xv5/abstract_swirls_2560x1600/ https://www.reddit.com/r/wallpapers/comments/8dvuro/samurai_spiral_1536_x_864/ https://www.reddit.com/r/wallpapers/comments/8e1j0g/blossom_wallpaper_shot_by_me/ https://www.reddit.com/r/wallpapers/comments/8dy02y/lagoon_nebula/ https://www.reddit.com/r/wallpapers/comments/8e4t9z/wallpaper_for_multi_screen_setup/ https://www.reddit.com/r/wallpapers/comments/8do356/living_tree_4000_2400/ https://www.reddit.com/r/wallpapers/comments/8dviei/ruined_canvas/ https://www.reddit.com/r/wallpapers/comments/8dvzwe/infinity_gauntlet_3840x2160/
过滤结果非常好!
随机选择一个链接
要随机选择链接,我们可以使用 shuf
命令来打乱链接的顺序,然后选择第一个链接作为我们要访问的链接就行了
wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/' |shuf -n 1
找出帖子链接中图片的URL
按照之前的方法,我们来看看帖子链接中都有哪些链接。不过由于我们只需要图片,因此可以加个限制只显示包含 .jpg
, .jpeg
, 或者 .png
的URL.
url=$(wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/' |shuf -n 1) wget ${url} -O - |grep -o -E 'href="([^"#]+)"' |grep -E "\.jpg|\.jpeg|\.png"
href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-57x57.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-60x60.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-72x72.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-76x76.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-114x114.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-120x120.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-144x144.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-152x152.png" href="https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-180x180.png" href="https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png" href="https://www.redditstatic.com/desktop2x/img/favicon/favicon-32x32.png" href="https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png" href="https://www.redditstatic.com/desktop2x/img/favicon/favicon-16x16.png" href="https://i.redditmedia.com/Ii0AWUY-N8ibB4bNA0g20PPlNcSc8-nC3kQBlXiT3UA.jpg?s=fb92a5cd0a6d82cb423f87bd3f876b27" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1920×1080.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1600×900.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1366×768.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1280×720.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1280×800.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1024×768.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-1280×1024.jpg" href="https://resi.ze-robot.com/dl/3d/3d-city-glitch-768×1024.jpg" href="https://i.redd.it/yt2lweavmqt01.jpg"
其中 redditstatic
中的图片是reddit本身的那些图片,而 resi.ze=robot.com
中的图片是提供下载的不同尺寸的地址,我们过滤掉这两个地址
url=$(wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/' |shuf -n 1) wget ${url} -O - |grep -o -E 'href="([^"#]+)"' |grep -E "\.jpg|\.jpeg|\.png" |grep -v -E "www.redditstatic.com|resi.ze-robot.com"
href="https://i.imgur.com/0eSNAHv.jpg" href="https://i.imgur.com/0eSNAHv.jpg" href="https://i.imgur.com/0eSNAHv.jpg" href="https://i.imgur.com/nFZpEAo.png" href="https://i.imgur.com/0eSNAHv.jpg"
还可能剩下多张图片,我们再用 shuf
来随机选择其中一张
url=$(wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/' |shuf -n 1) wget ${url} -O - |grep -o -E 'href="([^"#]+)"' |grep -E "\.jpg|\.jpeg|\.png" |grep -v -E "www.redditstatic.com|resi.ze-robot.com" |shuf -n 1
我们把URL从 href
中抽取出来
url=$(wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/' |shuf -n 1) wget ${url} -O - |grep -o -E 'href="([^"#]+)"' |grep -E "\.jpg|\.jpeg|\.png" |grep -v -E "www.redditstatic.com|resi.ze-robot.com" |shuf -n 1 |grep -o -E 'http[^"]+'
下载并设置壁纸
我们使用wget来下载壁纸,并通过feh来设置壁纸。
不过有一点需要注意的是,我们上一步中假设壁纸是以 .jpg
, .jpeg
, 或者 .png
结尾的。
那如果壁纸不是以这三个后缀结尾怎么办?一个简单的办法就是重新再随机选择一次。
#!/bin/bash function get_image_url() { url=$(wget https://www.reddit.com/r/wallpapers -O - |grep -o -E 'href="([^"#]+)"' |grep -o -E 'https://www.reddit.com/r/wallpapers/comments/[^"]+/' |shuf -n 1) wget ${url} -O - |grep -o -E 'href="([^"#]+)"' |grep -E "\.jpg|\.jpeg|\.png" |grep -v -E "www.redditstatic.com|resi.ze-robot.com" |shuf -n 1 |grep -o -E 'http[^"]+' } image_url=$(get_image_url) while [[ -z "${image_url}" ]];do image_url=$(get_image_url) done wget ${image_url} -O - |feh --bg-scale -