How do I... Fetch the daily Dilbert comic strip?

Table of contents
  1. 1. How it works
  2. 2. Other web comics

The Dilbert comic strip is available by RSS, with an inline image for every day.

We can use DekiScript to parse out the topmost entry, which is the entry for today.

<pre class="script">
var src = string.match(web.text('http://feedproxy.google.com/DilbertDailyStrip', 'channel/item/description'), 'src="([^"]+)"');
if(src)
    web.image(src);
else
    "Sorry, no Dilbert today. :(";
</pre>
<p>&nbsp;</p>

Next: Use the template.

{{ DailyDilbert() }}

And see the output:

 

How it works

The web.text function takes a parameter of an xpath query, which can be used to drill down into the RSS feed.

web.text('http://feedproxy.google.com/DilbertDailyStrip', 'channel/item/description')

This will return us:

<channel>
    <item>
        <description>
            anything contained here!  
        </description>
    </item>
<channel>

The next part uses string.match to match the contents of the src="..." block:

var src = string.match(web.text('http://feedproxy.google.com/DilbertDailyStrip', 'channel/item/description'), 'src="([^"]+)"'); 

That value is then passed to web.image to render the comic.

Other web comics

It is left as an exercise to the reader to provide a daily XKCD template!

Tag page
Viewing 2 of 2 comments: view all
Thanks for this, I'm not sure if this is supposed to happen but, when I add the {{ template.DailyDilbert() }} code, this appears in red below the cartoon. If I don't add the code, I still get the cartoon - so what is the point of the template code? I've added the cartoon to our wiki in the hope that it will encourage people to start using it. :)
Posted 12:09, 30 Jan 2009
The template is only relevant if you were to copy/paste the code into a template for re-use. Otherwise, you can use the code directly on a page as long as you paste it in while you're in source mode (not WYSIWYG).
Posted 14:15, 30 Jan 2009
Viewing 2 of 2 comments: view all
You must login to post a comment.