diff --git a/bin/main.ml b/bin/main.ml index f4ccb77..cca447b 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,66 +1,5 @@ -open Lwt.Syntax -open Cohttp_lwt -open Cohttp_lwt_unix - -let fmt = Printf.sprintf - -let rec download uri dest = - let* response, body = Client.get uri in - let status = Response.status response in - let code = Cohttp.Code.code_of_status status in - if Cohttp.Code.is_redirection code - then ( - let headers = Response.headers response in - match Cohttp.Header.get headers "location" with - | Some url -> - let uri = Uri.of_string url in - let redirect_url = Uri.resolve "" uri uri in - download redirect_url dest - | None -> Lwt.fail_with "Redirect location not found") - else if Cohttp.Code.is_success code - then ( - print_endline "Downloading..."; - let stream = Body.to_stream body in - let res = - Lwt_io.with_file ~mode:Lwt_io.output dest (fun chan -> - Lwt_stream.iter_s (Lwt_io.write chan) stream) - in - let* () = res in - print_endline "Download done!"; - Lwt.return_unit) - else - Lwt.fail_with - ("Failed to download file. HTTP status: " ^ Cohttp.Code.string_of_status status) -;; - -let download_selenium version = - let url = - format_of_string - "https://github.com/SeleniumHQ/selenium/releases/download/selenium-%s/selenium-server-%s.jar" - in - download (Uri.of_string (fmt url version version)) (fmt "./selenium-%s.jar" version) -;; - -let download_gecko_driver version output = - let url = - format_of_string - "https://github.com/mozilla/geckodriver/releases/download/v%s/geckodriver-v%s-linux64.tar.gz" - in - download (Uri.of_string (fmt url version version)) output -;; - -let run_program_in_background program args = - let pid = Unix.fork () in - match pid with - | 0 -> - (* Child process *) - let dev_null = Unix.openfile "/dev/null" [ O_WRONLY ] 0o666 in - Unix.dup2 dev_null Unix.stdout; - Unix.dup2 dev_null Unix.stderr; - Unix.close dev_null; - Unix.execvp program (Array.of_list (program :: args)) - | _ -> pid (* Parent process *) -;; +open Pusk.Utils +open Pusk.Selenium_init let () = (* Selenium *) diff --git a/lib/dune b/lib/dune index 7e3ff31..069dba8 100644 --- a/lib/dune +++ b/lib/dune @@ -1,2 +1,4 @@ (library - (name pusk)) + (name pusk) + (modules utils selenium_init) + (libraries cohttp-lwt-unix)) diff --git a/lib/selenium_init.ml b/lib/selenium_init.ml new file mode 100644 index 0000000..f4e9fde --- /dev/null +++ b/lib/selenium_init.ml @@ -0,0 +1,62 @@ +open Lwt.Syntax +open Cohttp_lwt +open Cohttp_lwt_unix +open Utils + +let rec download uri dest = + let* response, body = Client.get uri in + let status = Response.status response in + let code = Cohttp.Code.code_of_status status in + if Cohttp.Code.is_redirection code + then ( + let headers = Response.headers response in + match Cohttp.Header.get headers "location" with + | Some url -> + let uri = Uri.of_string url in + let redirect_url = Uri.resolve "" uri uri in + download redirect_url dest + | None -> Lwt.fail_with "Redirect location not found") + else if Cohttp.Code.is_success code + then ( + print_endline "Downloading..."; + let stream = Body.to_stream body in + let res = + Lwt_io.with_file ~mode:Lwt_io.output dest (fun chan -> + Lwt_stream.iter_s (Lwt_io.write chan) stream) + in + let* () = res in + print_endline "Download done!"; + Lwt.return_unit) + else + Lwt.fail_with + ("Failed to download file. HTTP status: " ^ Cohttp.Code.string_of_status status) +;; + +let download_selenium version = + let url = + format_of_string + "https://github.com/SeleniumHQ/selenium/releases/download/selenium-%s/selenium-server-%s.jar" + in + download (Uri.of_string (fmt url version version)) (fmt "./selenium-%s.jar" version) +;; + +let download_gecko_driver version output = + let url = + format_of_string + "https://github.com/mozilla/geckodriver/releases/download/v%s/geckodriver-v%s-linux64.tar.gz" + in + download (Uri.of_string (fmt url version version)) output +;; + +let run_program_in_background program args = + let pid = Unix.fork () in + match pid with + | 0 -> + (* Child process *) + let dev_null = Unix.openfile "/dev/null" [ O_WRONLY ] 0o666 in + Unix.dup2 dev_null Unix.stdout; + Unix.dup2 dev_null Unix.stderr; + Unix.close dev_null; + Unix.execvp program (Array.of_list (program :: args)) + | _ -> pid (* Parent process *) +;; diff --git a/lib/utils.ml b/lib/utils.ml new file mode 100644 index 0000000..6fe97ea --- /dev/null +++ b/lib/utils.ml @@ -0,0 +1 @@ +let fmt = Printf.sprintf