]> git.donarmstrong.com Git - lib.git/blob - emacs_el/org-google-weather.el
update org-google-weather
[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
41 (defgroup org-google-weather nil
42   "Google Weather for Org mode."
43   :group 'comm)
44
45 (defcustom org-google-weather-location "Paris"
46   "Default location for org-google-weather."
47   :group 'org-google-weather)
48
49 (defcustom org-google-weather-format "%i %c, %l-%h %s"
50   "String to return to describe the weather.
51 Valid %-sequences are:
52   - %i the icon
53   - %c means the weather condition
54   - %L the supplied location
55   - %C the city the weather is for
56   - %l the lower temperature
57   - %h the higher temperature
58   - %s the temperature unit symbol")
59
60 (defcustom org-google-weather-cache-time 43200
61   "Define for how many seconds we should cache the weather."
62   :group 'org-google-weather)
63
64 (defcustom org-google-weather-display-icon-p t
65   "Display icons."
66   :group 'org-google-weather)
67
68 (defcustom org-google-weather-icon-directory "/usr/share/icons/gnome/16x16/status"
69   "Directory where to find icon listed in `org-google-weather-icon-alist'."
70   :group 'org-google-weather)
71
72 (defcustom org-google-weather-icon-alist
73   '((chance_of_rain . "weather-showers-scattered.png")
74     (chance_of_snow . "weather-snow.png")
75     (chance_of_storm . "weather-storm.png")
76     (cn_heavyrun . "weather-showers.png")
77     (cloudy . "weather-overcast.png")
78     (dust . "weather-fog.png")
79     (flurries . "weather-storm.png")
80     (fog . "weather-fog.png")
81     (haze . "weather-fog.png")
82     (icy . "weather-snow.png")
83     (mist . "weather-storm.png")
84     (mostly_cloudy . "weather-overcast.png")
85     (mostly_sunny . "weather-clear.png")
86     (partly_cloudy . "weather-few-clouds.png")
87     (rain . "weather-showers.png")
88     (sleet . "weather-snow.png")
89     (smoke . "weather-fog.png")
90     (snow . "weather-snow.png")
91     (storm . "weather-storm.png")
92     (thunderstorm . "weather-storm.png")
93     (sunny . "weather-clear.png"))
94   "Icons to used to illustrate the weather.")
95
96 ;;;###autoload
97 (defun org-google-weather (&optional location language)
98   "Return Org entry with the weather for LOCATION in LANGUAGE.
99 If LOCATION is not set, use org-google-weather-location."
100   (let* ((data (google-weather-get-data (or location
101                                             org-google-weather-location)
102                                         language
103                                         org-google-weather-cache-time))
104          (forecast (google-weather-data->forecast-for-date data date)))
105     (when forecast
106       (let ((condition (cadr (assoc 'condition forecast)))
107             (low (cadr (assoc 'low forecast)))
108             (high (cadr (assoc 'high forecast)))
109             (city (google-weather-data->city data))
110             ;; But *they* told me it's just about calling functions!
111             (icon (cdr
112                    (assoc
113                     (intern
114                      (file-name-sans-extension
115                       (file-name-nondirectory
116                        (cadr (assoc 'icon forecast)))))
117                     org-google-weather-icon-alist)))
118             (temp-symbol (google-weather-data->temperature-symbol data)))
119         (format-spec org-google-weather-format
120                      `((?i . ,(if org-google-weather-display-icon-p
121                                   (concat (propertize "icon"
122                                                       'display
123                                                       (create-image
124                                                        (concat
125                                                         org-google-weather-icon-directory
126                                                         "/"
127                                                         icon))
128                                                       'rear-nonsticky '(display))
129                                           " ")
130                                 ""))
131                        (?c . ,condition)
132                        (?L . ,location)
133                        (?C . ,city)
134                        (?l . ,low)
135                        (?h . ,high)
136                        (?s . ,temp-symbol)))))))
137
138 (provide 'org-google-weather)