(require 2htdp/universe) (require 2htdp/image) (require "lab8-teachpack.rkt") (define PREFIX "https://www.google.com/finance?q=") (define SUFFIX "&btnG=Search") (define SIZE 22) (define SPACER (text " " SIZE 'white)) ;; String -> Number ;; retrieve stock price the specified company ;; refresh every 15 seconds and display price together with price change ;; until the ticker window is killed; sample call: (stock-alert "Google") (define (stock-alert company) (local ((define-struct data (price delta)) ;; StockWorld is ;; (make-data String String) ;; price and delta specify the current price and how ;; much it changed since the last update (define url (string-append PREFIX company SUFFIX)) ;; StockWorld -> StockWorld ;; retrieve price, change, and time from url (define (retrieve-stock-data _w) (local ((define x (read-xexpr/web url))) (make-data (get x "price") (get x "priceChange")))) ;; StockWorld -> Image ;; render the stock market data as a single long line (define (render-stock-data w) (local ((define pt (text (data-price w) SIZE 'black)) (define dt (text (data-delta w) SIZE 'red))) (overlay (beside pt SPACER dt) (rectangle 300 35 'solid 'white))))) ;; -- IN -- (string->number (data-price (big-bang (retrieve-stock-data 'no-use) [on-tick retrieve-stock-data 15] [to-draw render-stock-data] [name (string-append "Stock Ticker: " company)]))))) ;; ----------------------------------------------------------------------------- ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ; ; ; ;;; ;;;; ;;; ;; ; ;;; ; ;; ;;; ; ;;; ;;; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;; ; ; ;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;; ; ; ;;;;; ; ;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ;;; ;; ; ; ;;;; ;;; ;; ; ;;;; ; ; ;;;; ; ;;;; ; ; ; ;; ; ;; ----------------------------------------------------------------------------- ;; the library of xexpr functions ;; An Xexpr.v2 is one of: ;; -- (cons Symbol [Listof SX.v2]) ;; -- (cons Symbol (cons [Listof Attribute] [Listof SX.v2])) ;; An Attribute has the shape: ;; (cons Symbol (cons String empty)) ;; An SX.v2 is one of: ;; -- String ;; -- Xexpr.v2 ;; Xexpr -> Symbol ;; retrieve the name of the element (define (xexpr-name xe) (first xe)) ;; Xexpr -> [Listof Attribute] ;; retrieve the list of attributes of @racket[xe] (define (xexpr-attributes xe) (local ((define optional-loa+content (rest xe))) (cond [(empty? optional-loa+content) '()] [else (local ((define possible-loa (first optional-loa+content))) (if (list-of-attributes? possible-loa) possible-loa '()))]))) ;; LoA-or-LoX -> Boolean ;; is the given value a list of attributes? (define (list-of-attributes? x) (cond [(empty? x) true] [else (local ((define possible-attribute (first x))) (cons? possible-attribute))])) ;; Xexpr -> [Listof Xexpr] ;; retrieve the content of an Xexpr (define (xexpr-content xe) (local ((define optional-loa+content (rest xe))) (cond [(empty? optional-loa+content) '()] [else (if (list-of-attributes? (first optional-loa+content)) (rest optional-loa+content) optional-loa+content)]))) (define MISSING "missing attribute") ;; [Listof Attribute] -> [Maybe String] ;; retrieve a specific attribute value from an XML element (define (attribute-value al a) (local ((define r (assq a al))) (if (boolean? r) #f (second r)))) ; (define x (read-xexpr/web (string-append PREFIX "Ford" SUFFIX))) ; (get-xexpr x "foo")