pusk/lib/drivers.ml

78 lines
2.3 KiB
OCaml
Raw Normal View History

2023-05-12 20:04:04 +02:00
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 -> raise (Any "Redirect location not found"))
2023-05-12 20:04:04 +02:00
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
raise
(Any ("Failed to download file. HTTP status: " ^ Cohttp.Code.string_of_status status))
2023-05-12 20:04:04 +02:00
;;
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
;;
2023-05-12 20:30:02 +02:00
type driver = Gecko of string
let prepare = function
2023-05-12 20:30:02 +02:00
| Gecko version_driver ->
2023-05-13 10:22:36 +02:00
let driver = fmt "geckodriver-%s" version_driver in
if not (Sys.file_exists driver)
2023-05-12 20:30:02 +02:00
then (
let archive = fmt "./gecko-%s.tar.gz" version_driver in
Lwt_main.run (download_gecko_driver version_driver archive);
(* TODO: Use native version instead of relying on Unix tools *)
2023-05-13 12:37:12 +02:00
ignore (Sys.command (fmt "tar xvzf %s" archive));
ignore (Sys.command (fmt "mv geckodriver %s" driver));
ignore (Sys.command (fmt "rm %s" archive)));
driver
;;
2023-05-13 11:59:09 +02:00
let run_process path args =
let command = fmt "./%s" path in
2023-05-14 01:59:29 +02:00
(* TODO: Log into a log/ folder
* + create file based on time of run *)
2023-05-13 11:59:09 +02:00
let output_file = fmt "%s-output.txt" path in
let out_channel = open_out output_file in
let output_fd = Unix.descr_of_out_channel out_channel in
let pid =
2023-05-13 15:23:55 +02:00
Unix.create_process command (Array.of_list args) output_fd output_fd output_fd
2023-05-13 11:59:09 +02:00
in
Unix.sleepf 0.5;
2023-05-13 11:59:09 +02:00
out_channel, pid
;;
let stop_process data =
let out_channel, pid = data in
close_out out_channel;
Unix.kill pid Sys.sigterm
2023-05-12 20:30:02 +02:00
;;