forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_weather.ml
186 lines (174 loc) · 5.59 KB
/
plugin_weather.ml
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(*
* (c) 2004-2010 Anastasia Gornostaeva
*)
open Common
open Hooks
open Plugin_command
open Http_suck
(* http://weather.noaa.gov/pub/data/observations/metar/decoded/ULLI.TXT *)
let split_lines contents =
let rec aux_split q acc =
let l =
try Some (String.index_from contents q '\n')
with Not_found -> None
in
match l with
| Some v ->
aux_split (v+1) (String.sub contents q (v-q) :: acc)
| None ->
List.rev (String.sub contents q (String.length contents - q) :: acc)
in
aux_split 0 []
let get_fields lines =
List.fold_left (fun acc x ->
let v =
try
let q = String.index x ':' in
let field = String.sub x 0 q in
let value = String.sub x (q+2) (String.length x -q-2) in
let fname =
match field with
| "Wind" -> "wind"
| "Visibility" -> "visibility"
| "Sky conditions" -> "sky"
| "Weather" -> "weather"
| "Temperature" -> "temperature"
| "Dew Point" -> "dewpoint"
| "Relative Humidity" -> "humidity"
| "Pressure (altimeter)" -> "pressure"
| "ob" -> "ob"
| _ ->
raise Not_found
in
Some (fname, value)
with Not_found ->
None
in
match v with
| Some f ->
f :: acc
| None ->
acc
) [] lines
let remove_zero str =
let i = String.length str in
if i > 2 && str.[i-1] = '0' && str.[i-2] = ':' then
String.sub str 0 (i-2)
else
str
let parse_temperature str =
let rec temper = parser
| [< f = get_number; '' '; ''F'; '' '; ''('; c = get_number; '' ';
''C'; >] ->
Some (f, c)
| [< rest >] ->
None
and get_number = parser
| [< ''-'; t = get_digits 0; rest >] ->
(-t)
| [< t = get_digits 0; rest >] ->
t
| [< >] ->
0
and get_digits acc = parser
| [< ' ('0'..'9') as c; rest >] ->
let i = int_of_char c - 48 in
get_digits (acc * 10 + i) rest
| [< >] ->
acc
in
temper (Stream.of_string str)
let parse_weather content =
match split_lines content with
| line1 :: line2 :: rest ->
let map = get_fields rest in
let place =
try
let q = String.index line1 '(' in
String.sub line1 0 (q-1)
with Not_found -> line1 in
let time =
try
let q = String.index line2 '/' in
String.sub line2 (q+2) (String.length line2 - q - 2)
with Not_found -> line2 in
let weather =
try List.assoc "weather" map with _ ->
try List.assoc "sky" map with _ -> ""
in
let temperature =
try
let z = List.assoc "temperature" map in
let data = parse_temperature z in
data
with Not_found -> None
in
let humidity = try List.assoc "humidity" map with Not_found -> "n/a" in
let pressure = try List.assoc "pressure" map with Not_found -> "n/a" in
let wind =
try let w = List.assoc "wind" map in remove_zero w
with _ -> "n/a"
in
let visibility =
try let v = List.assoc "visibility" map in remove_zero v
with _ -> "n/a"
in
Printf.sprintf
"%s - %s / %s%s%shumidity: %s, pressure: %s, wind: %s, visibility: %s"
place time weather (if weather <> "" then ", " else "")
(match temperature with
| Some (f, c) -> Printf.sprintf "%d°C / %d°F, " c f
| None -> "")
humidity pressure wind visibility
| _ ->
"n/a"
let is_noaa_code str =
if String.length str = 4 then
let rec aux_check i =
if i < 4 then
match str.[i] with
| 'a'..'z'
| 'A'..'Z' ->
aux_check (succ i)
| _ -> false
else
true
in
aux_check 0
else
false
let weather xmpp env kind jid_from text =
if is_noaa_code text then
let callback data =
let resp = match data with
| OK (_media, _charset, body) -> (
try
parse_weather body
with _exn ->
Lang.get_msg env.env_lang "plugin_weather_not_parsed" []
)
| Exception exn ->
match exn with
| ClientError ->
Lang.get_msg env.env_lang "plugin_weather_404" []
| ServerError ->
Lang.get_msg env.env_lang "plugin_weather_server_error" []
| _ ->
Lang.get_msg env.env_lang "plugin_weather_server_error" []
in
env.env_message xmpp kind jid_from resp
in
Http_suck.http_get
("http://weather.noaa.gov/pub/data/observations/metar/decoded/" ^
String.uppercase text ^ ".TXT")
callback
else
env.env_message xmpp kind jid_from
(Lang.get_msg env.env_lang "plugin_weather_invalid_syntax" [])
let plugin opts =
add_for_token
(fun _opts xmpp ->
add_commands xmpp [("wz", weather)] opts
)
let _ =
Plugin.add_plugin "weather" plugin