hacker-news: use new hackernews api on firebase.

db4
Doug Coleman 2015-07-29 21:05:20 -07:00
parent f6ebea6320
commit 92a0e1e701
1 changed files with 19 additions and 28 deletions

View File

@ -6,27 +6,21 @@ io.styles json json.reader kernel make math math.parser
sequences splitting ui urls ; sequences splitting ui urls ;
IN: hacker-news IN: hacker-news
TUPLE: post title postedBy points id url commentCount postedAgo ;
<PRIVATE <PRIVATE
: json-null>f ( obj -- obj/f ) : json-null>f ( obj -- obj/f )
dup json-null = [ drop f ] when ; dup json-null = [ drop f ] when ;
: items> ( seq -- seq' ) : hacker-news-items ( n -- seq )
[ [
\ post from-slots "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
[ json-null>f ] change-postedAgo http-get nip json>
[ json-null>f ] change-postedBy ] dip head
dup url>> "/comments" head? [ [
dup url>> "/" split last string>number >>id number>string
"self" >>url "https://hacker-news.firebaseio.com/v0/item/" ".json?print=pretty" surround
] when http-get nip json>
] map ; ] parallel-map ;
: hacker-news-items ( -- seq )
"http://api.ihackernews.com/page" http-get nip
json> "items" of items> ;
: write-title ( title url -- ) : write-title ( title url -- )
'[ '[
@ -44,31 +38,27 @@ TUPLE: post title postedBy points id url commentCount postedAgo ;
H{ { foreground HEXCOLOR: 888888 } } format ; H{ { foreground HEXCOLOR: 888888 } } format ;
: post>user-url ( post -- user-url ) : post>user-url ( post -- user-url )
postedBy>> "http://news.ycombinator.com/user?id=" prepend >url ; "by" of "http://news.ycombinator.com/user?id=" prepend >url ;
: post>comments-url ( post -- user-url ) : post>comments-url ( post -- user-url )
id>> "http://news.ycombinator.com/item?id=%d" sprintf >url ; "id" of "http://news.ycombinator.com/item?id=%d" sprintf >url ;
! Api is funky, gives id=0 and /comment/2342342 for self-post ads ! Api is funky, gives id=0 and /comment/2342342 for self-post ads
: post>url ( post -- url ) : post>url ( post -- url )
dup url>> "self" = [ dup "url" of "self" = [ post>comments-url ] [ "url" of >url ] if ;
post>comments-url
] [
url>> >url
] if ;
PRIVATE> PRIVATE>
: post. ( post index -- ) : post. ( post index -- )
"%2d. " sprintf write-text { "%2d. " sprintf write-text {
[ [ title>> ] [ post>url ] bi write-title ] [ [ "title" of ] [ "url" of ] bi write-title ]
[ post>url host>> " (" ")" surround write-text nl ] [ post>url host>> " (" ")" surround write-text nl ]
[ points>> " %d points" sprintf write-text ] [ "score" of " %d points" sprintf write-text ]
[ dup postedBy>> [ " by " write-text [ postedBy>> ] [ post>user-url ] bi write-link ] [ drop ] if ] [ dup "by" of [ " by " write-text [ "by" of ] [ post>user-url ] bi write-link ] [ drop ] if ]
[ dup postedAgo>> [ " " write-text postedAgo>> write-text ] [ drop ] if ] ! [ dup postedAgo>> [ " " write-text postedAgo>> write-text ] [ drop ] if ]
[ [
" | " write-text " | " write-text
[ commentCount>> [ "discuss" ] [ "%d comments" sprintf ] if-zero ] [ "descendants" of [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
[ post>comments-url ] bi write-link nl nl [ post>comments-url ] bi write-link nl nl
] ]
} cleave ; } cleave ;
@ -84,5 +74,6 @@ PRIVATE>
} assoc-union format nl ; } assoc-union format nl ;
: hacker-news. ( -- ) : hacker-news. ( -- )
hacker-news-items banner. 25 hacker-news-items
banner.
[ 1 + post. ] each-index ; [ 1 + post. ] each-index ;