Not that long ago i started reading about ruby, in the hopes
of finally finding a scripting language that i could
learn, one that is simple, clean and with a beautiful syntax.
During the learning process I was looking for small projects,
that could help solidify my understanding. Searching online was
pretty frustrating since people were always recommending rails projects
and that was not what i had in mind.
I love wallpapers, i’m always on the favorites section of
wallhaven and i change it daily.
Imaginy does exactly that but better and faster,
by parsing the favorites section it downloads a new image daily and sets it as my background picture,
super simple stuff.
My custom url (screen resolution, SFW) is constructed as so :
with a default of 25 images per page.
We need a way of storing dynamic data like the image index to download, the image page index
and also static information like the website url, the name of the folder where images are stored.
Obtain the url of the current image.
Download it to the image folder.
Set it as a background image.
Update our dynamic data.
First of all we need a configuration class that will store all the data and is capable
of exporting it to a file config.data for future use,
We must assure that if the user deletes this file, our script won’t crash :)
For our export format we will use yaml, a simple language that describes data.
By using the yaml format we can manipulate our config file really easily.
Internally we will use a hash table:
All the fields are self-explanatory, except maybe the cmd one, we will explain it in the final section.
This is the config.data file, once the configuration class has been exported.
So now we have our default configuration, how do we export it?
Simple, by using the yaml library.
In order to read the config file we do as follows :
If you want to understand how all of this fits toghether, check the MyConfig class.
The image manager is where all the action happens :), we get to play with
nokogiri, a html parser.
If you never used nokogiri or any other html parser don’t be worried, it’s super easy.
You can check the provided link to get familiar with the parser and you should also check the html code of the pages
that we want to parse
First we must obtain the urls of all the images on the page :
For the first page we obtain this html code, containing all the 25 image urls on this page :
The next step is to choose the current image index and download it locally like so :
Now that we have our image on the hdd, we must set it as a background.
We could just execute a shell command like gsettings for Gnome3 or mateconftool for Mate, but those are desktop environment dependent.
The easy way out is using feh which is shipped with many linux distributions
including Arch Linux, Debian, FreeBSD, Gentoo, OpenBSD and Ubuntu.
Now that mystery line in our config file makes sense :
this is a part of the command that is executed in order to change the background image, all that’s missing
is the image path.
After that the only thing left is to update our config file :
That’s it! All we have to do is set this bad-boy running on system startup and
enjoy our new wallpaper.
I hope this article was useful, if you want to check/fork/star the github repo you can do that here.
If you have any questions, suggestions leave a comment bellow.