 sequences splitting ui urls ;
 IN: hacker-news
 
-TUPLE: post title postedBy points id url commentCount postedAgo ;
-
 <PRIVATE
 
 : json-null>f ( obj -- obj/f )
     dup json-null = [ drop f ] when ;
 
-: items> ( seq -- seq' )
+: hacker-news-items ( n -- seq )
     [
-        \ post from-slots
-        [ json-null>f ] change-postedAgo
-        [ json-null>f ] change-postedBy
-        dup url>> "/comments" head? [
-            dup url>> "/" split last string>number >>id
-            "self" >>url
-        ] when
-    ] map ;
-
-: hacker-news-items ( -- seq )
-    "http://api.ihackernews.com/page" http-get nip
-    json> "items" of items> ;
+        "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
+        http-get nip json>
+    ] dip head
+    [
+        number>string
+        "https://hacker-news.firebaseio.com/v0/item/" ".json?print=pretty" surround
+        http-get nip json>
+    ] parallel-map ;
 
 : write-title ( title url -- )
     '[

    } assoc-union format nl ;

: hacker-news. ( -- )
    hacker-news-items banner.
    [ 1 + post. ] each-index ;

    H{ { foreground HEXCOLOR: 888888 } } format ;

: post>user-url ( post -- user-url )
    "by" of "http://news.ycombinator.com/user?id=" prepend >url ;

: post>comments-url ( post -- user-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
: post>url ( post -- url )
    dup "url" of "self" = [ post>comments-url ] [ "url" of >url ] if ;

PRIVATE>

: post. ( post index -- )
    "%2d. " sprintf write-text {
        [ [ "title" of ] [ "url" of ] bi write-title ]
        [ post>url host>> " (" ")" surround write-text nl ]
        [ "score" of "    %d points" sprintf write-text ]
        [ 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 ]
        [
            " | " write-text
            [ "descendants" of [ "discuss" ] [ "%d comments" sprintf ] if-zero ]
            [ post>comments-url ] bi write-link nl nl
        ]
    } cleave ;
