1 ! Copyright (C) 2012 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors assocs calendar calendar.format
5 calendar.holidays.us colors combinators concurrency.combinators
6 formatting hashtables http.client io io.styles json.reader
7 kernel make math sequences ui ui.theme urls ;
11 CONSTANT: christmas-red COLOR: #bc2c21
12 CONSTANT: christmas-green COLOR: #376627
15 : hacker-news-ids ( endpoint -- ids )
16 "https://hacker-news.firebaseio.com/v0/%s.json?print=pretty" sprintf
19 : hacker-news-id>json-url ( n -- url )
20 "https://hacker-news.firebaseio.com/v0/item/%d.json?print=pretty" sprintf ;
22 : hacker-news-items ( n endpoint -- seq )
23 hacker-news-ids swap index-or-length head
24 [ hacker-news-id>json-url http-get nip json> ] parallel-map ;
26 : hacker-news-top-stories ( n -- seq )
27 "topstories" hacker-news-items ;
29 : hacker-news-new-stories ( n -- seq )
30 "newstories" hacker-news-items ;
32 : hacker-news-best-stories ( n -- seq )
33 "beststories" hacker-news-items ;
35 : hacker-news-ask-stories ( n -- seq )
36 "askstories" hacker-news-items ;
38 : hacker-news-show-stories ( n -- seq )
39 "showstories" hacker-news-items ;
41 : hacker-news-job-stories ( n -- seq )
42 "jobstories" hacker-news-items ;
44 : christmas-day? ( -- ? )
45 now dup christmas-day same-day? ;
47 : number-color ( n -- color )
49 odd? christmas-red christmas-green ?
54 : background-color ( -- color )
55 christmas-day? COLOR: #bc2c21 COLOR: #ff6600 ? ;
57 : write-number ( n -- )
58 [ "%2d. " sprintf H{ } clone ] keep
59 number-color foreground pick set-at format ;
61 : write-title ( title url -- )
64 ui-running? text-color COLOR: white ? foreground ,,
67 : write-link ( title url -- )
70 COLOR: #888888 foreground ,,
73 : write-text ( str -- )
74 text-color foreground associate format ;
76 : post>user-url ( post -- user-url )
77 "by" of "http://news.ycombinator.com/user?id=" prepend >url ;
79 : post>comments-url ( post -- user-url )
80 "id" of "http://news.ycombinator.com/item?id=%d" sprintf >url ;
82 ! Api is funky, gives id=0 and /comment/2342342 for self-post ads
83 : post>url ( post -- url )
84 dup "url" of "self" = [ post>comments-url ] [ "url" of >url ] if ;
88 : post. ( post index -- )
91 [ [ "title" of ] [ "url" of ] bi write-title ]
92 [ post>url host>> " (" ")" surround write-text nl ]
93 [ "score" of " %d points" sprintf write-text ]
94 [ dup "by" of [ " by " write-text [ "by" of ] [ post>user-url ] bi write-link ] [ drop ] if ]
95 [ "time" of [ " " write-text unix-time>timestamp relative-time write-text ] when* ]
97 dup "descendants" of [
99 [ "descendants" of [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
100 [ post>comments-url ] bi write-link
108 "http://news.ycombinator.com" >url presented associate
112 { foreground COLOR: black }
114 background-color background pick set-at
117 : hacker-news-feed. ( seq -- )
118 [ 1 + post. ] each-index ;
120 : hacker-news. ( str seq -- )
122 [ hacker-news-feed. ] bi* ;
124 : hacker-news-top. ( -- )
126 30 hacker-news-top-stories
129 : hacker-news-new. ( -- )
131 50 hacker-news-new-stories
134 : hacker-news-best. ( -- )
136 50 hacker-news-best-stories
139 : hacker-news-ask. ( -- )
141 50 hacker-news-ask-stories
144 : hacker-news-show. ( -- )
146 50 hacker-news-show-stories
149 : hacker-news-job. ( -- )
151 50 hacker-news-job-stories