From 73aafd941c8f66262f7587b8c9d6dc18cf4e6da2 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 15 May 2023 16:28:24 +0200 Subject: [PATCH] add some optional logging --- bin/main.ml | 25 ++++++++++++++++++++----- bin/twitter.ml | 9 ++++++++- lib/utils.ml | 5 ++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 1377cab..9606d86 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -19,13 +19,17 @@ let rec check ctx = (* Loop *) let max_time = 2505600 (* 29 days *) in let recheck timeout = + if ctx.debug + then print_endline (fmt "Waiting for the next check in %d seconds" timeout); Unix.sleep timeout; + if ctx.debug then print_endline "Refresh profile page..."; refresh_page ctx.session_id; Unix.sleep 4; check ctx in (* Time to wait until next check *) let timeout = + if ctx.debug then print_endline "Check latest tweets..."; match find_latest_tweet ctx with | Some tweet_date -> (* Get date of tweet and return time to wait before tweeting *) @@ -34,15 +38,17 @@ let rec check ctx = if diff > max_time then 0 (* Timeout expired *) else max_time - diff (* Timeout for when it will expire *) - | None -> 0 + | None -> + if ctx.debug then print_endline "No tweets found..."; + 0 in if 0 = timeout then ( + if ctx.debug then print_endline "Tweeting..."; (* Tweet and returns to profile page *) tweet ctx "This tweet is for the Twitter's CTO: don't suspend my account for inactivity."; - go_to_profile ctx; (* Wait the maximum time since we just tweeted *) recheck max_time) else (* Wait the amount of time calculated from the post *) @@ -50,8 +56,6 @@ let rec check ctx = ;; let main ctx = - (* 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 @@ -62,6 +66,7 @@ let main ctx = login_twitter ctx username password (Sys.getenv_opt "TWITTER_TOTP"); go_to_profile ctx; (* Start check routine *) + if ctx.debug then print_endline "Start routine..."; check ctx ;; @@ -76,7 +81,17 @@ let handler data (signal : int) = let () = let data = start (Gecko "0.33.0") in Sys.set_signal Sys.sigint (Sys.Signal_handle (handler (fst data))); - let ctx = { session_id = snd data } in + (* Load env variables *) + load_dotenv; + let ctx = + { session_id = snd data + ; debug = + (match Sys.getenv_opt "PUSK_DEBUG" with + | Some boolean -> if String.lowercase_ascii boolean = "true" then true else false + | None -> false) + } + in + if ctx.debug then print_endline "Logging is enabled"; (try main ctx with | Any why -> print_endline why); stop data diff --git a/bin/twitter.ml b/bin/twitter.ml index bd3ec39..cc6041b 100644 --- a/bin/twitter.ml +++ b/bin/twitter.ml @@ -69,13 +69,16 @@ let inject_2fa session_id secret input = ;; let login_twitter ctx username password secret = + if ctx.debug then print_endline "Login to twitter..."; (* Navigate to login page and wait for page loaded*) ignore (navigate ctx.session_id "https://twitter.com/i/flow/login"); Unix.sleep 5; let creds = { username; password } in (* Insert the username *) + if ctx.debug then print_endline "Type username..."; inject_username ctx.session_id creds; (* Find password input *) + if ctx.debug then print_endline "Type password..."; inject_password ctx.session_id creds; (* Detection and injection of 2FA code if needed *) match find ctx.session_id (CSS "input[name='text']") with @@ -83,10 +86,13 @@ let login_twitter ctx username password secret = | _ as l -> if List.length l > 1 then raise (Any "Too many elements found as 2FA input") - else inject_2fa ctx.session_id secret (List.nth l 0) + else ( + if ctx.debug then print_endline "Type 2FA code..."; + inject_2fa ctx.session_id secret (List.nth l 0)) ;; let go_to_profile ctx = + if ctx.debug then print_endline "Locate profile button..."; let profile_button = match find ctx.session_id (XPath "//a[@data-testid='AppTabBar_Profile_Link']") with | [] -> raise (Any (fmt "Profile button not found")) @@ -95,6 +101,7 @@ let go_to_profile ctx = then raise (Any "Too many profile button found") else List.nth l 0 in + if ctx.debug then print_endline "Navigate to user replies..."; ignore (navigate ctx.session_id diff --git a/lib/utils.ml b/lib/utils.ml index 81c530b..cf5cbe0 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -2,7 +2,10 @@ exception Any of string let fmt = Printf.sprintf -type context = { session_id : string } +type context = + { session_id : string + ; debug : bool + } let load_dotenv = (* Load variables *)