mirror of
https://github.com/standardebooks/web.git
synced 2025-07-12 17:42:29 -04:00
Refactor generate-opds script and create a new 'newest 30' OPDS feed
This commit is contained in:
parent
7a043c3e85
commit
cae117951b
2 changed files with 57 additions and 42 deletions
|
@ -1,4 +1,8 @@
|
|||
<?
|
||||
use function Safe\krsort;
|
||||
use function Safe\getopt;
|
||||
use function Safe\preg_replace;
|
||||
|
||||
$longopts = array("webroot:", "weburl:");
|
||||
$options = getopt("", $longopts);
|
||||
$webRoot = $options["webroot"] ?? "/standardebooks.org/web";
|
||||
|
@ -6,12 +10,9 @@ $webUrl = $options["weburl"] ?? "https://standardebooks.org";
|
|||
|
||||
require_once($webRoot . '/lib/Core.php');
|
||||
|
||||
$updatedTimestamp = gmdate('Y-m-d\TH:i:s\Z');
|
||||
|
||||
$contentFiles = explode("\n", trim(shell_exec('find ' . escapeshellarg($webRoot . '/www/ebooks/') . ' -name "content.opf" | sort') ?? ''));
|
||||
$sortedContentFiles = [];
|
||||
|
||||
$allFeedEbooks = '';
|
||||
$allEbooks = [];
|
||||
$newestEbooks = [];
|
||||
|
||||
foreach($contentFiles as $path){
|
||||
if($path == '')
|
||||
|
@ -20,45 +21,17 @@ foreach($contentFiles as $path){
|
|||
$ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $path) ?? '';
|
||||
$ebook = new Ebook($ebookWwwFilesystemPath);
|
||||
|
||||
$sortedContentFiles[$ebook->ModifiedTimestamp->format('Y-m-dTH:i:sZ') . ' ' . $ebook->Identifier] = $ebook;
|
||||
$allEbooks[$ebook->ModifiedTimestamp->format('Y-m-d\TH:i:s\Z') . ' ' . $ebook->Identifier] = $ebook;
|
||||
$newestEbooks[$ebook->Timestamp->format('Y-m-d\TH:i:s\Z') . ' ' . $ebook->Identifier] = $ebook;
|
||||
}
|
||||
|
||||
krsort($sortedContentFiles);
|
||||
krsort($allEbooks);
|
||||
$allFeed = new OpdsFeed(SITE_URL . '/opds/all', 'All Standard Ebooks', $allEbooks, true);
|
||||
$allFeed->Save(WEB_ROOT . '/opds/all.xml');
|
||||
|
||||
$url = SITE_URL . '/opds/all';
|
||||
|
||||
$feed = Template::OpdsFeed(['id' => $url, 'url' => $url, 'title' => 'All Standard Ebooks', 'updatedTimestamp' => $updatedTimestamp, 'isCrawlable' => true, 'entries' => $sortedContentFiles]);
|
||||
|
||||
$tempFilename = tempnam('/tmp/', 'se-opds-');
|
||||
|
||||
file_put_contents($tempFilename, $feed);
|
||||
exec('se clean ' . escapeshellarg($tempFilename));
|
||||
|
||||
// If the feed has changed compared to the version currently on disk, copy our new version over
|
||||
// and update the updated timestamp in the master opds index.
|
||||
try{
|
||||
if(filesize($webRoot . '/www/opds/all.xml') !== filesize($tempFilename)){
|
||||
$oldFeed = file_get_contents($webRoot . '/www/opds/all.xml');
|
||||
$newFeed = file_get_contents($tempFilename);
|
||||
if($oldFeed != $newFeed){
|
||||
file_put_contents($webRoot . '/www/opds/all.xml', $newFeed);
|
||||
|
||||
// Update the index feed with the last updated timestamp
|
||||
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($webRoot . '/www/opds/index.xml')));
|
||||
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
||||
$xml->registerXPathNamespace('schema', 'http://schema.org/');
|
||||
|
||||
$allUpdated = $xml->xpath('/feed/entry[id="https://standardebooks.org/opds/all"]/updated')[0];
|
||||
$allUpdated[0] = $updatedTimestamp;
|
||||
file_put_contents($webRoot . '/www/opds/index.xml', str_replace(" ns=", " xmlns=", $xml->asXml()));
|
||||
exec('se clean ' . escapeshellarg($webRoot) . '/www/opds/index.xml');
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception $ex){
|
||||
rename($tempFilename, $webRoot . '/www/opds/all.xml');
|
||||
}
|
||||
|
||||
unlink($tempFilename);
|
||||
krsort($newestEbooks);
|
||||
$newestEbooks = array_slice($newestEbooks, 0, 30);
|
||||
$newestFeed = new OpdsFeed(SITE_URL . '/opds/newest', 'Newest 30 Standard Ebooks', $newestEbooks);
|
||||
$newestFeed->Save(WEB_ROOT . '/opds/newest.xml');
|
||||
|
||||
?>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue