Typo
[google-weather-el.git] / org-google-weather.el
blob3ff0aa39efb76f099f678d850f9c8f67db2ebfd4
1 ;;; org-google-weather.el --- Show Google Weather forecasts in Org agenda.
3 ;; Copyright (C) 2010 Julien Danjou
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: comm
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/>.
23 ;;; Commentary:
24 ;; This module allows to display the weather forecast fetched from Google in
25 ;; your Org agenda.
27 ;; Wednesday 8 September 2010
28 ;; Weather: Pluie, 12/18 ℃
29 ;; Thursday 9 September 2010
30 ;; Weather: Couverture nuageuse partielle, 11/21 ℃
32 ;; Just add the following in an Org buffer:
33 ;; %%(org-google-weather)
35 ;;; Code:
37 (require 'google-weather)
38 (require 'image)
39 (require 'format-spec)
40 (require 'solar)
42 (defgroup org-google-weather nil
43 "Google Weather for Org mode."
44 :group 'comm
45 :group 'org)
47 (defcustom org-google-weather-location calendar-location-name
48 "Default location for org-google-weather."
49 :group 'org-google-weather)
51 (defcustom org-google-weather-format "%i %c, [%l,%h] %s"
52 "String to return to describe the weather.
53 Valid %-sequences are:
54 - %i the icon
55 - %c means the weather condition
56 - %L the supplied location
57 - %C the city the weather is for
58 - %l the lower temperature
59 - %h the higher temperature
60 - %s the temperature unit symbol")
62 (defcustom org-google-weather-cache-time 43200
63 "Define for how many seconds we should cache the weather."
64 :group 'org-google-weather)
66 (defcustom org-google-weather-display-icon-p t
67 "Display icons."
68 :group 'org-google-weather)
70 (defun org-google-weather-get-icon (url)
71 (with-current-buffer
72 (google-weather-retrieve-data-raw url org-google-weather-cache-time)
73 (unless (search-forward "\n\n" nil t)
74 (error "Data not found"))
75 (let ((data (buffer-substring (point) (point-max))))
76 (kill-buffer (current-buffer))
77 data)))
79 ;;;###autoload
80 (defun org-google-weather (&optional location language)
81 "Return Org entry with the weather for LOCATION in LANGUAGE.
82 If LOCATION is not set, use org-google-weather-location."
83 (let* ((location (or location org-google-weather-location))
84 (data (ignore-errors
85 (google-weather-get-data location
86 language
87 org-google-weather-cache-time)))
88 (problem-cause (when data (google-weather-data->problem-cause data)))
89 (forecast (when (and (null problem-cause) data)
90 (google-weather-data->forecast-for-date data date))))
91 (if problem-cause
92 (message "%s: %s" location problem-cause)
93 (when forecast
94 (let ((condition (cadr (assoc 'condition forecast)))
95 (low (cadr (assoc 'low forecast)))
96 (high (cadr (assoc 'high forecast)))
97 (city (google-weather-data->city data))
98 ;; But *they* told me it's just about calling functions!
99 (icon (when (display-images-p)
100 (create-image (org-google-weather-get-icon
101 (cadr (assoc 'icon forecast)))
102 nil t)))
103 (temp-symbol (google-weather-data->temperature-symbol data)))
104 (format-spec org-google-weather-format
105 `((?i . ,(if (and icon org-google-weather-display-icon-p)
106 (propertize "icon"
107 'display
108 (append
109 icon
110 '(:ascent center))
111 'rear-nonsticky '(display))
112 ""))
113 (?c . ,condition)
114 (?L . ,location)
115 (?C . ,city)
116 (?l . ,low)
117 (?h . ,high)
118 (?s . ,temp-symbol))))))))
120 (provide 'org-google-weather)