add .env in config directory to load credentials, also always unload backend properly in case of a failwith

This commit is contained in:
Mylloon 2023-05-13 18:00:57 +02:00
parent cb691fc16b
commit e62a82fc62
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 28 additions and 4 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@ _build/
/selenium-*.jar /selenium-*.jar
/gecko-*.tar.gz /gecko-*.tar.gz
/geckodriver-* /geckodriver-*
config/

View file

@ -22,7 +22,7 @@ $ opam switch list
``` ```
```sh ```sh
$ opam install cohttp-lwt-unix tls-lwt $ opam install cohttp-lwt-unix tls-lwt dotenv
``` ```
## Name history ## Name history

View file

@ -1,5 +1,8 @@
open Pusk.Net open Pusk.Net
open Pusk.Drivers open Pusk.Drivers
open Pusk.Utils
exception Any of string
let start driver = let start driver =
let name_driver = prepare driver in let name_driver = prepare driver in
@ -14,13 +17,26 @@ let stop (driver_process, session_id) =
;; ;;
let main 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); ignore (navigate "https://twitter.com/i/flow/login" session_id);
(* Extra wait to be sure the page is loaded *) (* 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 () =
let data = start (Gecko "0.33.0") in let data = start (Gecko "0.33.0") in
main (snd data); (try main (snd data) with
| Any why -> print_endline why);
stop data stop data
;; ;;

View file

@ -1,4 +1,4 @@
(library (library
(name pusk) (name pusk)
(modules utils drivers net json) (modules utils drivers net json)
(libraries cohttp-lwt-unix yojson)) (libraries cohttp-lwt-unix yojson dotenv))

View file

@ -1 +1,7 @@
let fmt = Printf.sprintf let fmt = Printf.sprintf
let load_dotenv =
(* Load variables *)
let path = "config/.env" in
if Sys.file_exists path then Dotenv.export ~path ()
;;