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