Browse Source

Updated to “lib” versions

pezcurrel 2 weeks ago
parent
commit
483638291d
4 changed files with 31 additions and 10 deletions
  1. 3 3
      index.php
  2. 2 2
      lib/booltostr.php
  3. 25 4
      lib/ckratelimit.php
  4. 1 1
      post.php

+ 3 - 3
index.php

@@ -378,11 +378,11 @@ if ($loggedin && isset($_POST['postwaituntil']) && preg_match('#^\d+$#',$_POST['
 					$lspostid=$res['data']['id'];
 					$lspostid=$res['data']['id'];
 					$debug.="lspostid: {$lspostid}<br>\n";
 					$debug.="lspostid: {$lspostid}<br>\n";
 					$rls=ckratelimit($res['headers']);
 					$rls=ckratelimit($res['headers']);
-					//if ($i==2) $rls=['remaining'=>0,'sleep'=>3895];// test err
-					//$rls=['remaining'=>0,'sleep'=>5];// test err
+					//if ($i==2) $rls=['ok'=>true,'remaining'=>0,'sleep'=>3895];// test err
+					//$rls=['ok'=>true,'remaining'=>0,'sleep'=>5];// test err
 					if ($rls['ok'] && $rls['remaining']<=REQSTOPRESV && $i<$postscount-1) {
 					if ($rls['ok'] && $rls['remaining']<=REQSTOPRESV && $i<$postscount-1) {
 						$postingok=false;
 						$postingok=false;
-						$postwait=$rls['secstoreset'];
+						$postwait=$rls['sleep'];
 						$postwaituntil=time()+$postwait;
 						$postwaituntil=time()+$postwait;
 						$postsoffset=$i+1;
 						$postsoffset=$i+1;
 						$postmsgs.='<div class="warning">Sending split post '.($i+1)."/{$postscount}, {$sname} reached your {$host}’s account posting rate limit, so it stopped sending; you’ll find the rest of your split posts (those which have not been sent, ".($i+2)."-{$postscount}) below; before posting them, you’ll have to wait ".ght($postwait)." for rate limit reset.</div>\n";
 						$postmsgs.='<div class="warning">Sending split post '.($i+1)."/{$postscount}, {$sname} reached your {$host}’s account posting rate limit, so it stopped sending; you’ll find the rest of your split posts (those which have not been sent, ".($i+2)."-{$postscount}) below; before posting them, you’ll have to wait ".ght($postwait)." for rate limit reset.</div>\n";

+ 2 - 2
lib/booltostr.php

@@ -1,8 +1,8 @@
 <?php
 <?php
 function booltostr($bool,$true='true',$false='false') {
 function booltostr($bool,$true='true',$false='false') {
 	if ($bool)
 	if ($bool)
-		return($true);
+		return $true;
 	else
 	else
-		return($false);
+		return $false;
 }
 }
 ?>
 ?>

+ 25 - 4
lib/ckratelimit.php

@@ -1,4 +1,5 @@
 <?php
 <?php
+
 function ckratelimit($headers) {
 function ckratelimit($headers) {
 	$aaheaders=[];
 	$aaheaders=[];
 	array_shift($headers);
 	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'];
 	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
 	// 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 ($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);*/
+
 ?>
 ?>

+ 1 - 1
post.php

@@ -34,7 +34,7 @@ if (isset($_COOKIE['verbose_host']) && isset($_COOKIE['verbose_token']) && isset
 		$resp['ok']=true;
 		$resp['ok']=true;
 		$resp['id']=$res['data']['id'];
 		$resp['id']=$res['data']['id'];
 		$rls=ckratelimit($res['headers']);
 		$rls=ckratelimit($res['headers']);
-		//$rls=['remaining'=>20,'sleep'=>5];// test
+		//$rls=['ok'=>true,'remaining'=>20,'sleep'=>5];// test
 		if ($rls['ok']) {
 		if ($rls['ok']) {
 			$resp['remaining']=$rls['remaining'];
 			$resp['remaining']=$rls['remaining'];
 			$resp['secstoreset']=$rls['sleep'];
 			$resp['secstoreset']=$rls['sleep'];