factor/extra/xkcd/xkcd.factor

56 lines
1.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2011 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
2014-11-12 22:44:25 -05:00
USING: accessors formatting html.entities html.parser
html.parser.analyzer html.parser.printer http.client images.http
images.viewer images.viewer.prettyprint io kernel parser
prettyprint.custom prettyprint.sections regexp sequences strings
ui wrap.strings ;
IN: xkcd
<PRIVATE
2012-08-17 13:41:45 -04:00
: comic-image ( url -- image )
http-get nip
R/ \/\/imgs\.xkcd\.com\/comics\/[^\.]+\.(png|jpg)/
first-match >string "http:" prepend load-http-image ;
2014-11-12 22:41:58 -05:00
: comic-image. ( url -- )
comic-image image. ;
2012-08-17 12:47:33 -04:00
2014-11-12 22:41:58 -05:00
: comic-text ( url -- string )
scrape-html nip "transcript" find-by-id-between
html-text html-unescape ;
2012-08-17 12:47:33 -04:00
: comic-text. ( url -- )
2014-11-12 22:41:58 -05:00
comic-text 80 wrap-string print ;
: comic. ( url -- )
2012-07-24 19:00:17 -04:00
ui-running? [ comic-image. ] [ comic-text. ] if ;
PRIVATE>
2012-08-17 12:47:33 -04:00
: xkcd-url ( n -- url )
"http://xkcd.com/%s/" sprintf ;
: xkcd-image ( n -- image )
xkcd-url comic-image ;
: xkcd. ( n -- )
2012-08-17 12:47:33 -04:00
xkcd-url comic. ;
: random-xkcd. ( -- )
"http://dynamic.xkcd.com/random/comic/" comic. ;
: latest-xkcd. ( -- )
"http://xkcd.com" comic. ;
2012-08-17 12:47:33 -04:00
TUPLE: xkcd number image ;
2012-08-17 12:47:33 -04:00
C: <xkcd> xkcd
SYNTAX: XKCD: scan-number dup xkcd-image <xkcd> suffix! ;
2012-08-17 12:47:33 -04:00
2012-08-17 19:49:41 -04:00
M: xkcd pprint* image>> <image-section> add-section ;