]> git.donarmstrong.com Git - lib.git/blob - emacs_el/org-google-weather.el
add helm google and zap-to-char
[lib.git] / emacs_el / org-google-weather.el
1 ;;; org-google-weather.el --- Show Google Weather forecasts in Org agenda.
2
3 ;; Copyright (C) 2010 Julien Danjou
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: comm
7
8 ;; This file is NOT part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24 ;; This module allows to display the weather forecast fetched from Google in
25 ;; your Org agenda.
26 ;;
27 ;;     Wednesday   8 September 2010
28 ;;       Weather:    Pluie, 12/18 ℃
29 ;;     Thursday    9 September 2010
30 ;;       Weather:    Couverture nuageuse partielle, 11/21 ℃
31 ;;
32 ;; Just add the following in an Org buffer:
33 ;; %%(org-google-weather)
34 ;;
35 ;;; Code:
36
37 (require 'google-weather)
38 (require 'image)
39 (require 'format-spec)
40 (require 'solar)
41
42 (defgroup org-google-weather nil
43   "Google Weather for Org mode."
44   :group 'comm
45   :group 'org)
46
47 (defcustom org-google-weather-location calendar-location-name
48   "Default location for org-google-weather."
49   :group 'org-google-weather)
50
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")
61
62 (defcustom org-google-weather-cache-time 43200
63   "Define for how many seconds we should cache the weather."
64   :group 'org-google-weather)
65
66 (defcustom org-google-weather-display-icon-p t
67   "Display icons."
68   :group 'org-google-weather)
69
70 (defcustom org-google-weather-icon-directory "/usr/share/icons/gnome/16x16/status"
71   "Directory where to find icon listed in `org-google-weather-icon-alist'."
72   :group 'org-google-weather)
73
74 (defcustom org-google-weather-icon-alist
75   '((chance_of_rain . "weather-showers-scattered.png")
76     (chance_of_snow . "weather-snow.png")
77     (chance_of_storm . "weather-storm.png")
78     (cn_cloudy . "weather-overcast.png")
79     (cn_heavyrun . "weather-showers.png")
80     (cn_sunny . "weather-clear.png")
81     (cloudy . "weather-overcast.png")
82     (dust . "weather-fog.png")
83     (flurries . "weather-storm.png")
84     (fog . "weather-fog.png")
85     (haze . "weather-fog.png")
86     (icy . "weather-snow.png")
87     (jp_sunny . "weather-clear.png")
88     (jp_cloudy . "weather-overcast.png")
89     (mist . "weather-storm.png")
90     (mostly_cloudy . "weather-overcast.png")
91     (mostly_sunny . "weather-clear.png")
92     (partly_cloudy . "weather-few-clouds.png")
93     (rain . "weather-showers.png")
94     (rain_snow . "weather-snow.png")
95     (sleet . "weather-snow.png")
96     (smoke . "weather-fog.png")
97     (snow . "weather-snow.png")
98     (storm . "weather-storm.png")
99     (thunderstorm . "weather-storm.png")
100     (sunny . "weather-clear.png"))
101   "Icons to use to illustrate the weather."
102   :group 'org-google-weather)
103
104 (defcustom org-google-weather-use-google-icons nil
105   "Fetch icons from Google or use local ones.
106 If you decide to use local ones, you should check
107 `org-google-weather-icon-directory' and
108 `org-google-weather-icon-alist'. Otherwise, if you want to use
109 icons from Google, you have nothing to do."
110   :group 'org-google-weather
111   :type 'boolean)
112
113 (defun org-google-weather-get-icon (url)
114   (with-current-buffer
115       (google-weather-retrieve-data-raw url org-google-weather-cache-time)
116     (goto-char (point-min))
117     (unless (search-forward "\n\n" nil t)
118       (error "Data not found"))
119     (let ((data (buffer-substring (point) (point-max))))
120       (kill-buffer (current-buffer))
121       data)))
122
123 ;;;###autoload
124 (defun org-google-weather (&optional location language)
125   "Return Org entry with the weather for LOCATION in LANGUAGE.
126 If LOCATION is not set, use org-google-weather-location."
127   (let* ((location (or location org-google-weather-location))
128          (data (ignore-errors
129                  (google-weather-get-data location
130                                           language
131                                           org-google-weather-cache-time)))
132          (problem-cause (when data (google-weather-data->problem-cause data)))
133          (forecast (when (and (null problem-cause) data)
134                      (google-weather-data->forecast-for-date data date))))
135     (if problem-cause
136         (message "%s: %s" location problem-cause)
137       (when forecast
138         (let ((condition (cadr (assoc 'condition forecast)))
139               (low (cadr (assoc 'low forecast)))
140               (high (cadr (assoc 'high forecast)))
141               (city (google-weather-data->city data))
142               ;; But *they* told me it's just about calling functions!
143               (icon (when (and org-google-weather-display-icon-p (display-images-p))
144                       (if org-google-weather-use-google-icons
145                           (create-image (org-google-weather-get-icon
146                                          (cadr (assoc 'icon forecast)))
147                                         nil t)
148                         (create-image
149                          (concat
150                           org-google-weather-icon-directory
151                           "/"
152                           (cdr
153                            (assoc
154                             (intern
155                              (file-name-sans-extension
156                               (file-name-nondirectory
157                                (cadr (assoc 'icon forecast)))))
158                             org-google-weather-icon-alist)))))))
159               (temp-symbol (google-weather-data->temperature-symbol data)))
160           (format-spec org-google-weather-format
161                        `((?i . ,(if icon
162                                     (propertize "icon"
163                                                 'display
164                                                 (append
165                                                  icon '(:ascent center))
166                                                 'rear-nonsticky '(display))
167                                   ""))
168                          (?c . ,condition)
169                          (?L . ,location)
170                          (?C . ,city)
171                          (?l . ,low)
172                          (?h . ,high)
173                          (?s . ,temp-symbol))))))))
174
175 (provide 'org-google-weather)