Version 29 of Daily Dilbert

Updated 2008-10-26 16:09:12 by Stu

Just for fun: Get your daily Dilbert comic and display it. If you provide a directory name as argument, it'll be saved to dirname/dilbertyyyymmdd.gif where yyyymmdd is todays date.


 package require Tk
 package require http
 package require base64

 set um [http::geturl http://www.dilbert.com/]
 set b [split [http::data $um] "\n"]
 #set ln [lsearch -inline -glob $b "*ALT=?Today's Dilbert Comic*"]
 set ln [lsearch -inline -glob $b "*ALT=?Today's Comic*"]

 regexp {^.*IMG SRC=\"([^"]+)\".*$} $ln --> picurl
 http::cleanup $um
 set um [http::geturl http://www.unitedmedia.com$picurl]
 if {$argc && [file isdirectory [lindex $argv 0]]} {
   set today [clock format [clock seconds] -format "dilbert%Y%m%d.gif"]
   set fd [open [file join [lindex $argv 0] $today] w]
   fconfigure $fd -translation binary -encoding binary
   puts $fd [http::data $um]
   close $fd
 }
 set pic [base64::encode [http::data $um]]
 http::cleanup $um
 image create photo dilbert -data $pic
 label .l -image dilbert
 pack .l

Michael Jacobson: Also see Comic Server for some code to get a bunch of comic strips from uComics. Or you can check out (on windows only ~ sorry) in a TclKit application that servers the comics and display them in a embededd MSIE frame using optcl. Get the application here ... http://mywebpages.comcast.net/jakeforce/COMon!ics.kit (96kb). (I use this code in NewzPoint)


02. Aug. 2005: The Text for the Daily Dilbert comic changed. Replace the lsearch line with the following (This has already been done; the if 0 { wrapper was added to make the version grabbed by wish-reaper operate correctly):

 if 0 {
   set ln [lsearch -inline -glob $b "*ALT=?Today's Comic*"]
 }

RLH 20050802: I get the error:

 if 0 {
   "Can't read "picurl": no such variable while executing "http::geturl http://www.unitedmedia.com$picurl"
 }

RS Have you applied the fix above? It worked for me after I did it.

RLH I had read the post wrong and thought it was fixed above. Works now and I have commented out the old line above and added the one that works.

escargo 3 Aug 2005 - Touched up the previous included text to make compatible with wish-reaper.

CJL 11 July 2006 - The presence (and validity) of the optional directory name argument determined whether a file was written, but didn't affect where it was written. Fixed.


Stu 2008-10-25 The Dilbert site has changed.
Here's the above functionality (minus the image saving) in two lines:

Stu 2008-10-26 Updated to work on Sunday; now a one-liner.

foreach p {Tk http base64} { package require $p }; pack [label .l -image [image create photo dilbert -data [base64::encode [http::data [set tok [http::geturl [lindex [regexp -inline [append {} {http://dilbert.com/dyn/str_strip/(0+/){4}\d{5}/\d{4}/\d{3}/\d{5}/\d{5}.strip} [expr {[clock format [clock seconds] -format %u]==7?".sunday":""}] .gif] [http::data [set tok [http::geturl http://www.dilbert.com/]]][http::cleanup $tok]] 0]]]][http::cleanup $tok]]]]

See also:


Category Community | Category Example | Category Internet