From 62571b4de5216e7f2eed30860181e3109779ccda Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 13 May 2023 19:58:48 +0200 Subject: [PATCH] move login logic to twitter file --- bin/dune | 1 + bin/main.ml | 13 +++++-------- bin/twitter.ml | 9 +++++++++ lib/utils.ml | 2 ++ 4 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 bin/twitter.ml diff --git a/bin/dune b/bin/dune index 608edfb..db6c5b1 100644 --- a/bin/dune +++ b/bin/dune @@ -1,4 +1,5 @@ (executable (public_name pusk) (name main) + (modules main twitter) (libraries pusk)) diff --git a/bin/main.ml b/bin/main.ml index 93dab77..1d8cc81 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,6 +1,7 @@ open Pusk.Net open Pusk.Drivers open Pusk.Utils +open Twitter let start driver = let name_driver = prepare driver in @@ -14,7 +15,7 @@ let stop (driver_process, session_id) = stop_process driver_process ;; -let main session_id = +let main ctx = (* Load credentials *) load_dotenv; let username, password = @@ -24,17 +25,13 @@ let main session_id = | 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; - (* DEBUG *) - print_endline (fmt "%s:%s" username password) + login_twitter ctx username password ;; let () = let data = start (Gecko "0.33.0") in - (try main (snd data) with + let ctx = { session_id = snd data } in + (try main ctx with | Any why -> print_endline why); stop data ;; diff --git a/bin/twitter.ml b/bin/twitter.ml new file mode 100644 index 0000000..f4023e5 --- /dev/null +++ b/bin/twitter.ml @@ -0,0 +1,9 @@ +open Pusk.Net +open Pusk.Utils + +let login_twitter ctx _username _password = + (* Navigate to login page *) + ignore (navigate "https://twitter.com/i/flow/login" ctx.session_id); + (* Extra wait to be sure the page is loaded *) + Unix.sleep 5 +;; diff --git a/lib/utils.ml b/lib/utils.ml index 104fe69..0f2cb2a 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -2,6 +2,8 @@ exception Any of string let fmt = Printf.sprintf +type context = { session_id : string } + let load_dotenv = (* Load variables *) let path = "config/.env" in