better usage of the pattern-matching

This commit is contained in:
Mylloon 2024-03-09 15:23:28 +01:00
parent 8201e2cbe4
commit a8248827e0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -12,10 +12,8 @@ let inject_username session_id creds =
let input_username = let input_username =
match find session_id strat with match find session_id strat with
| [] -> raise (Any (fmt "Username input not found")) | [] -> raise (Any (fmt "Username input not found"))
| _ as l -> | it :: [] -> it
if List.length l > 1 | _ -> raise (Any "Too many elements found as the username input")
then raise (Any "Too many elements found as the username input")
else List.nth l 0
in in
(* Insert the username *) (* Insert the username *)
send_keys session_id input_username creds.username; send_keys session_id input_username creds.username;
@ -38,10 +36,8 @@ let rec _inject_password session_id creds try_count =
inject_username session_id creds; inject_username session_id creds;
_inject_password session_id creds (try_count - 1); _inject_password session_id creds (try_count - 1);
None None
| _ as l -> | it :: [] -> Some it
if List.length l > 1 | _ -> raise (Any "Too many elements found as the password input")
then raise (Any "Too many elements found as the password input")
else Some (List.nth l 0)
in in
match input_password with match input_password with
| Some input -> | Some input ->
@ -83,12 +79,10 @@ let login_twitter ctx username password secret =
(* 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
| [] -> print_endline "Doesn't use 2FA as no input found" | [] -> print_endline "Doesn't use 2FA as no input found"
| _ as l -> | it :: [] ->
if List.length l > 1
then raise (Any "Too many elements found as 2FA input")
else (
if ctx.debug then print_endline "Type 2FA code..."; if ctx.debug then print_endline "Type 2FA code...";
inject_2fa ctx.session_id secret (List.nth l 0)) inject_2fa ctx.session_id secret it
| _ -> raise (Any "Too many elements found as 2FA input")
;; ;;
let go_to_profile ctx = let go_to_profile ctx =
@ -96,10 +90,8 @@ let go_to_profile ctx =
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"))
| _ as l -> | it :: [] -> it
if List.length l > 1 | _ -> raise (Any "Too many profile button found")
then raise (Any "Too many profile button found")
else List.nth l 0
in in
if ctx.debug then print_endline "Navigate to user replies..."; if ctx.debug then print_endline "Navigate to user replies...";
ignore ignore
@ -148,18 +140,16 @@ let tweet ctx msg =
let tweet_area = let tweet_area =
match find ctx.session_id (CSS "div[data-testid='tweetTextarea_0']") with match find ctx.session_id (CSS "div[data-testid='tweetTextarea_0']") with
| [] -> raise (Any (fmt "Tweet area not found")) | [] -> raise (Any (fmt "Tweet area not found"))
| _ as l -> | it :: [] -> it
if List.length l > 1 then raise (Any "Too many tweet areas found") else List.nth l 0 | _ -> raise (Any "Too many tweet areas found")
in in
send_keys ctx.session_id tweet_area msg; send_keys ctx.session_id tweet_area msg;
Unix.sleep 2; Unix.sleep 2;
let send_tweet_button = let send_tweet_button =
match find ctx.session_id (XPath "//div[@data-testid='tweetButtonInline']") with match find ctx.session_id (XPath "//div[@data-testid='tweetButtonInline']") with
| [] -> raise (Any (fmt "Tweet button not found")) | [] -> raise (Any (fmt "Tweet button not found"))
| _ as l -> | it :: [] -> it
if List.length l > 1 | _ -> raise (Any "Too many tweet button found")
then raise (Any "Too many tweet button found")
else List.nth l 0
in in
click ctx.session_id send_tweet_button; click ctx.session_id send_tweet_button;
Unix.sleep 8 Unix.sleep 8