i3wm setup with amazing gruvbox color scheme and a wallpaper from Reddit

Update [2019/05/08]: Many people have been asking for the wallpaper in the above screenshot. It is from System Failure II, oil on canvas, 31x43” on r/Art.

Well, I am really fascinated by Reddit art and genuine creative ideas such as Scrolller which was made possible thanks to gazillions of art pieces scattered throughout various art subreddits. I am also fascinated by Unix philosophy and have been a *nix enthusiast for as long as I can remember. In addition to all this, the discovery of r/unixporn - realizing I am not the only one who cares about aesthetics of their Unix box - was a huge blow for me; to the point that studying the GitHub dotfiles posted along the screenshots on r/unixporn by fellow nix-enthusiast redditors felt like a day to day hubby for me.

All the while, I had a successful experiment with writing a complex piece of real-world software in pure Bash with an amazingly wide range of features for around 3.5K lines of code. The real excitement came when it made to the official FreeBSD Ports Tree. In spite of the fact that many people find Bash syntax annoyingly ugly and unmaintainable and often wonder why do people still write shell scripts by asking it on Quora, since MS-DOS 6.22 era, I did develop a certain love–hate relationship with shell scripting languages such as Batch Files, Bash, etc. Thus, still I do automate almost everything with these ancient technologies.

So, here is my fully-configurable wallpaper changer software written in bash which automagically fetches and display wallpapers from your favorite subs. It has been powering and brightening up my i3wm setup for the past eight months which led me to the conclusion that it deserves a proper introduction.

Requirements and Dependencies

Taking a look at the source code reveals the necessitas for running the script successfully:

readonly BASENAME = "basename" readonly CALLER = "caller" readonly CUT = $( which cut 2>/dev/null ) z readonly CURL = $( which curl 2>/dev/null ) readonly DATE = $( which date 2>/dev/null ) readonly ECHO = "echo" readonly ECHO_FMT = "echo -e" readonly ESETROOT = $( which esetroot 2>/dev/null ) readonly FEH = $( which feh 2>/dev/null ) readonly FIREFOX = $( which firefox 2>/dev/null ) readonly HSETROOT = $( which hsetroot 2>/dev/null ) readonly JQ = $( which jq 2>/dev/null ) readonly LOGGER = "logger" readonly PERL = $( which perl 2>/dev/null ) readonly PRINT = "print" readonly REV = $( which rev 2>/dev/null ) readonly TR = $( which tr 2>/dev/null ) readonly XSETROOT = $( which xsetroot 2>/dev/null )

Most of the dependencies in this list are either internal shell commands or are already present on a nix system such as *BSDs or common GNU/Linux systems. Otherwise, it must be installed from your distro’s package manager. The ones that may not be present by default are curl , feh , and jq . Between esetroot , hsetroot and xsetroot only one of them is required since they could be used interchangeably. Firefox is not a hard-dependency and can be avoided at cost of a warning:

[ WARNING ] 288 Firefox executable not found! [ WARNING ] 290 Setting Firefox version string to: Mozilla Firefox 66.0

Firefox is being used to fake the user agent while downloading meta data or actual images from Reddit. If the script won’t be able to find it, it sets the version number to the latest version of Firefox which at the time of this writing is: Mozilla Firefox 66.0 .

Usage Syntax

Before running the script, please note that it must has the executable permission set on it. If not, in order to grant executable permission for all users:

$ chmod a+x /path/to/reddit-wallpaper.sh

Or, only the current user (the user who owns the file):

$ chmod u+x /path/to/reddit-wallpaper.sh

Or, the users under the group who owns the file:

$ chmod g+x /path/to/reddit-wallpaper.sh

Getting away from the basics, one can simply run the script by issuing the following command and it will download and set a wallpaper from Reddit for you, since it assumes some defaults in case it’s not given the required arguments:

$ /path/to/reddit-wallpaper.sh [ INFO ] 304 Run 'reddit-wallpaper.sh -h' for more information on available options. [ INFO ] 306 Setting user agent to 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0' ... [ INFO ] 310 Downloading meta file 'https://www.reddit.com/r/Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture+ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology/hot.json' ... [ INFO ] 381 Found a wallpaper on 'r/Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture+ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology' at 'https://cdnb.artstation.com/p/assets/images/images/017/756/697/large/jon-juarez-caramanchada3-800.jpg' ! [ INFO ] 382 Fetching 'https://cdnb.artstation.com/p/assets/images/images/017/756/697/large/jon-juarez-caramanchada3-800.jpg' ... [ INFO ] 392 Setting desktop background color to '#282828' ... [ INFO ] 402 Using 'https://cdnb.artstation.com/p/assets/images/images/017/756/697/large/jon-juarez-caramanchada3-800.jpg' as the desktop wallpaper... [ INFO ] 412 Done! [ INFO ] 413 Hope you enjoy it : )

