Re: Factor

Factor: the language, the theory, and the practice.

Random Desktop Background

Monday, August 17, 2015


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 )
    "" 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-get nip
    R@ https://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 )
    scrape-html nip "wallpaper_thumb" find-by-class-between
    "a" find-by-name nip "href" attribute
    "" prepend scrape-html nip
    "the_view_link" find-by-id nip "href" attribute
    "https:" prepend scrape-html nip "myImage" find-by-id nip
    "src" attribute "https:" prepend ;

Using this is as easy as:

IN: scratchpad random-imgur

IN: scratchpad random-xkcd

IN: scratchpad random-wallpaperstock

This is available on my GitHub.