Oct 23 2007
rComic: comic strip downloader
rComic is a small script to download and display Internet comic strips. To add new strips, you only need to modify the config file. And it is an interesting exercise to play with the Net::HTTP and YAML libraries.
rComic makes use of two external programs: wget and display for downloading and displaying the image. Both tools are available as packages in all major distros (look for imagemagick). Get the code and let’s get it started.
To store the information of our comic strips we will be using YAML:
The YAML library serializes and deserializes Ruby object trees to and from and external, readable, plain-text format.
In the config file (rcomic.yaml) every comic strip definition will have the following appearance:
xkcd: desc: A webcomic of romance, sarcasm, math, and language. host: www.xkcd.com path: / rexp: <img>
You need a key word that will be used to refer to the strip and a set of configuration parameters, the host name, the path inside the server and a regular expression to identify the desired image.
In order to add a new strip, you only need to append a block like the one above to your configuration file.
Three steps are performed in the script: command line parsing, HTTP connection and download of the page, image download and display. In order to know what strip are we working on, first we need to load the configuration file as show:-
#load configuration
config = YAML.load_file('rcomic.yaml')Then some simple logic determines if the requested strip (the first argument provided) is configured in the .yaml file. If no reference to the key word is found in the YAML file, a help message is displayed. Otherwise, we carry on with the next steps:
# prepare an HTTP connection http = Net::HTTP.new($host) # get the page response = http.get($path) # scan for the image img = response.body.scan($rexp).first.first file = File.basename(img)
First we request the page and then we apply the regular expression to the body of the HTML returned. The img variable will contain the full URL to the image (i.e. http://imgs.xkcd.com/comics/gyroscopes.png) and the file will contain only the file name (i.e. gyroscopes.png).
With this information is dead easy to download and display the images:
# download (if not already downloaded)
unless File.exist?("/tmp/#{file}")
`wget -O /tmp/#{file} #{img}`
end
# display
`display /tmp/#{file}`As a side note, we will only download the file if the file is not already present in our /tmp/ folder.
Happy comics ![]()




