1 (** Wrapper around Perl [WWW::Mechanize] class.
3 * Copyright (C) 2004 Merjis Ltd.
5 * $Id: pl_WWW_Mechanize.ml,v 1.4 2005-02-13 16:33:28 rich Exp $
12 let _ = eval "use WWW::Mechanize"
14 class www_mechanize sv =
17 inherit lwp_useragent sv
19 method agent_alias alias =
20 call_method_void sv "agent_alias" [sv_of_string alias]
21 method known_agent_aliases =
22 let svlist = call_method_array sv "known_agent_aliases" [] in
23 List.map string_of_sv svlist
26 call_method_void sv "get" [sv_of_string url]
28 call_method_void sv "reload" []
30 call_method_void sv "back" []
32 method follow_link ?text ?text_regex ?url ?url_regex ?url_abs ?url_abs_regex
33 ?name ?name_regex ?tag ?tag_regex ?n () =
35 let add name f = function
37 | Some p -> args := sv_of_string name :: f p :: !args
39 add "text" sv_of_string text;
40 add "text_regex" sv_of_string text_regex;
41 add "url" sv_of_string url;
42 add "url_regex" sv_of_string url_regex;
43 add "url_abs" sv_of_string url_abs;
44 add "url_abs_regex" sv_of_string url_abs_regex;
45 add "name" sv_of_string name;
46 add "name_regex" sv_of_string name_regex;
47 add "tag" sv_of_string tag;
48 add "tag_regex" sv_of_string tag_regex;
50 call_method_void sv "follow_link" !args
53 let svlist = call_method_array sv "forms" [] in
54 List.map (new Pl_HTML_Form.html_form) svlist
55 method form_number n =
56 let sv = call_method sv "form_number" [sv_of_int n] in
57 new Pl_HTML_Form.html_form sv
58 method form_name name =
59 let sv = call_method sv "form_name" [sv_of_string name] in
60 new Pl_HTML_Form.html_form sv
62 (* XXX There is an arrayref variant of this method, but what
63 * it does is apparently undocumented.
65 method field ?n name value =
66 let args = match n with
67 None -> [sv_of_string name; sv_of_string value]
68 | Some n -> [sv_of_string name; sv_of_string value; sv_of_int n] in
69 call_method_void sv "field" args
70 method set_fields fields =
72 List.iter (fun (k, v) ->
73 (* Note: reversed k, v because we'll reverse the whole list.*)
74 args := sv_of_string v :: sv_of_string k :: !args) fields;
75 let args = List.rev !args in
76 call_method_void sv "set_fields" args
78 method value ?n name =
79 let args = match n with
80 None -> [sv_of_string name]
81 | Some n -> [sv_of_string name; sv_of_int n] in
82 let sv = call_method sv "value" args in
85 (* XXX Doesn't support setting criteria. *)
86 method set_visible names =
87 let names = List.map sv_of_string names in
88 call_method_void sv "set_visible" names
90 method tick ?set name value =
91 let args = match set with
92 None -> [ sv_of_string name; sv_of_string value ]
93 | Some b -> [ sv_of_string name; sv_of_string value; sv_of_bool b ] in
94 call_method_void sv "tick" args
96 method untick name value =
97 call_method_void sv "untick" [ sv_of_string name; sv_of_string value ]
99 method click ?xy button =
100 let args = match xy with
101 None -> [ sv_of_string button ]
102 | Some (x, y) -> [ sv_of_string button; sv_of_int x; sv_of_int y ] in
103 call_method_void sv "click" args
105 call_method_void sv "click" []
106 method click_button ?name ?number ?value ?xy () =
108 let add name f = function
110 | Some p -> args := sv_of_string name :: f p :: !args
112 add "name" sv_of_string name;
113 add "number" sv_of_int number;
114 add "value" sv_of_string value;
118 args := sv_of_string "x" :: sv_of_int x ::
119 sv_of_string "y" :: sv_of_int y :: !args);
120 call_method_void sv "click" !args
122 method select name value =
123 call_method_void sv "select" [ sv_of_string name; sv_of_string value ]
124 method select_multiple name values =
125 let av = av_empty () in
126 List.iter (av_push av) (List.map sv_of_string values);
127 call_method_void sv "select" [ sv_of_string name; arrayref av ]
130 call_method_void sv "submit" []
132 (*method submit_form ?form_number ?form_name ?fields ?button ?xy () *)
135 let sv = call_method sv "success" [] in
138 let sv = call_method sv "uri" [] in
141 let sv = call_method sv "response" [] in
142 new Pl_HTTP_Response.http_response sv
144 let sv = call_method sv "res" [] in
145 new Pl_HTTP_Response.http_response sv
147 let sv = call_method sv "status" [] in
150 let sv = call_method sv "ct" [] in
153 let sv = call_method sv "base" [] in
156 let sv = call_method sv "content" [] in
158 (* method current_forms = *)
160 let svs = call_method_array sv "links" [] in
161 List.map (new www_mechanize_link) svs
163 let sv = call_method sv "is_html" [] in
166 let sv = call_method sv "title" [] in
169 (* method find_link .... = *)
170 (* method find_all_links .... = *)
172 (* method add_header .... = *)
173 (* method delete_header .... = *)
176 let sv = call_method sv "quiet" [] in
179 call_method_void sv "quiet" [sv_of_bool b]
180 (* method stack_depth ... = *)
182 let sv = call_method sv "redirect_ok" [] in
185 (* method request ... = *)
186 (* method update_html ... = *)
190 (* Not much documentation exists for the WWW::Mechanize::Link class. As far
191 * as I can see, the only documented method is #url to return the URL. XXX
193 and www_mechanize_link sv =
200 let sv = call_method sv "url" [] in
205 (* XXX Should be able to pass args to constructor of LWP::UserAgent. *)
206 (* XXX WWW::Mechanize has additional parameters. *)
207 let new_ ?autocheck () =
209 let may f = function None -> () | Some v -> f v in
211 args := sv_of_string "autocheck" :: sv_of_bool v :: !args) autocheck;
212 let sv = call_class_method "WWW::Mechanize" "new" !args in