1 ;;; google-weather.el --- Fetch Google Weather forecasts.
3 ;; Copyright (C) 2010 Julien Danjou
5 ;; Author: Julien Danjou <julien@danjou.info>
8 ;; This file is NOT part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; This module allows you to fetch Google Weather forecast from the
34 (defgroup google-weather nil
38 (defconst google-weather-url
39 "http://www.google.com/ig/api"
40 "URL used to access the Google Weather API.")
42 (defconst google-weather-image-url
43 "http://www.google.com"
44 "URL prefix for images.")
46 (defcustom google-weather-unit-system-temperature-assoc
49 "Find temperature symbol from unit system."
50 :group
'google-weather
)
52 (defun google-weather-cache-expired (url expire-time
)
53 "Check if URL is cached for more than EXPIRE-TIME."
54 (cond (url-standalone-mode
55 (not (file-exists-p (url-cache-create-filename url
))))
56 (t (let ((cache-time (url-is-cached url
)))
61 (seconds-to-time expire-time
))
65 (defun google-weather-cache-fetch (url)
66 "Fetch URL from the cache."
67 (with-current-buffer (generate-new-buffer " *temp*")
68 (url-cache-extract (url-cache-create-filename url
))
71 (defun google-weather-retrieve-data (url &optional expire-time
)
72 "Retrieve URL and return its data as string.
73 If EXPIRE-TIME is set, the data will be fetched from the cache if
74 their are not older than EXPIRE-TIME seconds. Otherwise, they
75 will be fetched and then cached. Therefore, setting EXPIRE-TIME
76 to 0 force a cache renewal."
77 (let* ((expired (google-weather-cache-expired url expire-time
))
79 (url-retrieve-synchronously url
)
80 (google-weather-cache-fetch url
)))
82 (with-current-buffer buffer
83 (goto-char (point-min))
84 (search-forward "\n\n")
87 (detect-coding-region (point) (point-max) t
))
88 (set-buffer-multibyte t
)
89 (setq data
(xml-parse-region (point) (point-max)))
90 (when (and expired expire-time
)
91 (url-store-in-cache (current-buffer)))
92 (kill-buffer (current-buffer))
95 (defun google-weather-build-url (location &optional language
)
96 "Build URL to retrieve weather for LOCATION in LANGUAGE."
97 (concat google-weather-url
"?weather=" (url-hexify-string location
)
99 (concat "&hl=" language
))))
101 (defun google-weather-get-data (location &optional language cache
)
102 "Get weather data for LOCATION in LANGUAGE.
103 If CACHE is t, cache the result."
104 (google-weather-retrieve-data
105 (google-weather-build-url location language
) cache
))
107 (defun google-weather-data->weather
(data)
108 "Return all weather information from DATA."
109 (cddr (assoc 'weather
(cdr (assoc 'xml_api_reply data
)))))
111 (defun google-weather-data->forecast-information
(data)
112 "Return the forecast information of DATA."
113 (cddr (assoc 'forecast_information
(google-weather-data->weather data
))))
115 (defun google-weather-assoc (key data
)
116 "Do some sort of magic 'assoc to find fields in DATA."
117 (cdr (assoc 'data
(cadr (assoc key data
)))))
119 (defun google-weather-data->city
(data)
120 "Return the city where the DATA come from."
121 (google-weather-assoc
123 (google-weather-data->forecast-information data
)))
125 (defun google-weather-data->postal-code
(data)
126 "Return the postal code where the data come from."
127 (google-weather-assoc
129 (google-weather-data->forecast-information data
)))
131 (defun google-weather-data->unit-system
(data)
132 "Return the unit system used for data."
133 (google-weather-assoc
135 (google-weather-data->forecast-information data
)))
137 (defun google-weather-data->forecast-date
(data)
138 "Return the unit system used for data."
139 (google-weather-assoc
141 (google-weather-data->forecast-information data
)))
143 (defun google-weather-data->forecast
(data)
144 "Get forecast list from DATA."
145 ;; Compute date of the forecast in the same format as `current-time'
146 (let ((date (apply 'encode-time
148 (concat (google-weather-data->forecast-date data
) " 00:00:00")))))
151 (let* ((forecast-date (decode-time date
))
152 (forecast-encoded-date (list (nth 4 forecast-date
)
153 (nth 3 forecast-date
)
154 (nth 5 forecast-date
))))
155 ;; Add one day to `date'
156 (setq date
(time-add date
(days-to-time 1)))
157 `(,forecast-encoded-date
158 (low ,(google-weather-assoc 'low forecast
))
159 (high ,(google-weather-assoc 'high forecast
))
160 (icon ,(concat google-weather-image-url
161 (google-weather-assoc 'icon forecast
)))
162 (condition ,(google-weather-assoc 'condition forecast
)))))
163 (loop for entry in
(google-weather-data->weather data
)
164 when
(eq (car entry
) 'forecast_conditions
)
167 (defun google-weather-data->forecast-for-date
(data date
)
168 "Return forecast for DATE from DATA.
169 DATE should be in the same format used by calendar i.e. (MONTH DAY YEAR)."
171 (google-weather-data->forecast data
))))
173 (defun google-weather-data->temperature-symbol
(data)
174 "Return the temperature to be used according to `google-weather-unit-system-temperature-assoc' in DATA."
175 (cdr (assoc (google-weather-data->unit-system data
) google-weather-unit-system-temperature-assoc
)))
177 (provide 'google-weather
)