As a follow-up to my Desktop Background post, I wanted to show how to set your desktop background to random images from various online image sites. We will be downloading a URL to a local file and then setting the desktop picture to that file:
: download-and-set-desktop-picture ( url -- ) dup "/" split1-last nip cache-file [ download-to ] [ set-desktop-picture ] bi ;
Okay, now we need some random images!
Imgur is a huge image-hosting website frequently used on sites like Reddit.
: random-imgur ( -- url ) "https://imgur.com/random" scrape-html nip "image_src" "rel" find-by-attribute-key-value first "href" attribute ;
XKCD has some fun comics. Maybe they would look good on the desktop!
: random-xkcd ( -- url ) "http://dynamic.xkcd.com/random/comic/" http-get nip R@ http://imgs\.xkcd\.com/comics/[^\.]+\.(png|jpg)@ first-match >string ;
WallpaperStock has a bunch of more traditional desktop images. We will scrape their random wallpaper page, find the first wallpaper thumbnail, load that wallpaper page, find the default image page, and then load that to find the image URL.
: random-wallpaperstock ( -- url ) "http://wallpaperstock.net/random-wallpapers.html" scrape-html nip "wallpaper_thumb" find-by-class-between "a" find-by-name nip "href" attribute "http://wallpaperstock.net" prepend scrape-html nip "the_view_link" find-by-id nip "href" attribute "http:" prepend scrape-html nip "myImage" find-by-id nip "src" attribute "http:" prepend ;
Using this is as easy as:
IN: scratchpad random-imgur download-and-set-desktop-picture IN: scratchpad random-xkcd download-and-set-desktop-picture IN: scratchpad random-wallpaperstock download-and-set-desktop-picture
This is available on my GitHub.
No comments:
Post a Comment