#reader(lib "htdp-beginner-reader.ss" "lang")((modname parse-google-maps-places) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
(define-struct loc (lat long))
(define-struct place (name loc radius))
(define-struct world (url places out-of-sync?))
(define (make-mymaps-url msid)
(string-append "http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=1&jsv=151e&msa=0&output=georss&msid="
msid))
(define initial-world
(make-world (make-mymaps-url "106933521686950086948.00046579f4b482756abc5")
(list)
true))
(define (parse-places xexpr)
(parse-items
(find-children 'item
(children (first (find-children 'channel (children xexpr)))))))
(define (parse-items xexprs)
(cond
[(empty? xexprs)
empty]
[else
(cons (parse-item (first xexprs))
(parse-items (rest xexprs)))]))
(define (parse-item xexpr)
(make-place (get-text (first (find-children 'title (children xexpr))))
(parse-georss:point (first (find-children 'georss:point (children xexpr))))
0))
(define (parse-georss:point xexpr)
(make-loc (first (split-whitespace (get-text xexpr)))
(second (split-whitespace (get-text xexpr)))))
(define (children an-xexpr)
(cond
[(string? an-xexpr)
(error 'children "Can't have children of a string xexpr")]
[else
(rest (rest an-xexpr))]))
(define (attrs an-xexpr)
(cond
[(string? an-xexpr)
(error 'attrs "Can't get attributes of a string xexpr")]
[else
(second an-xexpr)]))
(define (get-text an-xexpr)
(cond
[(string? an-xexpr)
an-xexpr]
[(pair? an-xexpr)
(get-text* (children an-xexpr))]))
(define (get-text* xexprs)
(cond
[(empty? xexprs)
""]
[else
(string-append (get-text (first xexprs))
(get-text* (rest xexprs)))]))
(define (find-children name children)
(cond [(empty? children)
empty]
[else
(cond [(string? (first children))
(find-children name (rest children))]
[(pair? (first children))
(cond
[(symbol=? name (first (first children)))
(cons (first children)
(find-children name (rest children)))]
[else
(find-children name (rest children))])]
[else
(error 'find-children children)])]))
(define (place->string a-place)
(string-append (place-name a-place)
" "
(loc->string (place-loc a-place))
" "
(number->string (place-radius a-place))))
(define (loc->string a-loc)
(string-append "(" (loc-lat a-loc) ", " (loc-long a-loc) ")"))
(define (places->string places)
(cond
[(empty? places)
""]
[else
(string-append (place->string (first places))
", "
(places->string (rest places)))]))
(define (description a-world)
(cond [(world-out-of-sync? a-world)
"Out of sync"]
[else
(places->string (world-places a-world))]))
(define (reparse a-world)
(make-world (world-url a-world)
(parse-places (xml->s-exp (get-url (world-url a-world))))
false))
(define view
(col (message description)
(button "Parse" reparse)))
(big-bang initial-world view)