From a7d418192cbdd337dbc06cb0a453b4468991b9d3 Mon Sep 17 00:00:00 2001 From: pezcurrel Date: Sat, 28 Sep 2024 06:01:07 +0200 Subject: [PATCH] =?UTF-8?q?Updated=20to=20=E2=80=9Clib=E2=80=9D=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ckratelimit.php | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/ckratelimit.php b/lib/ckratelimit.php index b8ae8b1..ad9b21d 100644 --- a/lib/ckratelimit.php +++ b/lib/ckratelimit.php @@ -1,4 +1,5 @@ false,'error'=>'«x-ratelimit-reset» header could not be converted to a unix timestamp']; // don't do the one on the line below, since it happens lots of times //if ($reset<$date) return ['ok'=>false,'error'=>'the unix timestamp parsed from «x-ratelimit-reset» header is less than the unix timestamp parsed from «date» header']; - if ($remaining==0) - return ['ok'=>true,'sleep'=>$reset-$date+1,'remaining'=>$remaining]; - else - return ['ok'=>true,'sleep'=>0,'remaining'=>$remaining]; + return ['ok'=>true,'sleep'=>$reset-$date+1,'remaining'=>$remaining]; } + +/* +// test +$context=[ + 'http'=>[ + 'header'=>"Accept: application/json\r\n"; + ] +]; +$context=stream_context_create($context); +while (true) { + $res=@file_get_contents('https://livellosegreto.it/api/v2/instance',false,$context); + echo "{$res}\n"; + print_r($http_response_header); + $rl=ckratelimit($http_response_header); + print_r($rl); + if ($rl['sleep']>0) { + echo 'Reached rate limit, sleeping for '.ght($rl['sleep']).' (until '.date('c',time()+$rl['sleep']).') ...'; + sleep($rl['sleep']); + echo "\n"; + } +} +exit(0);*/ + ?>