Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 187 lines (174 sloc) 5.725 kb
fab830d bugfix
Anastasia Gornostaeva authored
1 (*
08aac6a @ermine Remove email
authored
2 * (c) 2004-2010 Anastasia Gornostaeva
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
3 *)
cc68359 Added copyright
Anastasia Gornostaeva authored
4
19debed Big rework, added many new plugins
Anastasia Gornostaeva authored
5 open Common
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
6 open Hooks
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
7 open Plugin_command
12494c9 in changelog
Anastasia Gornostaeva authored
8 open Http_suck
34c9f2c initial import
Anastasia Gornostaeva authored
9
10 (* http://weather.noaa.gov/pub/data/observations/metar/decoded/ULLI.TXT *)
11
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
12 let split_lines contents =
13 let rec aux_split q acc =
14 let l =
15 try Some (String.index_from contents q '\n')
16 with Not_found -> None
17 in
18 match l with
19 | Some v ->
20 aux_split (v+1) (String.sub contents q (v-q) :: acc)
21 | None ->
22 List.rev (String.sub contents q (String.length contents - q) :: acc)
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
23 in
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
24 aux_split 0 []
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
25
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
26 let get_fields lines =
27 List.fold_left (fun acc x ->
28 let v =
29 try
30 let q = String.index x ':' in
31 let field = String.sub x 0 q in
32 let value = String.sub x (q+2) (String.length x -q-2) in
33 let fname =
34 match field with
35 | "Wind" -> "wind"
36 | "Visibility" -> "visibility"
37 | "Sky conditions" -> "sky"
38 | "Weather" -> "weather"
39 | "Temperature" -> "temperature"
40 | "Dew Point" -> "dewpoint"
41 | "Relative Humidity" -> "humidity"
42 | "Pressure (altimeter)" -> "pressure"
43 | "ob" -> "ob"
44 | _ ->
45 raise Not_found
46 in
47 Some (fname, value)
48 with Not_found ->
49 None
50 in
51 match v with
52 | Some f ->
53 f :: acc
54 | None ->
55 acc
56 ) [] lines
57
58 let remove_zero str =
59 let i = String.length str in
60 if i > 2 && str.[i-1] = '0' && str.[i-2] = ':' then
61 String.sub str 0 (i-2)
62 else
63 str
64
65 let parse_temperature str =
66 let rec temper = parser
fab830d bugfix
Anastasia Gornostaeva authored
67 | [< f = get_number; '' '; ''F'; '' '; ''('; c = get_number; '' ';
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
68 ''C'; >] ->
69 Some (f, c)
70 | [< rest >] ->
71 None
fab830d bugfix
Anastasia Gornostaeva authored
72 and get_number = parser
73 | [< ''-'; t = get_digits 0; rest >] ->
74 (-t)
75 | [< t = get_digits 0; rest >] ->
76 t
77 | [< >] ->
78 0
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
79 and get_digits acc = parser
80 | [< ' ('0'..'9') as c; rest >] ->
81 let i = int_of_char c - 48 in
82 get_digits (acc * 10 + i) rest
83 | [< >] ->
84 acc
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
85 in
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
86 temper (Stream.of_string str)
fab830d bugfix
Anastasia Gornostaeva authored
87
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
88 let parse_weather content =
89 match split_lines content with
90 | line1 :: line2 :: rest ->
91 let map = get_fields rest in
92 let place =
93 try
94 let q = String.index line1 '(' in
95 String.sub line1 0 (q-1)
96 with Not_found -> line1 in
97 let time =
98 try
99 let q = String.index line2 '/' in
100 String.sub line2 (q+2) (String.length line2 - q - 2)
101 with Not_found -> line2 in
102 let weather =
103 try List.assoc "weather" map with _ ->
104 try List.assoc "sky" map with _ -> ""
105 in
106 let temperature =
107 try
108 let z = List.assoc "temperature" map in
109 let data = parse_temperature z in
110 data
111 with Not_found -> None
112 in
113 let humidity = try List.assoc "humidity" map with Not_found -> "n/a" in
114 let pressure = try List.assoc "pressure" map with Not_found -> "n/a" in
115 let wind =
116 try let w = List.assoc "wind" map in remove_zero w
117 with _ -> "n/a"
118 in
119 let visibility =
120 try let v = List.assoc "visibility" map in remove_zero v
121 with _ -> "n/a"
122 in
123 Printf.sprintf
124 "%s - %s / %s%s%shumidity: %s, pressure: %s, wind: %s, visibility: %s"
125 place time weather (if weather <> "" then ", " else "")
126 (match temperature with
127 | Some (f, c) -> Printf.sprintf "%d°C / %d°F, " c f
128 | None -> "")
129 humidity pressure wind visibility
130 | _ ->
131 "n/a"
132
133 let is_noaa_code str =
134 if String.length str = 4 then
135 let rec aux_check i =
136 if i < 4 then
137 match str.[i] with
138 | 'a'..'z'
139 | 'A'..'Z' ->
140 aux_check (succ i)
141 | _ -> false
142 else
143 true
144 in
145 aux_check 0
146 else
147 false
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
148
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
149 let weather xmpp env kind jid_from text =
eaeae96 Weather plugin: removed pcre, added pressure
Anastasia Gornostaeva authored
150 if is_noaa_code text then
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
151 let callback data =
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
152 let resp = match data with
153 | OK (_media, _charset, body) -> (
a780a3a * sulci/plugin_translate.ml: Bugfix (forgot to raise Filtered except…
Anastasia Gornostaeva authored
154 try
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
155 parse_weather body
a780a3a * sulci/plugin_translate.ml: Bugfix (forgot to raise Filtered except…
Anastasia Gornostaeva authored
156 with _exn ->
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
157 Lang.get_msg env.env_lang "plugin_weather_not_parsed" []
a780a3a * sulci/plugin_translate.ml: Bugfix (forgot to raise Filtered except…
Anastasia Gornostaeva authored
158 )
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
159 | Exception exn ->
160 match exn with
161 | ClientError ->
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
162 Lang.get_msg env.env_lang "plugin_weather_404" []
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
163 | ServerError ->
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
164 Lang.get_msg env.env_lang "plugin_weather_server_error" []
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
165 | _ ->
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
166 Lang.get_msg env.env_lang "plugin_weather_server_error" []
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
167 in
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
168 env.env_message xmpp kind jid_from resp
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
169 in
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
170 Http_suck.http_get
171 ("http://weather.noaa.gov/pub/data/observations/metar/decoded/" ^
172 String.uppercase text ^ ".TXT")
173 callback
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
174 else
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
175 env.env_message xmpp kind jid_from
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
176 (Lang.get_msg env.env_lang "plugin_weather_invalid_syntax" [])
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
177
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
178
179 let plugin opts =
f80f254 More or less working scheme for hooks, not completed
Anastasia Gornostaeva authored
180 add_for_token
181 (fun _opts xmpp ->
182 add_commands xmpp [("wz", weather)] opts
183 )
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
184
34c9f2c initial import
Anastasia Gornostaeva authored
185 let _ =
f80f254 More or less working scheme for hooks, not completed
Anastasia Gornostaeva authored
186 Plugin.add_plugin "weather" plugin
Something went wrong with that request. Please try again.