diff --git a/pfaltgall b/pfaltgall index 67f8cd6..5547bea 100755 --- a/pfaltgall +++ b/pfaltgall @@ -23,22 +23,23 @@ require 'lib/ckratelimit.php'; require 'lib/httpjson.php'; $configfp=null; +$outfp=null; $conf=[ 'host'=>null, 'token'=>null, - 'accid'=>null ]; $help= "[[[ SYNOPSIS ]]] - {$SCRIPTNAME} [options] + {$SCRIPTNAME} [options] [[[ DESCRIPTION ]]] This is {$SCRIPTNAME} v{$SCRIPTVERSION}, a CLI PHP script that can generate an html file with -a gallery from your Pixelfed profile. +a gallery from your Pixelfed profile, preserving each post’s content and +possible image descriptions (alt-texts). In order to create it, you just need to login to your Pixelfed account and get an app token (Settings -> Applications -> Create new token), then create a configuration file for {$SCRIPTNAME} like this (don’t write the «---» lines): @@ -55,10 +56,11 @@ host=pixelfed.social token=as7f8a7s0d89f7as97df09a8s7d90f81jkl2h34lkj12h3jkl4 --- - Then run {$SCRIPTNAME} with the path of the configuration file you have -created as an argument. This will create an «index.html» file in the current -directory, that will be ready to be put where you want (you can also see it -locally, obviously). + Then run {$SCRIPTNAME} with the path of the configuration file you have created +and the path of an output file as arguments (if the output file exists, it +will be overwritten) - e.g.: «{$SCRIPTNAME} goofy@pixelfed.social.conf index.html». + This will create an html file that will be ready to be put where you want +(you can also see it locally, obviously). [[[ OPTIONS ]]] @@ -80,14 +82,24 @@ for ($i=1; $i<$argc; $i++) { exit(0); } elseif (is_null($configfp)) { $configfp=$argv[$i]; + } elseif (is_null($outfp)) { + $outfp=$argv[$i]; } else { - eecho("Error: «{$argv[$i]}» is not a valid option and the configuration file has already been set to «{$configfp}» (use «-h» or «--help» to read help).\n"); + eecho("Error: «{$argv[$i]}» is not a valid option and configuration file and output file have already been set to «{$configfp}» and «{$outfp}» (use «-h» or «--help» to read help).\n"); exit(1); } } -if (is_null($configfp)) { - eecho("Error: you have not specified a config file (use «-h» or «--help» to read help).\n"); +$errors=[]; +if (is_null($configfp)) + $errors[]="you have not specified a config file"; +if (is_null($outfp)) + $errors[]="you have not specified an output file"; +if (count($errors)>0) { + eecho("Errors:\n"); + foreach ($errors as $val) + eecho(" - {$val}\n"); + eecho("Use «-h» or «--help» to read help.\n"); exit(1); } @@ -117,13 +129,13 @@ foreach ($conf as $key=>$val) $acc=httpjson("https://{$conf['host']}/api/v1/accounts/verify_credentials",null,null,null,null,$conf['token']); //print_r($res); if (!$acc['ok']) { - eecho("Error: {$SCRIPTNAME} could not retrieve the account id associated with the given token ({$acc['errors']}).\n"); + eecho("Error: {$SCRIPTNAME} could not retrieve the account associated with the given token ({$acc['errors']}).\n"); exit(2); } ckrl($acc['headers']); $acc=$acc['content']; -$urls=[]; +$imgurls=[]; $imgs=''; $i=0; $ic=0; @@ -159,7 +171,8 @@ do { $ia=0; foreach ($status['media_attachments'] as $attachment) { if (isset($attachment['url'])) { - $url=$attachment['url']; + $imgurl=$attachment['url']; + $imgurls[]=$imgurl; $ia++; if (isset($attachment['description']) && preg_match('#^\s+$#',$attachment['description'])!==1) $altdesc=' alt="'.htmlspecialchars(trim($attachment['description']),ENT_QUOTES|ENT_HTML5).'"'; @@ -169,7 +182,7 @@ do { $icnt=" ({$ia}/{$ca})"; else $icnt=''; - $imgs.="
{$desc}{$icnt}{$date}
\n"; + $imgs.="
{$desc}{$icnt}{$date}
\n"; $ic++; } } @@ -381,9 +394,48 @@ hr { } } + - -
+ +

@@ -397,7 +449,10 @@ hr { '; -file_put_contents('index.html',$html); +if (@file_put_contents($outfp,$html)===false) { + eecho("Error: {$SCRIPTNAME} could not save html into «{$outfp}».\n"); + exit(2); +} exit(0);