XKCD random comic viewer

By Savage_CL on Jun 06, 2011

Wrote this because I was bored and someone told me to. Always starts with the latest comic, and moves on to randomize.

Left click on the loaded image to load a new one. Right click for a few options.

If you find the picture is too large to view, save it and view it in a different viewer.

Enjoy :)

EDIT: Updated

Now shows the image title in the titlebar and the alt text in a right-click menu!

Added "Default Image Viewer" for easy access to big images.

on *:START:{
  if (!$isdir(xkcd)) { mkdir xkcd }
}
alias xkcd {
  .remove %image
  clear @xkcd
  drawtext -o @xkcd 0 Arial 48 10 10 Loading...
  sockopen xkcd xkcd.com 80
}
on *:sockopen:xkcd:{
  sockwrite -n $sockname GET $iif(!%max,/#,/ $+ $rand(1,%max) $+ /) HTTP/1.0
  sockwrite -n $sockname Host:xkcd.org $+ $crlf $+ $crlf  
}
on *:sockread:xkcd:{
  if ($sockerr) error
  sockread %t
  while ($sockbr) {
    if (!%max) { 
      if ($regex(max,%t,/Permanent link to this comic: http:\/\/xkcd.com\/(\d+)\//Si)) { 
        set %max $regml(max,1)
      }
    }
    if ($regex(image,%t,/<img src="http:\/\/imgs.xkcd.com/comics/(.*)" title="(.*)" alt="(.*)"/i)) { 
      set %image $regml(image,1)
      set %xkcd.image.title $regml(image,2)
      set %xkcd.image.alt $regml(image,3)
      sockopen xkcdimageget imgs.xkcd.com 80
    }
    sockread %t
  }
}
on *:SOCKCLOSE:xkcd:{
  unset %kxcd.found
}
on *:sockopen:xkcdimageget:{
  sockwrite -n $sockname GET /comics/ $+ %image HTTP/1.1
  sockwrite -n $sockname Host: imgs.xkcd.com $+ $crlf $+ $crlf
}
On 1:SockRead:xkcdimageget:{
  if (!$sock($sockname).mark) {
    var %SockReader
    sockread %SockReader
    if (%SockReader == $null) { sockmark $sockname 1 }
  }
  else {
    sockread &picture 
    bwrite $qt($mIRCdir $+ %image) -1 -1 &picture
  }
}
on *:sockclose:xkcdimageget:{
  window -aBdpk0f @xkcd -1 -1 $pic($mircdir $+ %image).width $pic($mircdir $+ %image).height
  titlebar @xkcd - %xkcd.image.title
  xkcd.alt.prep
  drawpic @xkcd -1 -1 %image
}
on *:CLOSE:@xkcd:{
  .remove %image
  unset %image
  unset %xkcd.image.*
  unset %max
}
menu status,channel {
  xkcd:{
    $iif(%image,.remove %image)
    window -aBdpk0f @xkcd -1 -1 250 60
    drawtext -o @xkcd 0 Arial 48 10 1 Loading...
    sockopen xkcd xkcd.com 80
  }
}
menu @xkcd {
  dclick:xkcd
  Alt-text
  .$style(2) Hover text normally shown on the website:noop
  .-
  .$submenu($altmenu($1))
  -
  copy url...: clipboard http://imgs.xkcd.com/comics/ $+ %image
  Default Image Viewer:run %image
  Save Image: .copy -o %image xkcd\ $+ %image
  Saved Images: run $scriptdir $+ \xkcd
}
alias -l error {
  echo 4 -a ERROR!
  HALT
}
alias -l altmenu {
  if ($1 == begin) return
  if ($1 == end) return
  if ($gettok(%xkcd.alt.string,$1,124)) {
    return $v1 $+ :noop
  }
}
alias -l xkcd.alt.prep {
  var %o = $null
  var %string = %xkcd.image.alt
  var %count = $ceil($calc($len(%string) / 60))
  if (%count == 1) { set %xkcd.alt.string %string | return }
  while %count {
    tokenize 32 %string
    var %new.string = $1,%c = 2
    while ($len(%new.string) <= 60) {
      var %new.string = %new.string $($ $+ %c,2)
      inc %c
      if (%c > $0) goto end
    }
    :end
    var %string = $gettok(%string,%c -,32)
    var %o = %o $+ $chr(124) $+ %new.string
    dec %count
  }
  set %xkcd.alt.string $right(%o,-1)
  return
}

Comments

Sign in to comment.
hxck   -  Jun 06, 2011

Wow, nice job man. I like it.

 Respond  
Savage_CL   -  Jun 06, 2011

Thanks guys :)
If there's enough interest I'll add some stuff to combat large images -_-

 Respond  
Jethro   -  Jun 06, 2011

This is quite a nice work. I like it.

 Respond  
blackvenomm666   -  Jun 06, 2011

tested and works nicely very well done 9.0 +like

 Respond  
napa182   -  Jun 06, 2011

Interesting concept for a snippet Savage_CL. I like the idea.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.