(* Given an AFN record name, generate html that redirects us to the
family search page for that record. First we retrieve a web page
that contains the recid, then we use that to create the redirect
url. *)
(*
curl -s 'http://www.familysearch.org/Eng/Search/customsearchresults.asp?date_range_index=0&type=0&LDS=0&event_index=0&date_range=0&file_number=1TF0-QKD&standardize=N&submit=Search&clear=Clear' ->
af/individual_record.asp?recid=44901677&lds=0®ion=%2D1®ionfriendly=&juris1=&juris2=&juris3=&juris4=®ionfriendly=&juris1friendly=&juris2friendly=&juris3friendly=&juris4friendly=
*)
let cgivars = ["file_number", ""]
(* Make a function that returns the value of any cgi variable,
either as passed in the url or the default. *)
let cgi =
Cgi.cgiinit ();
(*Printf.eprintf "List of CGI arguments:
\n";
List.iter
(fun (name, arg) -> Printf.eprintf " %s=%s\n" name arg)
(Cgi.arguments ());*)
let tmp = Hashtbl.create 20 in
List.iter (fun (name, defval) ->
Hashtbl.add tmp name
(try Cgi.string_of_cgivar name
with Not_found -> defval)) cgivars;
(* If a submit button was pressed, modify the cgi variable values *)
(*
List.iter
(fun name ->
try
let value = Cgi.string_of_cgivar name
and selected_value = List.assoc name buttons in
if value = selected_value then Hashtbl.replace tmp "view" name;
with Not_found -> ())
cgiinputs; *)
Hashtbl.find tmp
let new_request_uri =
(Unix.getenv "SCRIPT_NAME") ^ "?" ^
(String.concat "&"
(List.map
(fun (key, value) -> key ^ "=" ^ (cgi key))
cgivars))
let recid_url_re =
Str.regexp "href=['\"]\\(af/individual_record\\.asp\\?recid=[^'\"]*\\)['\"]"
let channel_to_string ichan =
let buffer = Buffer.create 16 in
try while true do Buffer.add_char buffer (input_char ichan) done; ""
with End_of_file -> Buffer.contents buffer
let process_to_string cmd =
(*prerr_endline ("process_to_string \"" ^ cmd ^ "\"");*)
let ichan = Unix.open_process_in cmd in
let html = channel_to_string ichan in
ignore (Unix.close_process_in ichan);
html
let _ =
Util.init_backtrace ();
let file = cgi "file_number" in
(*prerr_endline ("\nfile: " ^ file);*)
let url =
("curl -s '" ^
"http://www.familysearch.org/Eng/Search/customsearchresults.asp?" ^
"date_range_index=0&type=0&LDS=0&event_index=0&date_range=0&" ^
"file_number=" ^ file ^ "&standardize=N&submit=Search&clear=Clear'") in
(*prerr_endline ("url=" ^ url);*)
let html = process_to_string url in
(*prerr_endline ("html=" ^ html);*)
print_endline "Content-type: text/html";
try
begin match html with "" -> raise End_of_file | _ -> () end;
let pos = Str.search_forward recid_url_re html 0 in
(*prerr_endline (Printf.sprintf "pos=%d" pos);*)
let prefix = "http://www.familysearch.org/Eng/Search/" in
let url = prefix ^ Str.matched_group 1 html in
(* If the request URI doesn't include the values of the CGI variables,
re-issue the corrected request and exit. *)
print_endline ("Location: " ^ url ^ "\n")
with
End_of_file ->
print_endline ("\nCurl process failed\n")
| Not_found ->
print_endline ("\nCan't find record id:\n
\n" ^ html ^ "
")