From ed2d7182d9fe1d13e4d20ad189b04fa6c3b0cec1 Mon Sep 17 00:00:00 2001 From: pezcurrel Date: Tue, 27 Aug 2024 19:37:22 +0200 Subject: [PATCH] =?UTF-8?q?Now=20uses=20=E2=80=9C../lib/ckratelimit.php?= =?UTF-8?q?=E2=80=9D;=20minor=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/clitools/getinstinfo.php | 79 +++++++++++++++--------------------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/web/clitools/getinstinfo.php b/web/clitools/getinstinfo.php index 77eaee5..88181a2 100755 --- a/web/clitools/getinstinfo.php +++ b/web/clitools/getinstinfo.php @@ -19,21 +19,22 @@ const N="\n"; const RN="\r\n"; define('SNAME',basename(__FILE__)); -const LIBDP='/../lib'; -require __DIR__.LIBDP.'/parsetime.php'; -require __DIR__.LIBDP.'/gurl.php'; -require __DIR__.LIBDP.'/gethttpcode.php'; -require __DIR__.LIBDP.'/tables.php'; -require __DIR__.LIBDP.'/mb_ucfirst.php'; -require __DIR__.LIBDP.'/mb_lcfirst.php'; -require __DIR__.LIBDP.'/ghs.php'; -require __DIR__.LIBDP.'/ght.php'; -require __DIR__.LIBDP.'/fnum.php'; -require __DIR__.LIBDP.'/supplangs.php'; -require __DIR__.LIBDP.'/gettlds.php'; -require __DIR__.LIBDP.'/mastodon_postlen.php'; -require __DIR__.LIBDP.'/vendor/autoload.php'; +chdir(__DIR__); +require '../lib/parsetime.php'; +require '../lib/gurl.php'; +require '../lib/gethttpcode.php'; +require '../lib/tables.php'; +require '../lib/mb_ucfirst.php'; +require '../lib/mb_lcfirst.php'; +require '../lib/ghs.php'; +require '../lib/ght.php'; +require '../lib/fnum.php'; +require '../lib/supplangs.php'; +require '../lib/gettlds.php'; +require '../lib/mastodon_postlen.php'; +require '../lib/ckratelimit.php'; +require '../lib/vendor/autoload.php'; use LanguageDetection\Language; use function mysqli_real_escape_string as myesc; @@ -362,7 +363,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4 eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance info from API v2...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v2/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { if (make(['domain', 'title', 'version', 'source_url', 'description', 'usage', 'thumbnail', 'languages', 'configuration', 'registrations', 'contact', 'rules'],$buf)) { @@ -423,7 +424,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4 eecho(0,'«'.$opts['hostname'].'»: trying to fetch domain count from API v1...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { eecho(1,'«'.$opts['hostname'].'»: got instance info from API v1 :-)'.N); @@ -438,7 +439,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4 eecho(0,'«'.$opts['hostname'].'»: trying to fetch extended description from API v1...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/extended_description',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { eecho(1,'«'.$opts['hostname'].'»: got extended description from API v1 :-)'.N); @@ -453,7 +454,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4 eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance domain blocks from API v1...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/domain_blocks',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { eecho(1,'«'.$opts['hostname'].'»: got instance domain blocks from API v1 :-)'.N); @@ -495,7 +496,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4 eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance info from API v1...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { if (make(['uri', 'title', 'short_description', 'description', 'email', 'version', 'urls', 'stats', 'thumbnail', 'languages', 'registrations', 'approval_required', 'contact_account'],$buf)) { @@ -574,7 +575,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='2 eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance activity info from API v1...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/activity',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { eecho(1,'«'.$opts['hostname'].'»: got instance activity info from API v1 :-)'.N); @@ -593,7 +594,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='3 if ($idata['Version']>='3.5.0') $url.='/tags'; $buf=@gurl($url,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$opts['hostname']}»"); + ckrl($buf['headers'],"«{$opts['hostname']}»"); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { eecho(1,'«'.$opts['hostname'].'»: got instance tags trends info from API v1 :-)'.N); @@ -904,8 +905,8 @@ if ($instanswered && $opts['fetchusers'] && $idata['IsMastodon'] && !is_null($id eecho(0,'«'.$opts['hostname'].'»: trying to fetch chunk '.($chunk+1).' of users info from directory API (attempt '.($att+1).'/'.$opts['udiratts'].')...'.N); $buf=@gurl('https://'.$opts['hostname'].'/api/v1/directory?local=1&order=new&limit='.$limit.'&offset='.$offset,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - $xrlr=ckratelimit($buf['headers'],"«{$opts['hostname']}»"); - eecho(1,'«'.$opts['hostname'].'»: got chunk '.($chunk+1).' of users info from directory API on attempt '.($att+1).'/'.$opts['udiratts'].' (xrlr: '.$xrlr.') :-)'.N); + ckrl($buf['headers'],"«{$opts['hostname']}»"); + eecho(1,'«'.$opts['hostname'].'»: got chunk '.($chunk+1).' of users info from directory API on attempt '.($att+1).'/'.$opts['udiratts'].' :-)'.N); $buf=@json_decode($buf['cont'],true); if (is_array($buf)) { //print_r($buf); @@ -947,7 +948,7 @@ if ($instanswered && $opts['fetchusers'] && $idata['IsMastodon'] && !is_null($id eecho(0,'«'.$opts['hostname'].'»: trying to fetch tags for user «'.$user['username'].'»...'.N); $tags=@gurl('https://'.$opts['hostname'].'/api/v1/accounts/'.$user['id'].'/featured_tags',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($tags['cont']!==false) { - ckratelimit($tags['headers'],"«{$opts['hostname']}»"); + ckrl($tags['headers'],"«{$opts['hostname']}»"); $tags=@json_decode($tags['cont'],true); if (is_array($tags) && count($tags)>0) { eecho(1,'«'.$opts['hostname'].'»: got '.count($tags).' tag(s) for user «'.$user['username'].'» :-)'.N); @@ -1200,28 +1201,14 @@ function datetots($date) { return(mktime(0,0,0,$date[1],$date[2],$date[0])); } -function ckratelimit($headers,$ctx) { +function ckrl($headers,$ctx) { $headers=explode("\r\n",$headers); - $buff=[]; - array_shift($headers); - foreach ($headers as $header) - if (preg_match('/^([^:]+):(.*)$/Uu',$header,$matches)===1) - $buff[strtolower($matches[1])]=trim($matches[2]); - $headers=$buff; - if (isset($headers['date']) && isset($headers['x-ratelimit-reset']) && isset($headers['x-ratelimit-remaining'])) { - if ($headers['x-ratelimit-remaining']==0) { - $stosl=strtotime($headers['x-ratelimit-reset'])-strtotime($headers['date'])+1; - eecho(2,$ctx.': reached rate limit, sleeping for '.ght($stosl,null,0).' ...'.N); - sleep($stosl); - } - return($headers['x-ratelimit-remaining']); - } else { - $missing=[]; - if (!isset($headers['date'])) $missing[]='date'; - if (!isset($headers['x-ratelimit-reset'])) $missing[]='x-ratelimit-reset'; - if (!isset($headers['x-ratelimit-remaining'])) $missing[]='x-ratelimit-remaining'; - eecho(2,$ctx.': ckratelimit: headers did not contain «'.implode('», «',$missing).'» item(s)!'.N); - return(false); + $res=ckratelimit($headers); + if (!$res['ok']) { + eecho(2,"{$ctx}: ckratelimit: {$res['error']}.\n"); + } elseif ($res['sleep']>0) { + eecho(2,"{$ctx}: ckratelimit: x-ratelimit-remaining is 0, sleeping for ".ght($res['sleep']).' (until '.date('c',time()+$res['sleep']).") ...\n"); + sleep($res['sleep']); } } @@ -1237,7 +1224,7 @@ function get_api($host, $path) { global $opts; $buf = @gurl('https://'.$host.$path,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json'],$opts['proxy']); if ($buf['cont']!==false) { - ckratelimit($buf['headers'],"«{$host}»"); + ckrl($buf['headers'],"«{$host}»"); $data = json_decode($buf['cont'], true); return $data; } else {