So far so good! Now, the correct usage syntax for the script is as follows:

Correct usage: reddit-wallpaper.sh -h | [-r {r/subreddit}] [-s {sorty by}] [-n] [-b {background color}] [-f 'feh args'] -h: shows this usage note -r: subreddit name or names prefixed with r/ and combined by a + sign (e.g. r/Art or r/Art+ArtPorn; default: r/Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture+ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology) -s: reddit sort algorithm (e.g. hot, new, controversial, top, rising; default: hot) -n: allow nsfw wallpapers (no nsfw wallpaper is allowed by default, unless this flag is passed) -b: hex rgb color in 'ffffff' format (default: 282828) -f: feh arguments to pass; run 'man feh' for a list of available options (default: --no-fehbg --image-bg black --bg-max)

One can always get this information by utilizing -h option:

$ /path/to/reddit-wallpaper.sh -h

Here is the detailed explanation for the rest of the options:

-r : A typical sub-reddit url looks like https://reddit.com/r/SomeCoolSubRedditA or https://reddit.com/r/SomeCoolSubRedditB . If one desires, they may mix-up or simply join various subs by concatenating them; e.g., https://reddit.com/r/SomeCoolSubRedditA+SomeCoolSubRedditB+SomeCoolSubRedditC . This is one of the coolest features unique to Reddit among the social media. In the -r parameter the user should specify the name of the sub-reddit prefixed with r/ without the https://reddit.com/ part of the URL; e.g. r/SomeCoolSubRedditA or r/SomeCoolSubRedditA+SomeCoolSubRedditB+SomeCoolSubRedditC . For starters, one can take a look at the script’s source code which provides a list consisting of some of the best subreddits in various categories such as Animals , Art , Food , Imaginary , Man-made , and Nature . By default the script uses everything under the Art category. If one desires to find more subreddits they could navigate to Scrolller and after clicking the gear icon on the top-right corner, reveal the category information by enabling the Category info option which not only lists the top subreddits but also provides a list of sub-reddits in various sub-categories for the chosen category. There is also a NSFW (Not Safe For Work) category on Scrolller which is not included in the script since many are not comfortable with it

-s : Specifies the Reddit’s sort algorithm for the chosen subreddit(s). The valid values for this parameter are hot , new , controversial , top , and rising . Reddit’s default is hot . So, if not specified by the user, the script chooses hot as the default for for -s parameter. This is important since the script chooses the first wallpaper from the sorted list. As an example, if someone goes with top option, the wallpaper they get may not change for days or even months, even if they run the script one million times. Or, if the new option is chosen you may get a different wallpaper on each run of the script as it fetches the latest uploaded images by redditors, but it might not be much of an interesting image.

readonly SUBREDDIT_CATEGORY_ANIMALS = "AnimalsBeingBros+AnimalsBeingDerps+AnimalsBeingJerks+aww+Eyebleach+likeus+rarepuppers" readonly SUBREDDIT_CATEGORY_ART = "Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture" readonly SUBREDDIT_CATEGORY_FOOD = "Breadit+eatsandwiches+food+FoodPorn+grilledcheese+Pizza+slowcooking" readonly SUBREDDIT_CATEGORY_IMAGINARY = "ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology" readonly SUBREDDIT_CATEGORY_MAN_MADE = "AbandonedPorn+carporn+CityPorn+CozyPlaces+DesignPorn+powerwashingporn+RoomPorn" readonly SUBREDDIT_CATEGORY_NATURE = "chemicalreactiongifs+EarthPorn+MacroPorn+physicsgifs+spaceporn+waterporn+WeatherGifs"

-n : The script won’t download any NSFW (Not Safe For Work) image by default unless this flag is passed in the parameter list.

-b : Specifies the background color so in case that the downloaded image size does not match your screen size exactly, the extra space around the picture is going to be filled with this color instead. This parameter expects a color in HEX RGB format without the # character. For example, for red or magenta color one could pass ff0000 , or ff00ff respectively.

-f Anything passed as this parameter’s value will be directly passed to feh . feh is an amazing tool with a lot of features. It also supports multi-display setups. As a sane default which works in both single and multi-head setups, the script passes along –no-fehbg –image-bg black –bg-max arguments to feh by default if this parameter is not specified. For more information, consult the feh man page by entering the following command:

$ man feh

For the convenience of description, the following example tries to fetch the most recent picture submitted to any of r/Art , r/ImaginaryLandscapes , r/ImaginaryMonsters , or r/itookapicture subs. It does not care if it is safe for work or not (e.g. containing nudity, violence, …). It fills the extra space around the wallpaper to black if there are any. And, finally it tells feh to not write a ~/.fehbg file and center the wallpaper on the background. Notice that we did wrap feh parameters in a pair of double quotes, otherwise the script stops with an error complaining about the invalid options it was given.

