Paste: Basic twitter search client

Author: Gertm
Mode: factor
Date: Thu, 18 Feb 2010 14:38:32
Plain Text |
! Copyright (C) 2010 Gertm.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs http.client io json.reader kernel 
sequences ;
IN: twitsearch

: makejson ( response data -- x ) drop body>> json> ;

: get-tweet-hsh ( x -- x ) "results" swap at ;

: search ( string -- x ) "http://search.twitter.com/search.json?q="
    swap append http-get makejson get-tweet-hsh ;

: print-with-prefix ( prefix string -- ) append print ;

: twt-print-field ( hsh field prefix -- ) -rot swap at print-with-prefix ;

: twt-print-from ( x -- ) "from_user" "From: " twt-print-field ;

: twt-print-text ( x -- ) "text" ">> " twt-print-field ;

: tweet ( x -- ) [ twt-print-from ] [ twt-print-text ] bi ;

: results ( x -- ) search [ tweet ] each ;

Annotation: Refactored version

Author: Gertm
Mode: factor
Date: Thu, 18 Feb 2010 14:51:15
Plain Text |
! Copyright (C) 2010 Gertm.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs http.client io json.reader kernel prettyprint
sequences ;
IN: twitsearch

CONSTANT: searchurl "http://search.twitter.com/search.json?q="

: make-json ( response data -- x ) nip json> ;

: get-tweet-hsh ( x -- x ) "results" swap at ;

: search ( string -- x ) searchurl
    prepend http-get make-json get-tweet-hsh ;

: print-with-prefix ( prefix string -- ) append print ;

: twt-print-field ( hsh field prefix -- ) [ swap at ] dip print-with-prefix ;

: twt-print-from ( x -- ) "from_user" "From: " twt-print-field ;

: twt-print-text ( x -- ) "text" ">> " twt-print-field ;

: tweet ( x -- ) [ twt-print-from ] [ twt-print-text ] bi ;

: results ( x -- ) search [ tweet ] each ;

Annotation: Version 0.3 !

Author: gertm
Mode: factor
Date: Thu, 18 Feb 2010 14:56:03
Plain Text |
! Copyright (C) 2010 Gertm.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs http.client io json.reader kernel prettyprint
sequences ;
IN: twitsearch

CONSTANT: searchurl "http://search.twitter.com/search.json?q="

: make-json ( response data -- x ) nip json> ;

: get-tweet-hsh ( x -- x ) "results" swap at ;

: search ( string -- x ) searchurl
    prepend http-get nip json> get-tweet-hsh ;

: print-with-prefix ( prefix string -- ) append print ;

: print-field ( hsh field prefix -- ) [ swap at ] dip print-with-prefix ;

: print-from ( x -- ) "from_user" "From: " print-field ;

: print-text ( x -- ) "text" ">> " print-field ;

: tweet ( x -- ) [ print-from ] [ print-text ] bi ;

: results ( x -- ) search [ tweet ] each ;

New Annotation

Summary:
Author:
Mode:
Body: