Jelajahi Sumber

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 minggu lalu
induk
melakukan
eb9fd01489
1 mengubah file dengan 72 tambahan dan 17 penghapusan
  1. 72 17
      pfaltgall

+ 72 - 17
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] <configuration file path>
+ {$SCRIPTNAME} [options] <configuration file path> <output file path>
 
 [[[ 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.="<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++;
 					}
 				}
@@ -381,9 +394,48 @@ hr {
 	}
 }
 </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>
-<body>
-<div id="maindiv">
+<body onload="prel();">
+<div id="maindiv" onscrollend="prel();">
 <div class="page">
 <div class="profile">
 <p class="center"><img src="'.$acc['avatar_static'].'" class="avatar"></p>
@@ -397,7 +449,10 @@ hr {
 </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);