From 7a043c3e854cc558c9d506ca0c2e601702d13d12 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 24 Jun 2020 18:33:50 -0500 Subject: [PATCH] Update generate-opds script to use the web library and templates to generate feeds --- scripts/generate-opds.php | 132 +++----------------------------------- templates/OpdsFeed.php | 35 ++++++++++ 2 files changed, 44 insertions(+), 123 deletions(-) create mode 100644 templates/OpdsFeed.php diff --git a/scripts/generate-opds.php b/scripts/generate-opds.php index 81198676..0e9cdac4 100755 --- a/scripts/generate-opds.php +++ b/scripts/generate-opds.php @@ -4,144 +4,30 @@ $options = getopt("", $longopts); $webRoot = $options["webroot"] ?? "/standardebooks.org/web"; $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 = ''; + foreach($contentFiles as $path){ if($path == '') continue; - $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($path) ?: '')); - $xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); + $ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $path) ?? ''; + $ebook = new Ebook($ebookWwwFilesystemPath); - $updated = $xml->xpath('/package/metadata/meta[@property="dcterms:modified"]') ?: []; - $identifier = $xml->xpath('/package/metadata/dc:identifier') ?: []; - - if(sizeof($identifier) > 0 && sizeof($updated) > 0){ - $sortedContentFiles[(string)$updated[0] . ' ' . $identifier[0]] = $xml; - } + $sortedContentFiles[$ebook->ModifiedTimestamp->format('Y-m-dTH:i:sZ') . ' ' . $ebook->Identifier] = $ebook; } krsort($sortedContentFiles); -ob_start(); -print("\n"); +$url = SITE_URL . '/opds/all'; -/* Notes: - -- *All* OPDS feeds must contain a rel="crawlable" link pointing to the /opds/all feed - -- The element is required to note this as a "Complete Acquisition Feeds"; see https://specs.opds.io/opds-1.2#25-complete-acquisition-feeds - -*/ -?> - - /opds/all - - - - - All Standard Ebooks - Free and liberated ebooks, carefully produced for the true book lover. - /images/logo.png - - - - Standard Ebooks - - - xpath('/package/metadata/dc:identifier') ?: []; - $identifier = (string)array_shift($temp); - $url = preg_replace('/^url:/ius', '', $identifier) ?? ''; - $url = preg_replace('/^https:\/\/standardebooks\.org/ius', $webUrl, $url) ?? ''; - $relativeUrl = preg_replace('/^' . preg_quote($webUrl, '/') . '/ius', '', $url) ?? ''; - - $temp = $xml->xpath('/package/metadata/dc:title') ?: []; - $title = array_shift($temp); - - $temp = $xml->xpath('/package/metadata/meta[@property="se:long-description"]') ?: []; - $longDescription = array_shift($temp); - - $authors = $xml->xpath('/package/metadata/dc:creator') ?: []; - - $temp = $xml->xpath('/package/metadata/dc:date') ?: []; - $published = array_shift($temp); - - $temp = $xml->xpath('/package/metadata/dc:language') ?: []; - $language = array_shift($temp); - - $temp = $xml->xpath('/package/metadata/meta[@property="dcterms:modified"]') ?: []; - $modified = array_shift($temp); - - $temp = $xml->xpath('/package/metadata/dc:description') ?: []; - $description = array_shift($temp); - - $subjects = $xml->xpath('/package/metadata/dc:subject') ?: []; - - $sources = $xml->xpath('/package/metadata/dc:source') ?: []; - - $filesystemPath = preg_replace('/\/src\/epub\/content.opf$/ius', '', $path) ?? ''; - $temp = glob($filesystemPath . '/dist/*.epub'); - $filename = preg_replace('/^url:https:\/\/standardebooks\.org\/ebooks\//ius', '', $identifier); - $epubFilename = str_replace('/', '_', $filename) . '.epub'; - $kindleFilename = str_replace('/', '_', $filename) . '.azw3'; - - ?> - - - <?= $title ?> - attributes() !== null){ - $id = $author->attributes()->id; - } - $temp = $xml->xpath('/package/metadata/meta[@property="se:url.encyclopedia.wikipedia"][@refines="#' . $id . '"]') ?: []; - $wikiUrl = array_shift($temp); - $temp = $xml->xpath('/package/metadata/meta[@property="se:name.person.full-name"][@refines="#' . $id . '"]') ?: []; - $fullName = array_shift($temp); - $temp = $xml->xpath('/package/metadata/meta[@property="se:url.authority.nacoaf"][@refines="#' . $id . '"]') ?: []; - $nacoafLink = array_shift($temp); - ?> - - - - - - - - - - - - Standard Ebooks - - - - Public domain in the United States; original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication - - - - - - - - - - - - - - - $url, 'url' => $url, 'title' => 'All Standard Ebooks', 'updatedTimestamp' => $updatedTimestamp, 'isCrawlable' => true, 'entries' => $sortedContentFiles]); $tempFilename = tempnam('/tmp/', 'se-opds-'); diff --git a/templates/OpdsFeed.php b/templates/OpdsFeed.php new file mode 100644 index 00000000..62287525 --- /dev/null +++ b/templates/OpdsFeed.php @@ -0,0 +1,35 @@ + element is required to note this as a "Complete Acquisition Feeds"; see https://specs.opds.io/opds-1.2#25-complete-acquisition-feeds + +*/ + +if(!isset($isCrawlable)){ + $isCrawlable = false; +} + +print("\n"); +?> + + + + + + + <?= $title ?> + Free and liberated ebooks, carefully produced for the true book lover. + /images/logo.png + + + + Standard Ebooks + + + + $ebook]) ?> + +