Created
November 12, 2011 16:13
-
-
Save valvallow/1360744 to your computer and use it in GitHub Desktop.
get weather
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/gosh | |
(use srfi-1) | |
(use sxml.ssax) | |
(use sxml.sxpath) | |
(use rfc.http) | |
(use rfc.json) | |
(use gauche.parseopt) | |
(define (usage) | |
(print "Usage: weather [options ...] city-name") | |
(print " - c|city : city name") | |
(print " - j|json : print json format") | |
(print " - h|help : print usage") | |
(exit 2)) | |
(define (get-weather-xml city) | |
(receive (status head body) | |
(http-get "www.google.com" (string-append "/ig/api?weather=" city)) | |
(ssax:xml->sxml (open-input-string body) '()))) | |
(define (print-weather weather) | |
(for-each (^e (display (car e)) | |
(display ":") | |
(print (cadr (cadadr e)))) | |
(cdr weather)) | |
(newline)) | |
(define (weather->alist weather) | |
(map (^e (cons (x->string (car e)) | |
(x->string (cadar (cdadr e))))) | |
(cdr weather))) | |
(define (weathers->json date weathers) | |
(construct-json | |
(list (cons "date" date) | |
(cons "current" (weather->alist (car weathers))) | |
(cons "in_the_future" (list->vector (map weather->alist (cdr weathers))))))) | |
(define (main args) | |
(let-args (cdr args) | |
((city "c|city=s") | |
(json "j|json") | |
(help "h|help" => usage) | |
. rest) | |
(let* ((city (or city (if (null? rest) | |
(symbol->string (read)) | |
(car rest)))) | |
(xml (get-weather-xml city)) | |
(weathers (drop (car ((sxpath "/xml_api_reply/weather") xml)) 3)) | |
(date (car ((sxpath "/xml_api_reply/weather/forecast_information/forecast_date/@data[1]/text()") xml)))) | |
(if json | |
(write (weathers->json date weathers)) | |
(begin (print city ":" date) | |
(print-weather (car weathers)) | |
(for-each print-weather (cdr weathers))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment