 
 : get-user-info ( username -- user )
     "http://api.reddit.com/user/%s/about" sprintf
-    http-get nip json> parse-data ;
+    http-get nip json> ;
 
 : get-url-info ( url -- page )
     "http://api.reddit.com/api/info?url=%s" sprintf json-page ;

! Copyright (C) 2011-2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license

USING: accessors assocs calendar classes.tuple colors.constants
colors.hex combinators formatting http.client io io.styles json
json.reader kernel make math math.statistics sequences urls
namespaces fry ;

IN: reddit

<PRIVATE

TUPLE: comment approved_by author author_flair_css_class
author_flair_text banned_by body body_html created created_utc
downs id levenshtein likes link_id link_title name num_reports
parent_id replies edited subreddit subreddit_id ups score_hidden
distinguished gilded saved link_author link_url mod_reports
report_reasons score user_reports controversiality ;

TUPLE: user comment_karma created created_utc has_mail
has_mod_mail id is_gold is_mod link_karma name is_friend
has_verified_email over_18 ;

TUPLE: story author author_flair_css_class author_flair_text
approved_by banned_by clicked created created_utc domain downs
gilded hidden id is_self levenshtein likes link_flair_css_class
link_flair_text media media_embed mod_reports name edited num_comments
num_reports over_18 permalink report_reasons saved score selftext
selftext_html subreddit subreddit_id thumbnail title ups url
user_reports distinguished secure_media secure_media_embed stickied
visited ;

TUPLE: subreddit accounts_active created created_utc description
display_name id header_img header_size header_title name over18
public_description subscribers title url ;

: parse-data ( assoc -- obj )
    [ "data" of ] [ "kind" of ] bi {
        { "t1" [ comment ] }
        { "t2" [ user ] }
        { "t3" [ story ] }
        { "t5" [ subreddit ] }
        [ throw ]
    } case from-slots ;

TUPLE: page url data before after ;

: json-page ( url -- page )
    >url dup http-get nip json> "data" of {
        [ "children" of [ parse-data ] map ]
        [ "before" of [ f ] when-json-null ]
        [ "after" of [ f ] when-json-null ]
    } cleave \ page boa ;

PRIVATE>

: user-links ( username -- stories )
    get-user data>> [ "kind" of "t3" = ] filter
    [ "data" of "url" of ] map ;

: user-comments ( username -- comments )
    get-user data>> [ "kind" of "t1" = ] filter
    [ "data" of "body" of ] map ;

: user-karma ( username -- karma )
    get-user-info "data" of "link_karma" of ;

: url-score ( url -- score )
    get-url-info data>> [ "score" of ] map-sum ;

: subreddit-links ( subreddit -- links )
    get-subreddit data>> [ "url" of ] map ;

: story>comments-url ( story -- url )
    "permalink" of "http://reddit.com" prepend >url ;

: story>author-url ( story -- url )
    "author" of "http://reddit.com/user/" prepend >url ;

<PRIVATE

