forked from tecosaur/emacs-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wttrin.el
129 lines (114 loc) · 4.68 KB
/
wttrin.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
;;; wttrin.el --- Emacs frontend for weather web service wttr.in -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Carl X. Su
;; Author: Carl X. Su <[email protected]>
;; ono hiroko (kuanyui) <[email protected]>
;; Version: 0.2.0
;; Package-Requires: ((emacs "24.4") (xterm-color "1.0"))
;; Keywords: comm, weather, wttrin
;; URL: https://github.com/bcbcarl/emacs-wttrin
;;
;; Modifications made by @tecosaur
;;; Commentary:
;; Provides the weather information from wttr.in based on your query condition.
;;; Code:
(require 'url)
(require 'xterm-color)
(defgroup wttrin nil
"Emacs frontend for weather web service wttr.in."
:prefix "wttrin-"
:group 'comm)
(defcustom wttrin-default-api-version 1
"Specifies which version of the wttrin API to use."
:group 'wttrn
:type '(choice (const 1) (const 2)))
(defcustom wttrin-default-cities '("Baghdad"
"Beijing"
"Brussels"
"Buenos Aires"
"Cairo"
"Delhi"
"Gurnsey"
"Ho Chi Ming City"
"Hong Kong"
"Istanbul"
"Johannesburg"
"Kuala Lumpur"
"Leipzig"
"Lima"
"London"
"Madrid"
"Manila"
"Mexico City"
"Miami"
"Moscow"
"Mumbai"
"New York"
"Paris"
"Seoul"
"Shanghai"
"Singapore"
"Surat"
"Sydney"
"Tokyo"
"Toronto"
;; and the fun one!
"Moon")
"Specify default cities list for quick completion."
:group 'wttrin
:type 'list)
(defcustom wttrin-default-accept-language '("Accept-Language" . "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4")
"Specify default HTTP request Header for Accept-Language."
:group 'wttrin
:type '(list)
)
(defun wttrin-fetch-raw-string (query &optional api-version)
"Get the weather information based on your QUERY."
(unless api-version (setq api-version wttrin-default-api-version))
(let ((url-user-agent "curl"))
(add-to-list 'url-request-extra-headers wttrin-default-accept-language)
(with-current-buffer
(url-retrieve-synchronously
(concat "http://v" (number-to-string api-version) ".wttr.in/" query)
(lambda (status) (switch-to-buffer (current-buffer))))
(decode-coding-string (buffer-string) 'utf-8))))
(defun wttrin-exit ()
(interactive)
(quit-window t))
(defun wttrin-query (city-name &optional api-version)
"Query weather of CITY-NAME via wttrin, and display the result in new buffer."
(let ((raw-string (wttrin-fetch-raw-string city-name api-version)))
(if (string-match "ERROR" raw-string)
(message "Cannot get weather data. Maybe you inputed a wrong city name?")
(let ((buffer (get-buffer-create (format "*wttr.in - %s*" city-name))))
(switch-to-buffer buffer)
(setq buffer-read-only nil)
(erase-buffer)
(insert (xterm-color-filter raw-string))
(goto-char (point-min))
(save-excursion
(re-search-forward "^$")
(delete-region (point-min) (1+ (point))))
(save-excursion
(while (re-search-forward "(B" nil t)
(delete-region (match-beginning 0) (match-end 0))))
(use-local-map (make-sparse-keymap))
(local-set-key "q" 'wttrin-exit)
(local-set-key "g" 'wttrin)
(setq buffer-read-only t)))))
;;;###autoload
(defun wttrin (city &optional api-version)
"Display weather information for CITY."
(interactive
(cond ((equal current-prefix-arg nil)
(list "" nil))
((equal current-prefix-arg 1)
(list "" 1))
((equal current-prefix-arg 2)
(list "" 2))
(t (list
(completing-read "City name: " wttrin-default-cities nil nil
(when (= (length wttrin-default-cities) 1)
(car wttrin-default-cities)))))))
(wttrin-query city api-version))
(provide 'wttrin)
;;; wttrin.el ends here