浏览代码

Updated to “lib” version

pezcurrel 2 周之前
父节点
当前提交
a7d418192c
共有 1 个文件被更改,包括 25 次插入4 次删除
  1. 25 4
      lib/ckratelimit.php

+ 25 - 4
lib/ckratelimit.php

@@ -1,4 +1,5 @@
 <?php
+
 function ckratelimit($headers) {
 	$aaheaders=[];
 	array_shift($headers);
@@ -18,9 +19,29 @@ function ckratelimit($headers) {
 	if (!is_int($reset)) return ['ok'=>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);*/
+
 ?>