Browse Source

Added dynamic loading of previous, current and next images; added output file path as second mandatory argument; edited a bit the help text to make it clearer

pezcurrel 2 weeks ago
parent
commit
eb9fd01489
1 changed files with 72 additions and 17 deletions
  1. 72 17
      pfaltgall

+ 72 - 17
pfaltgall

@@ -23,22 +23,23 @@ require 'lib/ckratelimit.php';
 require 'lib/httpjson.php';
 require 'lib/httpjson.php';
 
 
 $configfp=null;
 $configfp=null;
+$outfp=null;
 
 
 $conf=[
 $conf=[
 	'host'=>null,
 	'host'=>null,
 	'token'=>null,
 	'token'=>null,
-	'accid'=>null
 ];
 ];
 
 
 $help=
 $help=
 "[[[ SYNOPSIS ]]]
 "[[[ SYNOPSIS ]]]
 
 
- {$SCRIPTNAME} [options] <configuration file path>
+ {$SCRIPTNAME} [options] <configuration file path> <output file path>
 
 
 [[[ DESCRIPTION ]]]
 [[[ DESCRIPTION ]]]
 
 
  This is {$SCRIPTNAME} v{$SCRIPTVERSION}, a CLI PHP script that can generate an html file with
  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
  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
 get an app token (Settings -> Applications -> Create new token), then create
 a configuration file for {$SCRIPTNAME} like this (don’t write the «---» lines):
 a configuration file for {$SCRIPTNAME} like this (don’t write the «---» lines):
@@ -55,10 +56,11 @@ host=pixelfed.social
 token=as7f8a7s0d89f7as97df09a8s7d90f81jkl2h34lkj12h3jkl4
 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 ]]]
 [[[ OPTIONS ]]]
 
 
@@ -80,14 +82,24 @@ for ($i=1; $i<$argc; $i++) {
 		exit(0);
 		exit(0);
 	} elseif (is_null($configfp)) {
 	} elseif (is_null($configfp)) {
 		$configfp=$argv[$i];
 		$configfp=$argv[$i];
+	} elseif (is_null($outfp)) {
+		$outfp=$argv[$i];
 	} else {
 	} 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);
 		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);
 	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']);
 $acc=httpjson("https://{$conf['host']}/api/v1/accounts/verify_credentials",null,null,null,null,$conf['token']);
 //print_r($res);
 //print_r($res);
 if (!$acc['ok']) {
 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);
 	exit(2);
 }
 }
 ckrl($acc['headers']);
 ckrl($acc['headers']);
 $acc=$acc['content'];
 $acc=$acc['content'];
 
 
-$urls=[];
+$imgurls=[];
 $imgs='';
 $imgs='';
 $i=0;
 $i=0;
 $ic=0;
 $ic=0;
@@ -159,7 +171,8 @@ do {
 				$ia=0;
 				$ia=0;
 				foreach ($status['media_attachments'] as $attachment) {
 				foreach ($status['media_attachments'] as $attachment) {
 					if (isset($attachment['url'])) {
 					if (isset($attachment['url'])) {
-						$url=$attachment['url'];
+						$imgurl=$attachment['url'];
+						$imgurls[]=$imgurl;
 						$ia++;
 						$ia++;
 						if (isset($attachment['description']) && preg_match('#^\s+$#',$attachment['description'])!==1)
 						if (isset($attachment['description']) && preg_match('#^\s+$#',$attachment['description'])!==1)
 							$altdesc=' alt="'.htmlspecialchars(trim($attachment['description']),ENT_QUOTES|ENT_HTML5).'"';
 							$altdesc=' alt="'.htmlspecialchars(trim($attachment['description']),ENT_QUOTES|ENT_HTML5).'"';
@@ -169,7 +182,7 @@ do {
 							$icnt=" ({$ia}/{$ca})";
 							$icnt=" ({$ia}/{$ca})";
 						else
 						else
 							$icnt='';
 							$icnt='';
-						$imgs.="<div class=\"page\"><table class=\"imgtab\"><tr><td class=\"imgcel\"><a href=\"{$url}\" name=\"img{$ic}\"><img class=\"img\" decoding=\"async\" loading=\"lazy\" src=\"{$url}\"{$altdesc}></a></td></tr><caption class=\"imgcaptcel\">{$desc}{$icnt}{$date}</caption></table></div>\n";
+						$imgs.="<div class=\"page\"><table class=\"imgtab\"><tr><td class=\"imgcel\"><a href=\"{$imgurl}\" name=\"img{$ic}\"><img class=\"img\" decoding=\"async\" loading=\"lazy\" id=\"img{$ic}\" src=\"{$acc['avatar_static']}\"{$altdesc}></a></td></tr><caption class=\"imgcaptcel\">{$desc}{$icnt}{$date}</caption></table></div>\n";
 						$ic++;
 						$ic++;
 					}
 					}
 				}
 				}
@@ -381,9 +394,48 @@ hr {
 	}
 	}
 }
 }
 </style>
 </style>
+<script type="text/javascript">
+let imgurls=["'.implode('", "',$imgurls).'"];
+let phimgurl="'.$acc['avatar_static'].'";//placeholder image url
+function prel() {
+	var md=document.getElementById("maindiv"),
+	ph=window.innerHeight,
+	th=md.scrollHeight,
+	pages=Math.round(th/ph),
+	sy=Math.round(md.scrollTop),
+	page=Math.round(pages-(th-sy)/ph)+1,
+	img,
+	u;
+	//console.log(ph+" "+th+" "+pages+" "+sy+" "+page);
+	if (page>1) {//current
+		img=document.getElementById("img"+(page-2));
+		if (img.src==phimgurl) {
+			u=imgurls[page-2];
+			img.src=u;
+			img.loading="eager";
+		}
+	}
+	if (page<pages) {//next
+		img=document.getElementById("img"+(page-1));
+		if (img.src==phimgurl) {
+			u=imgurls[page-1];
+			img.src=u;
+			img.loading="eager";
+		}
+	}
+	if (page>2) {//previous
+		img=document.getElementById("img"+(page-3));
+		if (img.src==phimgurl) {
+			u=imgurls[page-3];
+			img.src=u;
+			img.loading="eager";
+		}
+	}
+}
+</script>
 </head>
 </head>
-<body>
-<div id="maindiv">
+<body onload="prel();">
+<div id="maindiv" onscrollend="prel();">
 <div class="page">
 <div class="page">
 <div class="profile">
 <div class="profile">
 <p class="center"><img src="'.$acc['avatar_static'].'" class="avatar"></p>
 <p class="center"><img src="'.$acc['avatar_static'].'" class="avatar"></p>
@@ -397,7 +449,10 @@ hr {
 </html>
 </html>
 ';
 ';
 
 
-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);
 exit(0);