From 64368b1651bc6ced980e2c64e67a095f3d4fab06 Mon Sep 17 00:00:00 2001 From: silva guimaraes Date: Sun, 13 Jul 2025 13:45:10 -0300 Subject: [PATCH] captcha --- lib/lib.ml | 25 ++++++++++++++++--------- lib/rentry.ml | 19 +++++++++++++------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/lib.ml b/lib/lib.ml index 240f1e7..7b4b2e5 100644 --- a/lib/lib.ml +++ b/lib/lib.ml @@ -24,10 +24,7 @@ type api_team = { } [@@yojson.allow_extra_fields] [@@deriving yojson, show] -let team_name x = - match x.short_name with - | Some x -> x - | None -> x.name +let team_name x = match x.short_name with Some x -> x | None -> x.name type api_tournament_info = { name : string; slug : string } [@@yojson.allow_extra_fields] [@@deriving yojson, show] @@ -79,6 +76,7 @@ type status = | Suspended of { start_timestamp : int } | Postponed | Canceled + | Delayed | InProgress of time | Completed of time [@@deriving show] @@ -123,6 +121,7 @@ module Tournament = struct let timestamp = match x.status with | Postponed | Completed _ | Canceled -> None + | Delayed -> Some "delayed... " | Suspended t -> if is_today t.start_timestamp then Some "SUS! " else None | InProgress t -> @@ -146,8 +145,8 @@ module Tournament = struct | None -> None | Some timestamp -> Some - (sprintf "%s | %s x %s" timestamp (team_name x.home_team) (team_name x.away_team) - ) + (sprintf "%s | %s x %s" timestamp (team_name x.home_team) + (team_name x.away_team)) in let m = tournament.matches @@ -165,17 +164,24 @@ let ( let* ) = Lwt.bind exception Status_Not_found -(* TODO: handle "resolution fai led: name resolution failed" *) +(* TODO: handle "resolution failed: name resolution failed" *) let get url : string = let http_get url = Stdlib.flush Stdlib.stdout; - let* resp, body = Cohttp_lwt_unix.Client.get (Uri.of_string url) in + let req_headers = + Cohttp.Header.( + add (init ()) "X-Captcha" + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTI0OTgxMTR9.sZk2yhTk5TeWu_kXE7RO__LDYKlPgZuaA7KKFsB_0GQ") + in + let* resp, body = + Cohttp_lwt_unix.Client.get ~headers:req_headers @@ Uri.of_string url + in let code = resp |> Cohttp.Response.status |> Cohttp.Code.code_of_status in if code = 404 then raise Status_Not_found else if Cohttp.Code.is_success code then let* b = Cohttp_lwt.Body.to_string body in Lwt.return (Ok b) - else Lwt.return (Error (Cohttp.Code.reason_phrase_of_code code)) + else Lwt.return @@ Error (Cohttp.Code.reason_phrase_of_code code) in Lwt_main.run (let* result = http_get url in @@ -221,6 +227,7 @@ let matches_of_api_events (e : api_events) = | "suspended" -> Suspended { start_timestamp = m.startTimestamp } | "inprogress" -> InProgress t | "finished" -> Completed t + | "delayed" -> Delayed | _ -> failwith (sprintf "unrecognized match type: %s" typ)); }) diff --git a/lib/rentry.ml b/lib/rentry.ml index 39b459a..1393311 100644 --- a/lib/rentry.ml +++ b/lib/rentry.ml @@ -20,7 +20,8 @@ let update_page content = Cohttp_lwt.Body.of_form [ ("csrfmiddlewaretoken", [ value ]); - ("edit_code", [ "foo perro bar caca" ]); (* TODO: use environmental variables *) + (* TODO: use environmental variables *) + ("edit_code", [ "foo perro bar caca" ]); ("metadata", [ "ACCESS_RECOMMENDED_THEME=dark" ]); ("text", [ content ]); ] @@ -28,10 +29,6 @@ let update_page content = let uri = Uri.of_string "https://rentry.org/copalib-schedule/edit" in let req_headers = Cohttp.Header.( - let h0 = init () in - let h1 = add h0 "Referer" "https://rentry.org" in - let h2 = add h1 "Content-Type" "application/x-www-form-urlencoded" in - let h3 = add h2 "User-Agent" "copabot/0.1.0" in let hname, hvalue = cookies |> List.map (fun (x : string * Cookie.Set_cookie_hdr.t) -> @@ -39,7 +36,17 @@ let update_page content = v.cookie) |> Cookie.Cookie_hdr.serialize in - add h3 hname hvalue) + let rec aux h = function + | [] -> h + | (k, v) :: tail -> aux (add h k v) tail + in + aux (init ()) + [ + ("Referer", "https://rentry.org"); + ("Content-Type", "application/x-www-form-urlencoded"); + ( "User-Agent", "copabot/0.1.0" ); + (hname, hvalue); + ]) in let* resp, _ = Cohttp_lwt_unix.Client.post uri ?body:(Some form)