]> gitweb.factorcode.org Git - factor.git/blob - extra/hacker-news/hacker-news.factor
http.client: remove http-get* and its friends, change http-request* and with-http...
[factor.git] / extra / hacker-news / hacker-news.factor
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 ;
7 IN: hacker-news
8
9 TUPLE: post title postedBy points id url commentCount postedAgo ;
10
11 <PRIVATE
12
13 : json-null>f ( obj -- obj/f )
14     dup json-null = [ drop f ] when ;
15
16 : items> ( seq -- seq' )
17     [
18         \ post from-slots
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
23             "self" >>url
24         ] when
25     ] map ;
26
27 : hacker-news-items ( -- seq )
28     "http://api.ihackernews.com/page" http-get nip
29     json> "items" of items> ;
30
31 : write-title ( title url -- )
32     '[
33         _ presented ,,
34         ui-running? COLOR: black COLOR: white ? foreground ,,
35     ] H{ } make format ;
36
37 : write-link ( title url -- )
38     '[
39         _ presented ,,
40         HEXCOLOR: 888888 foreground ,,
41     ] H{ } make format ;
42
43 : write-text ( str -- )
44     H{ { foreground HEXCOLOR: 888888 } } format ;
45
46 : post>user-url ( post -- user-url )
47     postedBy>> "http://news.ycombinator.com/user?id=" prepend >url ;
48
49 : post>comments-url ( post -- user-url )
50     id>> "http://news.ycombinator.com/item?id=%d" sprintf >url ;
51
52 ! Api is funky, gives id=0 and /comment/2342342 for self-post ads
53 : post>url ( post -- url )
54     dup url>> "self" = [
55         post>comments-url
56     ] [
57         url>> >url
58     ] if ;
59
60 PRIVATE>
61
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 ]
69         [
70             " | " write-text
71             [ commentCount>> [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
72             [ post>comments-url ] bi write-link nl nl
73         ]
74     } cleave ;
75
76 : banner. ( -- )
77     "Hacker News"
78     "http://news.ycombinator.com" >url presented associate
79     H{
80         { font-size 20 }
81         { font-style bold }
82         { background HEXCOLOR: ff6600 }
83         { foreground COLOR: black }
84     } assoc-union format nl ;
85
86 : hacker-news. ( -- )
87     hacker-news-items banner.
88     [ 1 + post. ] each-index ;