pusk/bin/main.ml

101 lines
2.8 KiB
OCaml
Raw Normal View History

open Pusk.Net
open Pusk.Drivers
open Pusk.Utils
2023-05-13 19:58:48 +02:00
open Twitter
2023-05-13 13:06:15 +02:00
let start driver =
let name_driver = prepare driver in
let data_driver = run_process name_driver [] in
2023-05-16 02:18:35 +02:00
let session_id = get_session () in
2023-05-13 13:06:15 +02:00
data_driver, session_id
;;
2023-05-12 20:30:02 +02:00
2023-05-13 13:06:15 +02:00
let stop (driver_process, session_id) =
2023-05-13 13:33:46 +02:00
if not (close_session session_id) then print_endline "Can't close the session";
2023-05-13 11:59:09 +02:00
stop_process driver_process
2023-05-12 19:36:27 +02:00
;;
2023-05-13 13:06:15 +02:00
let rec check ctx =
(* Loop *)
2023-05-15 14:56:56 +02:00
let max_time = 2505600 (* 29 days *) in
let recheck timeout =
2023-05-15 16:28:24 +02:00
if ctx.debug
then print_endline (fmt "Waiting for the next check in %d seconds" timeout);
Unix.sleep timeout;
2023-05-15 16:28:24 +02:00
if ctx.debug then print_endline "Refresh profile page...";
refresh_page ctx.session_id;
2023-05-16 02:48:48 +02:00
Unix.sleep 8;
check ctx
in
(* Time to wait until next check *)
let timeout =
2023-05-15 16:28:24 +02:00
if ctx.debug then print_endline "Check latest tweets...";
match find_latest_tweet ctx with
| Some tweet_date ->
2023-05-15 14:56:56 +02:00
(* Get date of tweet and return time to wait before tweeting *)
let now = Float.to_int (Unix.time ()) in
let diff = now - tweet_date in
if diff > max_time
then 0 (* Timeout expired *)
else max_time - diff (* Timeout for when it will expire *)
2023-05-15 16:28:24 +02:00
| None ->
if ctx.debug then print_endline "No tweets found...";
0
in
if 0 = timeout
then (
2023-05-15 16:28:24 +02:00
if ctx.debug then print_endline "Tweeting...";
(* Tweet *)
2023-05-15 15:05:34 +02:00
tweet
ctx
2023-05-15 15:58:28 +02:00
"This tweet is for the Twitter's CTO: don't suspend my account for inactivity.";
(* Returns to profile page *)
go_to_profile ctx;
(* Wait the maximum time since we just tweeted *)
2023-05-15 14:56:56 +02:00
recheck max_time)
else (* Wait the amount of time calculated from the post *)
recheck timeout
;;
2023-05-13 19:58:48 +02:00
let main ctx =
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
2023-05-15 11:44:21 +02:00
login_twitter ctx username password (Sys.getenv_opt "TWITTER_TOTP");
go_to_profile ctx;
(* Start check routine *)
2023-05-15 16:28:24 +02:00
if ctx.debug then print_endline "Start routine...";
check ctx
2023-05-13 16:17:18 +02:00
;;
2023-05-13 13:06:15 +02:00
2023-05-15 11:02:56 +02:00
let handler data (signal : int) =
stop_process data;
exit
(match signal with
2024-01-26 15:31:24 +01:00
| v when v = Sys.sigint -> 130
| _ -> 1)
2023-05-15 11:02:56 +02:00
;;
2023-05-13 13:06:15 +02:00
let () =
let data = start (Gecko "0.33.0") in
2023-05-15 11:02:56 +02:00
Sys.set_signal Sys.sigint (Sys.Signal_handle (handler (fst data)));
2023-05-15 16:28:24 +02:00
(* Load env variables *)
load_dotenv;
let ctx =
{ session_id = snd data
; debug =
(match Sys.getenv_opt "PUSK_DEBUG" with
2024-01-26 15:31:24 +01:00
| Some boolean -> if String.lowercase_ascii boolean = "true" then true else false
| None -> false)
2023-05-15 16:28:24 +02:00
}
in
if ctx.debug then print_endline "Logging is enabled";
2023-05-13 19:58:48 +02:00
(try main ctx with
2024-01-26 15:31:24 +01:00
| Any why -> print_endline why);
2023-05-13 13:06:15 +02:00
stop data
;;