diff --git a/bin/main.ml b/bin/main.ml index db3da26..75c9fd0 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -15,6 +15,32 @@ let stop (driver_process, session_id) = stop_process driver_process ;; +let rec check ctx = + (* Loop *) + let recheck timeout = + Unix.sleep timeout; + refresh_page ctx.session_id; + Unix.sleep 4; + check ctx + in + (* Time to wait until next check *) + let timeout = + match find_latest_tweet ctx with + | Some _tweet -> + (* TODO: Get date of tweet and return time to wait before tweeting + * (0 if we need to tweet) *) + 30000000000000000 + | None -> 0 + in + if 0 = timeout + then ( + print_endline "TODO: We are tweeting here. :)"; + (* Wait the maximum time since we just tweeted *) + recheck 2505600 (* 29 days *)) + else (* Wait the amount of time calculated from the post *) + recheck timeout +;; + let main ctx = (* Load credentials *) load_dotenv; @@ -26,7 +52,9 @@ let main ctx = | Some _, None -> raise (Any "Password not set") in login_twitter ctx username password (Sys.getenv_opt "TWITTER_TOTP"); - go_to_profile ctx + go_to_profile ctx; + (* Start check routine *) + check ctx ;; let handler data (signal : int) = diff --git a/bin/twitter.ml b/bin/twitter.ml index 1189fbd..0b3ea19 100644 --- a/bin/twitter.ml +++ b/bin/twitter.ml @@ -102,4 +102,11 @@ let go_to_profile ctx = Unix.sleep 4 ;; -let find_latest_tweet = () +let find_latest_tweet ctx = + match find ctx.session_id (XPath "//article[@data-testid='tweet']") with + | [] -> None + | _ as tweets -> + (* TODO: Find latest tweet *) + List.iter print_endline tweets; + Some "" (* tmp *) +;; diff --git a/lib/net.ml b/lib/net.ml index b833168..4d381c8 100644 --- a/lib/net.ml +++ b/lib/net.ml @@ -141,3 +141,8 @@ let get_url session_id button_id = raise (Any (fmt "Unexpected response from driver: %s" (Yojson.Safe.to_string e)))) | _ -> raise (Any "get_url | Invalid JSON") ;; + +let refresh_page ?(wait = true) session_id = + ignore (execute_post_request (fmt "%s/refresh" (driver session_id)) Json.empty); + if wait then wait_for_load session_id +;;