$ /path/to/reddit-wallpaper.sh \ -r r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture \ -s new \ -n \ -b "000000" \ -f "--no-fehbg --bg-center" [ INFO ] 308 Run 'reddit-wallpaper.sh -h' for more information on available options. [ INFO ] 310 Setting user agent to 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0' ... [ INFO ] 314 Downloading meta file 'https://www.reddit.com/r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture/new.json' ... [ INFO ] 385 Found a wallpaper on 'r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture' at 'https://i.redd.it/w38h6jmwa1x21.jpg' ! [ INFO ] 386 Fetching 'https://i.redd.it/w38h6jmwa1x21.jpg' ... [ INFO ] 396 Setting desktop background color to '#000000' ... [ INFO ] 406 Using 'https://i.redd.it/w38h6jmwa1x21.jpg' as the desktop wallpaper... [ INFO ] 416 Done! [ INFO ] 417 Hope you enjoy it : )

Running through a Cron Job

I have already wrote a guide on how to properly add a cron job on *nix systems, so I won’t go through this in details. Fire up the crontab file in your favorite editor by issuing:

$ sudo -u user -g group -H crontab -e

In my case, the user and the group for my account is mamadou ; so it would be:

$ sudo -u mamadou -g mamadou -H crontab -e

I am running the script every 4 hours:

# At minute 0 past every 4th hour 0 */4 * * * export DISPLAY = :0; /path/to/reddit-wallpaper.sh > /dev/null 2>& 1

If you are not familiar with the crontab syntax, crontab.guru is a great visual aid.

Note that the export DISPLAY=:0; part before calling the script is mandatory since we are going to run our script through a cron job. If one forgets to export the correct display before running the script from the crontab, they will get the following error:

Cannot open X display!

On another note, due to the fact that this script is designed to run as a cron job, in addition to stdout and stderr , the scripts logs are getting passed through to the system’s log file. On my Gentoo or FreeBSD instances this file is located at /var/log/messages . This is a sample system log output taken from the example in the previous section:

$ tail -f /var/log/messages May 8 20:57:44 mamadou-pc REDDIT-WALLPAPER: INFO 308 Run 'reddit-wallpaper.sh -h' for more information on available options. May 8 20:57:44 mamadou-pc REDDIT-WALLPAPER: INFO 310 Setting user agent to 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0' ... May 8 20:57:44 mamadou-pc REDDIT-WALLPAPER: INFO 314 Downloading meta file 'https://www.reddit.com/r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture/new.json' ... May 8 20:57:45 mamadou-pc REDDIT-WALLPAPER: INFO 385 Found a wallpaper on 'r/Art+ImaginaryLandscapes+ImaginaryMonsters+itookapicture' at 'https://i.redd.it/w38h6jmwa1x21.jpg' ! May 8 20:57:45 mamadou-pc REDDIT-WALLPAPER: INFO 386 Fetching 'https://i.redd.it/w38h6jmwa1x21.jpg' ... May 8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 396 Setting desktop background color to '#000000' ... May 8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 406 Using 'https://i.redd.it/w38h6jmwa1x21.jpg' as the desktop wallpaper... May 8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 416 Done! May 8 20:57:47 mamadou-pc REDDIT-WALLPAPER: INFO 417 Hope you enjoy it : )

Wallpaper Cache Location

When the scripts finds and fetches an image from Reddit successfully it saves the wallpaper in the wallpaper cache located inside the user’s home directory located at ~/.cache/reddit_wallpapers in the YEAR-MONTH-DAY-HOUR-MINUTE-SECONDS.EXTENSION format; e.g. 2019-05-08-04-00-01.jpg . For obvious reasons this cache grows over time, thus, one should take care of cleaning it up manually, themselves.

In the future releases, I may add various clean up options and strategies, but for the time being I am happy with keeping the cache and removing it by hand after making a copy whenever I have to.

Obtaining the Source Code

The source code is available on both GitHub and GitLab for the sake of convenience. In order to download the source code using curl , aria2 , wget directly:

# GitHub $ curl -fLo /path/to/reddit-wallpaper.sh \ --create-dirs \ https://raw.githubusercontent.com/NuLL3rr0r/reddit-wallpaper/master/reddit-wallpaper.sh # GitLab $ curl -fLo /path/to/reddit-wallpaper.sh \ --create-dirs \ https://gitlab.com/NuLL3rr0r/reddit-wallpaper/raw/master/reddit-wallpaper.sh

It is also possible to obtain the whole repository by cloning it from git :

# GitHub $ git clone \ https://github.com/NuLL3rr0r/reddit-wallpaper.git \ /path/to/clone/reddit-wallpaper # GitLab $ git clone \ https://gitlab.com/NuLL3rr0r/reddit-wallpaper.git \ /path/to/clone/reddit-wallpaper

Alternatively, it can be copy-pasted directly from here, which is strongly discouraged due to Pastejacking Exploitation Technique: