From e62a82fc6210e33f717282ce7f59c91c5f7e86aa Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 13 May 2023 18:00:57 +0200 Subject: [PATCH] add .env in config directory to load credentials, also always unload backend properly in case of a failwith --- .gitignore | 2 ++ README.md | 2 +- bin/main.ml | 20 ++++++++++++++++++-- lib/dune | 2 +- lib/utils.ml | 6 ++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index e20c59f..d59a07d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ _build/ /selenium-*.jar /gecko-*.tar.gz /geckodriver-* + +config/ diff --git a/README.md b/README.md index 42592b6..7bb1e67 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ $ opam switch list ``` ```sh -$ opam install cohttp-lwt-unix tls-lwt +$ opam install cohttp-lwt-unix tls-lwt dotenv ``` ## Name history diff --git a/bin/main.ml b/bin/main.ml index 528e72f..e09b116 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,5 +1,8 @@ open Pusk.Net open Pusk.Drivers +open Pusk.Utils + +exception Any of string let start driver = let name_driver = prepare driver in @@ -14,13 +17,26 @@ let stop (driver_process, session_id) = ;; let main session_id = + (* Load credentials *) + load_dotenv; + let username, password = + match Sys.getenv_opt "TWITTER_USERNAME", Sys.getenv_opt "TWITTER_PASSWORD" with + | Some u, Some p -> u, p + | None, None -> raise (Any "Username and password not set") + | None, Some _ -> raise (Any "Username not set") + | Some _, None -> raise (Any "Password not set") + in + (* Navigate to login page *) ignore (navigate "https://twitter.com/i/flow/login" session_id); (* Extra wait to be sure the page is loaded *) - Unix.sleep 5 + Unix.sleep 5; + (* DEBUG *) + print_endline (fmt "%s:%s" username password) ;; let () = let data = start (Gecko "0.33.0") in - main (snd data); + (try main (snd data) with + | Any why -> print_endline why); stop data ;; diff --git a/lib/dune b/lib/dune index d3a429f..85d2a3b 100644 --- a/lib/dune +++ b/lib/dune @@ -1,4 +1,4 @@ (library (name pusk) (modules utils drivers net json) - (libraries cohttp-lwt-unix yojson)) + (libraries cohttp-lwt-unix yojson dotenv)) diff --git a/lib/utils.ml b/lib/utils.ml index 6fe97ea..0417f5e 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -1 +1,7 @@ let fmt = Printf.sprintf + +let load_dotenv = + (* Load variables *) + let path = "config/.env" in + if Sys.file_exists path then Dotenv.export ~path () +;;