読者です 読者をやめる 読者になる 読者になる

(define -ayalog '())

括弧に魅せられて道を外した名前のないプログラマ

あやぴーが今日の天気をお伝えします。

こんな感じ

元ネタというか、こんなものを走り書きしたのはこの発言を受けて。

コードこんなん。assoc-refがやたらめったら出てくるけど、んーんー…。

#!/usr/local/bin/gosh

(use rfc.http)
(use rfc.uri)
(use rfc.json)
(use gauche.parseopt)
(use net.twitter)

;; twitter
(define *cred* (make <twitter-cred>
		 :consumer-key "XXXXXXXXXXX"
		 :consumer-secret "XXXXXXXXXXX"
		 :access-token "XXXXXXXXXXX"
		 :access-token-secret "XXXXXXXXXXX"))

;; use livedoor weather api.
;; http://weather.livedoor.com/forecast/webservice/json/v1

(define (tenki-api city)
  (receive (status head body)
	   (http-get "weather.livedoor.com"
		     (string-append
		      "/forecast/webservice/json/v1"
		      "?city=" city))
	   (parse-json-string body)))

(define (temperature-celsius temperature m)
  (let ((temp (assoc-ref temperature m)))
    (if (pair? temp) (assoc-ref temp "celsius") "--")))

(define (formatting tenki day)
  (let* ((description (assoc-ref tenki "description"))
	 (forecasts   (assoc-ref tenki "forecasts"))
	 (day         (vector-ref forecasts day)))
    (let* ((title        (assoc-ref tenki "title"))
	   (date         (assoc-ref day "date"))
	   (image        (assoc-ref day "image"))
	   (image-title  (assoc-ref image "title"))
	   (image-url    (assoc-ref image "url"))
	   (temperature  (assoc-ref day "temperature"))
	   (temp-min-cel (temperature-celsius temperature "min"))
	   (temp-max-cel (temperature-celsius temperature "max")))
      (string-join `(,date
		     ,title
		     ,image-title
		     ,(string-append "最低気温: " temp-min-cel
				     ", 最高気温: " temp-max-cel))
		     (string #\newline)))))

(define (main args)
  (let-args (cdr args)
	    ((city "c|city=s" "130010")
	     (day "d|day=i" 0)
	     (tweet "tweet")
	     . rest)
	    (let ((content (formatting (tenki-api city) day)))
	      (if tweet
		  (twitter-update *cred* content)
		  (print content)))))