add some optional logging
This commit is contained in:
parent
39a5e10530
commit
73aafd941c
3 changed files with 32 additions and 7 deletions
25
bin/main.ml
25
bin/main.ml
|
@ -19,13 +19,17 @@ let rec check ctx =
|
||||||
(* Loop *)
|
(* Loop *)
|
||||||
let max_time = 2505600 (* 29 days *) in
|
let max_time = 2505600 (* 29 days *) in
|
||||||
let recheck timeout =
|
let recheck timeout =
|
||||||
|
if ctx.debug
|
||||||
|
then print_endline (fmt "Waiting for the next check in %d seconds" timeout);
|
||||||
Unix.sleep timeout;
|
Unix.sleep timeout;
|
||||||
|
if ctx.debug then print_endline "Refresh profile page...";
|
||||||
refresh_page ctx.session_id;
|
refresh_page ctx.session_id;
|
||||||
Unix.sleep 4;
|
Unix.sleep 4;
|
||||||
check ctx
|
check ctx
|
||||||
in
|
in
|
||||||
(* Time to wait until next check *)
|
(* Time to wait until next check *)
|
||||||
let timeout =
|
let timeout =
|
||||||
|
if ctx.debug then print_endline "Check latest tweets...";
|
||||||
match find_latest_tweet ctx with
|
match find_latest_tweet ctx with
|
||||||
| Some tweet_date ->
|
| Some tweet_date ->
|
||||||
(* Get date of tweet and return time to wait before tweeting *)
|
(* Get date of tweet and return time to wait before tweeting *)
|
||||||
|
@ -34,15 +38,17 @@ let rec check ctx =
|
||||||
if diff > max_time
|
if diff > max_time
|
||||||
then 0 (* Timeout expired *)
|
then 0 (* Timeout expired *)
|
||||||
else max_time - diff (* Timeout for when it will expire *)
|
else max_time - diff (* Timeout for when it will expire *)
|
||||||
| None -> 0
|
| None ->
|
||||||
|
if ctx.debug then print_endline "No tweets found...";
|
||||||
|
0
|
||||||
in
|
in
|
||||||
if 0 = timeout
|
if 0 = timeout
|
||||||
then (
|
then (
|
||||||
|
if ctx.debug then print_endline "Tweeting...";
|
||||||
(* Tweet and returns to profile page *)
|
(* Tweet and returns to profile page *)
|
||||||
tweet
|
tweet
|
||||||
ctx
|
ctx
|
||||||
"This tweet is for the Twitter's CTO: don't suspend my account for inactivity.";
|
"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 *)
|
(* Wait the maximum time since we just tweeted *)
|
||||||
recheck max_time)
|
recheck max_time)
|
||||||
else (* Wait the amount of time calculated from the post *)
|
else (* Wait the amount of time calculated from the post *)
|
||||||
|
@ -50,8 +56,6 @@ let rec check ctx =
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let main ctx =
|
let main ctx =
|
||||||
(* Load credentials *)
|
|
||||||
load_dotenv;
|
|
||||||
let username, password =
|
let username, password =
|
||||||
match Sys.getenv_opt "TWITTER_USERNAME", Sys.getenv_opt "TWITTER_PASSWORD" with
|
match Sys.getenv_opt "TWITTER_USERNAME", Sys.getenv_opt "TWITTER_PASSWORD" with
|
||||||
| Some u, Some p -> u, p
|
| Some u, Some p -> u, p
|
||||||
|
@ -62,6 +66,7 @@ let main ctx =
|
||||||
login_twitter ctx username password (Sys.getenv_opt "TWITTER_TOTP");
|
login_twitter ctx username password (Sys.getenv_opt "TWITTER_TOTP");
|
||||||
go_to_profile ctx;
|
go_to_profile ctx;
|
||||||
(* Start check routine *)
|
(* Start check routine *)
|
||||||
|
if ctx.debug then print_endline "Start routine...";
|
||||||
check ctx
|
check ctx
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
@ -76,7 +81,17 @@ let handler data (signal : int) =
|
||||||
let () =
|
let () =
|
||||||
let data = start (Gecko "0.33.0") in
|
let data = start (Gecko "0.33.0") in
|
||||||
Sys.set_signal Sys.sigint (Sys.Signal_handle (handler (fst data)));
|
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
|
(try main ctx with
|
||||||
| Any why -> print_endline why);
|
| Any why -> print_endline why);
|
||||||
stop data
|
stop data
|
||||||
|
|
|
@ -69,13 +69,16 @@ let inject_2fa session_id secret input =
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let login_twitter ctx username password secret =
|
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*)
|
(* Navigate to login page and wait for page loaded*)
|
||||||
ignore (navigate ctx.session_id "https://twitter.com/i/flow/login");
|
ignore (navigate ctx.session_id "https://twitter.com/i/flow/login");
|
||||||
Unix.sleep 5;
|
Unix.sleep 5;
|
||||||
let creds = { username; password } in
|
let creds = { username; password } in
|
||||||
(* Insert the username *)
|
(* Insert the username *)
|
||||||
|
if ctx.debug then print_endline "Type username...";
|
||||||
inject_username ctx.session_id creds;
|
inject_username ctx.session_id creds;
|
||||||
(* Find password input *)
|
(* Find password input *)
|
||||||
|
if ctx.debug then print_endline "Type password...";
|
||||||
inject_password ctx.session_id creds;
|
inject_password ctx.session_id creds;
|
||||||
(* Detection and injection of 2FA code if needed *)
|
(* Detection and injection of 2FA code if needed *)
|
||||||
match find ctx.session_id (CSS "input[name='text']") with
|
match find ctx.session_id (CSS "input[name='text']") with
|
||||||
|
@ -83,10 +86,13 @@ let login_twitter ctx username password secret =
|
||||||
| _ as l ->
|
| _ as l ->
|
||||||
if List.length l > 1
|
if List.length l > 1
|
||||||
then raise (Any "Too many elements found as 2FA input")
|
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 =
|
let go_to_profile ctx =
|
||||||
|
if ctx.debug then print_endline "Locate profile button...";
|
||||||
let profile_button =
|
let profile_button =
|
||||||
match find ctx.session_id (XPath "//a[@data-testid='AppTabBar_Profile_Link']") with
|
match find ctx.session_id (XPath "//a[@data-testid='AppTabBar_Profile_Link']") with
|
||||||
| [] -> raise (Any (fmt "Profile button not found"))
|
| [] -> raise (Any (fmt "Profile button not found"))
|
||||||
|
@ -95,6 +101,7 @@ let go_to_profile ctx =
|
||||||
then raise (Any "Too many profile button found")
|
then raise (Any "Too many profile button found")
|
||||||
else List.nth l 0
|
else List.nth l 0
|
||||||
in
|
in
|
||||||
|
if ctx.debug then print_endline "Navigate to user replies...";
|
||||||
ignore
|
ignore
|
||||||
(navigate
|
(navigate
|
||||||
ctx.session_id
|
ctx.session_id
|
||||||
|
|
|
@ -2,7 +2,10 @@ exception Any of string
|
||||||
|
|
||||||
let fmt = Printf.sprintf
|
let fmt = Printf.sprintf
|
||||||
|
|
||||||
type context = { session_id : string }
|
type context =
|
||||||
|
{ session_id : string
|
||||||
|
; debug : bool
|
||||||
|
}
|
||||||
|
|
||||||
let load_dotenv =
|
let load_dotenv =
|
||||||
(* Load variables *)
|
(* Load variables *)
|
||||||
|
|
Loading…
Reference in a new issue