Compare commits

...

2 Commits

Author SHA1 Message Date
quenousimporte 545507773a fix cumulative effect 2024-06-12 10:54:32 +02:00
quenousimporte 7c2d9c92d3 le monde add each page preview + cover 2024-06-12 10:54:18 +02:00
1 changed files with 50 additions and 13 deletions

View File

@ -19,10 +19,17 @@
function write_epub($epub) function write_epub($epub)
{ {
if ( ! $epub->error ) { if (file_exists($epub->epub_file))
{
unlink($epub->epub_file);
}
if (!$epub->error)
{
$epub->CreateEPUB(); $epub->CreateEPUB();
if ( ! $epub->error ) { if (!$epub->error)
{
echo 'Success: <a href="' . $epub->epub_file . '">' . $epub->epub_file . '</a> created.<br>'; echo 'Success: <a href="' . $epub->epub_file . '">' . $epub->epub_file . '</a> created.<br>';
} }
else else
@ -30,7 +37,9 @@
echo $epub->error; echo $epub->error;
} }
} else { }
else
{
echo $epub->error; echo $epub->error;
} }
} }
@ -41,9 +50,10 @@
// Le Monde // Le Monde
if (isset($_POST['lemonde']) && $_POST['lemonde']) if (isset($_POST['lemonde']) && $_POST['lemonde'])
{ {
// hardcoded for now // parameters hardcoded for now
$includeimages = true; $includeimages = true;
$includecover = false; $includepages = true;
$url = $_POST['lmurl']; $url = $_POST['lmurl'];
// extract url from curl command // extract url from curl command
@ -58,13 +68,6 @@
$epub->epub_file = 'epub/lemonde.epub'; $epub->epub_file = 'epub/lemonde.epub';
$epub->title = 'Le Monde ' . $date ; $epub->title = 'Le Monde ' . $date ;
if ($includecover)
{
// todo get correct cover according to date and time
$coverurl = 'https://www.lemonde.fr/thumbnail/journal/'. $date .'/1000/1490';
$epub->AddImage( $coverurl, 'image/jpeg', true );
}
// cache json in case url expires // cache json in case url expires
$tempjsonpath = 'temp/' . hash('md5', $url) . '.json'; $tempjsonpath = 'temp/' . hash('md5', $url) . '.json';
$json = ''; $json = '';
@ -82,8 +85,38 @@
$content = array_filter($publication->Content, function($item) { return $item->Category == 'Le Monde'; }); $content = array_filter($publication->Content, function($item) { return $item->Category == 'Le Monde'; });
usort($content, function ($a, $b) { return $a->PageNumber - $b->PageNumber; }); usort($content, function ($a, $b) { return $a->PageNumber - $b->PageNumber; });
if ($includepages)
{
$pageindex = json_decode(file_get_contents(str_replace('GetPublicationContentItems', 'GetPublicationPages', $url)));
}
$page = 0;
foreach ($content as $article) foreach ($content as $article)
{ {
if ($includepages && $article->PageNumber > $page)
{
$page = $article->PageNumber;
$pageobj = array_values(array_filter($pageindex->Page, function($p)
{
global $page;
return $page == $p->PageNumber;
}))[0];
$pageid = $pageobj->PublicationPageID;
// SMALL / MEDIUM / XLARGE
$pageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Preview-MEDIUM-' . $pageid . '.jpg', $url);
$tempimgpath = './temp/' . $pageid . '.jpg';
$tempcontent = file_get_contents($pageurl);
file_put_contents($tempimgpath, $tempcontent);
$epub->AddPage('<img src="' . $tempimgpath . '">', false, 'Page ' . $page, true);
// Add page 1 as cover
if ($page == 1)
{
$epub->AddImage($tempimgpath, 'image/jpeg', true);
}
}
$articlebody = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'text/xml'; }); $articlebody = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'text/xml'; });
$articlebody = array_values($articlebody)[0]; $articlebody = array_values($articlebody)[0];
@ -117,11 +150,15 @@
$tempimgpath = './temp/' . $imageid . '.jpg'; $tempimgpath = './temp/' . $imageid . '.jpg';
$tempcontent = file_get_contents($imageurl); $tempcontent = file_get_contents($imageurl);
file_put_contents($tempimgpath, $tempcontent); file_put_contents($tempimgpath, $tempcontent);
$pagecontent .= '<div><img src="' . $tempimgpath . '"></div>'; $pagecontent .= '<p><img src="' . $tempimgpath . '"></p>';
} }
} }
$pagecontent .= $articlebody->HtmlText; $pagecontent .= $articlebody->HtmlText;
// debug
// echo '<pre style="white-space: pre-wrap;">' . $pagecontent . '<br>----------------------</pre>';
$epub->AddPage($pagecontent, false, strip_tags($articlebody->Title), true); $epub->AddPage($pagecontent, false, strip_tags($articlebody->Title), true);
} }
} }