1 ! Copyright (C) 2012 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs classes.tuple colors.constants
4 colors.hex combinators formatting fry hashtables http.client io
5 io.styles json json.reader kernel make math math.parser
6 sequences splitting ui urls ;
9 TUPLE: post title postedBy points id url commentCount postedAgo ;
13 : json-null>f ( obj -- obj/f )
14 dup json-null = [ drop f ] when ;
16 : items> ( seq -- seq' )
19 [ json-null>f ] change-postedAgo
20 [ json-null>f ] change-postedBy
21 dup url>> "/comments" head? [
22 dup url>> "/" split last string>number >>id
27 : hacker-news-items ( -- seq )
28 "http://api.ihackernews.com/page" http-get nip
29 json> "items" of items> ;
31 : write-title ( title url -- )
34 ui-running? COLOR: black COLOR: white ? foreground ,,
37 : write-link ( title url -- )
40 HEXCOLOR: 888888 foreground ,,
43 : write-text ( str -- )
44 H{ { foreground HEXCOLOR: 888888 } } format ;
46 : post>user-url ( post -- user-url )
47 postedBy>> "http://news.ycombinator.com/user?id=" prepend >url ;
49 : post>comments-url ( post -- user-url )
50 id>> "http://news.ycombinator.com/item?id=%d" sprintf >url ;
52 ! Api is funky, gives id=0 and /comment/2342342 for self-post ads
53 : post>url ( post -- url )
62 : post. ( post index -- )
63 "%2d. " sprintf write-text {
64 [ [ title>> ] [ post>url ] bi write-title ]
65 [ post>url host>> " (" ")" surround write-text nl ]
66 [ points>> " %d points" sprintf write-text ]
67 [ dup postedBy>> [ " by " write-text [ postedBy>> ] [ post>user-url ] bi write-link ] [ drop ] if ]
68 [ dup postedAgo>> [ " " write-text postedAgo>> write-text ] [ drop ] if ]
71 [ commentCount>> [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
72 [ post>comments-url ] bi write-link nl nl
78 "http://news.ycombinator.com" >url presented associate
82 { background HEXCOLOR: ff6600 }
83 { foreground COLOR: black }
84 } assoc-union format nl ;
87 hacker-news-items banner.
88 [ 1 + post. ] each-index ;