Paste: get yahoo taiwan exchange info ver3

Author: cataska
Mode: factor
Date: Thu, 26 Mar 2009 04:51:35
Plain Text |
USING: formatting http.client sequences strings kernel io math regexp
unicode.case assocs generalizations ;

IN: currency

CONSTANT: coins {
    "TWD" "CNY" "JPY" "KRW"
    "HKD" "THB" "SGD" "IDR"
    "VND" "MYR" "PHP" "INR"
    "AED" "KWD" "AUD" "NZD"
    "USD" "CAD" "BRL" "MXN"
    "ARS" "CLP" "VEB" "EUR"
    "GBP" "RUB" "CHF" "SEK"
    "ZAR"
}

CONSTANT: calias H{ { "NTD" "TWD" } { "RMB" "CNY" } }

CONSTANT: yahootw-url "http://tw.money.yahoo.com/currency_exc_result?amt=%d&from=%s&to=%s"

CONSTANT: div-tag "            <div class"

: coins? ( coin -- ? )
    coins member? ;

: calias? ( alias -- ? )
    calias key? ;

: calias-at ( alias -- value/? )
    calias at* ;

: make-url ( money from to -- url )
    yahootw-url sprintf ;

: strip-em-tag ( str -- newstr )
    R! </?em>! "" re-replace ;

: find-exchange ( str -- result )
    "經過計算後," over start 7 + cut div-tag over start 0 spin subseq strip-em-tag nip ;

: get-ex-money ( money from to -- result )
    [ >upper ] bi@ 2dup [ dup coins? [ t ] [ dup calias? [ calias-at ] [ f ] if ] if ] bi@ swapd
    and [ [ 2drop ] 2dip make-url http-get nip find-exchange ]
    [ 5 ndrop f ] if ;

New Annotation

Summary:
Author:
Mode:
Body: