xkcd: having XKCD in a terminal - priceless.

db4
John Benediktsson 2012-07-24 15:55:04 -07:00
parent 928a460aa1
commit 5a323780d8
1 changed files with 16 additions and 7 deletions

View File

@ -1,25 +1,34 @@
! Copyright (C) 2011 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: formatting http.client images.http images.viewer kernel
regexp strings ;
USING: formatting http.client images.http images.viewer io
kernel regexp sequences strings ui wrap.strings xml
xml.traversal ;
IN: xkcd
<PRIVATE
: load-comic ( url -- image )
: xkcd-image. ( url -- )
http-get nip
R" http://imgs\.xkcd\.com/comics/[^\.]+\.(png|jpg)"
first-match >string load-http-image ;
first-match >string load-http-image image. ;
: xkcd-text. ( url -- )
http-get nip string>xml
"transcript" "id" deep-tag-with-attr children>string
80 wrap-lines [ print ] each ;
: comic. ( url -- )
ui-running? [ xkcd-image. ] [ xkcd-text. ] if ;
PRIVATE>
: xkcd. ( n -- )
"http://xkcd.com/%s/" sprintf load-comic image. ;
"http://xkcd.com/%s/" sprintf comic. ;
: random-xkcd. ( -- )
"http://dynamic.xkcd.com/random/comic/" load-comic image. ;
"http://dynamic.xkcd.com/random/comic/" comic. ;
: latest-xkcd. ( -- )
"http://xkcd.com" load-comic image. ;
"http://xkcd.com" comic. ;