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
37 (defgroup google-weather nil
41 (defcustom google-weather-use-https t
42 "Default protocol to use to access the Google Weather API."
43 :group
'google-weather
)
45 (defconst google-weather-url
46 "www.google.com/ig/api"
47 "URL of the Google Weather API.")
49 (defconst google-weather-image-url
50 "http://www.google.com"
51 "URL prefix for images.")
53 (defcustom google-weather-unit-system-temperature-assoc
56 "Find temperature symbol from unit system."
57 :group
'google-weather
)
59 (defun google-weather-cache-expired (url expire-time
)
60 "Check if URL is cached for more than EXPIRE-TIME."
61 (cond (url-standalone-mode
62 (not (file-exists-p (url-cache-create-filename url
))))
63 (t (let ((cache-time (url-is-cached url
)))
68 (seconds-to-time expire-time
))
72 (defun google-weather-cache-fetch (url)
73 "Fetch URL from the cache."
74 (with-current-buffer (generate-new-buffer " *temp*")
75 (url-cache-extract (url-cache-create-filename url
))
78 (defun google-weather-retrieve-data (url &optional expire-time
)
79 "Retrieve URL and return its data as string.
80 If EXPIRE-TIME is set, the data will be fetched from the cache if
81 their are not older than EXPIRE-TIME seconds. Otherwise, they
82 will be fetched and then cached. Therefore, setting EXPIRE-TIME
83 to 0 force a cache renewal."
84 (let* ((expired (if expire-time
85 (google-weather-cache-expired url expire-time
)
88 (url-retrieve-synchronously url
)
89 (google-weather-cache-fetch url
)))
91 (with-current-buffer buffer
92 (goto-char (point-min))
93 (unless (search-forward "\n\n" nil t
)
94 (error "Data not found"))
97 (detect-coding-region (point) (point-max) t
))
98 (set-buffer-multibyte t
)
99 (setq data
(xml-parse-region (point) (point-max)))
100 (when (and expired expire-time
)
101 (url-store-in-cache (current-buffer)))
102 (kill-buffer (current-buffer))
105 (defun google-weather-build-url (location &optional language
)
106 "Build URL to retrieve weather for LOCATION in LANGUAGE."
107 (concat "http" (when google-weather-use-https
"s") "://" google-weather-url
"?weather=" (url-hexify-string location
)
109 (concat "&hl=" language
))))
111 (defun google-weather-get-data (location &optional language expire-time
)
112 "Get weather data for LOCATION in LANGUAGE.
113 See `google-weather-retrieve-data' for the use of EXPIRE-TIME."
114 (google-weather-retrieve-data
115 (google-weather-build-url location language
) expire-time
))
117 (defun google-weather-data->weather
(data)
118 "Return all weather information from DATA."
119 (cddr (assoc 'weather
(cdr (assoc 'xml_api_reply data
)))))
121 (defun google-weather-data->forecast-information
(data)
122 "Return the forecast information of DATA."
123 (cddr (assoc 'forecast_information
(google-weather-data->weather data
))))
125 (defun google-weather-assoc (key data
)
126 "Extract value of field KEY from DATA."
127 (cdr (assoc 'data
(cadr (assoc key data
)))))
129 (defun google-weather-data->city
(data)
130 "Return the city where the DATA come from."
131 (google-weather-assoc
133 (google-weather-data->forecast-information data
)))
135 (defun google-weather-data->postal-code
(data)
136 "Return the postal code where the DATA come from."
137 (google-weather-assoc
139 (google-weather-data->forecast-information data
)))
141 (defun google-weather-data->unit-system
(data)
142 "Return the unit system used for DATA."
143 (google-weather-assoc
145 (google-weather-data->forecast-information data
)))
147 (defun google-weather-data->forecast-date
(data)
148 "Return the unit system used for DATA."
149 (google-weather-assoc
151 (google-weather-data->forecast-information data
)))
153 (defun google-weather-data->forecast
(data)
154 "Get forecast list from DATA."
155 ;; Compute date of the forecast in the same format as `current-time'
156 (let ((date (apply 'encode-time
158 (concat (google-weather-data->forecast-date data
) " 00:00:00")))))
161 (let* ((forecast-date (decode-time date
))
162 (forecast-encoded-date (list (nth 4 forecast-date
)
163 (nth 3 forecast-date
)
164 (nth 5 forecast-date
))))
165 ;; Add one day to `date'
166 (setq date
(time-add date
(days-to-time 1)))
167 `(,forecast-encoded-date
168 (low ,(google-weather-assoc 'low forecast
))
169 (high ,(google-weather-assoc 'high forecast
))
170 (icon ,(concat google-weather-image-url
171 (google-weather-assoc 'icon forecast
)))
172 (condition ,(google-weather-assoc 'condition forecast
)))))
173 (loop for entry in
(google-weather-data->weather data
)
174 when
(eq (car entry
) 'forecast_conditions
)
177 (defun google-weather-data->forecast-for-date
(data date
)
178 "Return forecast for DATE from DATA.
179 DATE should be in the same format used by calendar,
180 i.e. (MONTH DAY YEAR)."
181 (cdr (assoc date
(google-weather-data->forecast data
))))
183 (defun google-weather-data->temperature-symbol
(data)
184 "Return the temperature to be used according in DATA.
185 It uses `google-weather-unit-system-temperature-assoc' to find a
187 (cdr (assoc (google-weather-data->unit-system data
) google-weather-unit-system-temperature-assoc
)))
189 (provide 'google-weather
)