presse/lemonde.php

130 lines
3.5 KiB
PHP
Raw Normal View History

2024-05-27 13:28:36 +02:00
<html>
<body>
<?php
2024-06-03 17:03:19 +02:00
require 'settings.php';
2024-05-27 13:28:36 +02:00
$url = '';
2024-06-03 17:03:19 +02:00
$date = (new DateTime('today'))->format('Ymd');
2024-05-27 13:28:36 +02:00
if (isset($_POST['url']))
{
require('TPEpubCreator.php');
2024-05-27 13:28:36 +02:00
$url = $_POST['url'];
// extract url from curl command
if (str_starts_with($url, 'curl '))
{
$url = explode("'", $url)[1];
echo '<p>extracted url from curl command:</p><p>' . $url . '</p>';
}
2024-05-27 13:28:36 +02:00
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
2024-06-03 17:03:19 +02:00
$epub->epub_file = 'epub/lemonde' . $date . '.epub';
$epub->title = 'Le Monde ' . $date ;
2024-06-03 17:03:19 +02:00
if ($lm_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 );
}
2024-05-27 13:28:36 +02:00
// cache json in case url expires
$tempjsonpath = 'temp/' . hash('md5', $url) . '.json';
$json = '';
if (file_exists($tempjsonpath))
{
$json = file_get_contents($tempjsonpath);
}
else
{
$json = file_get_contents($url);
file_put_contents($tempjsonpath, $json);
}
$publication = json_decode($json);
$content = array_filter($publication->Content, function($item) { return $item->Category == 'Le Monde'; });
usort($content, function ($a, $b) { return $a->PageNumber - $b->PageNumber; });
2024-05-27 13:28:36 +02:00
foreach ($content as $article)
{
$articlebody = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'text/xml'; });
$articlebody = array_values($articlebody)[0];
2024-05-27 13:28:36 +02:00
if ($articlebody->Title && $articlebody->HtmlText)
{
$pagecontent = '<h1>'. strip_tags($articlebody->Title) . '</h1>';
2024-06-03 17:03:19 +02:00
$pagecontent .= '<p>Page ' . $article->PageNumber . '</p>';
$author = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'author/xml'; });
$author = array_values($author)[0];
if ($author->Author)
{
$pagecontent .= $author->Author;
}
2024-05-27 13:28:36 +02:00
if ($articlebody->Introduction)
{
$pagecontent .= '<b>' . $articlebody->Introduction . '</b>';
}
2024-06-03 17:03:19 +02:00
if ($lm_includeimages)
{
2024-06-03 17:03:19 +02:00
$images = array_values(array_filter($article->ContentItem, function($item) { return $item->ContentType == 'graphic/jpeg' || $item->ContentType == 'image/jpeg'; }));
foreach ($images as $image)
{
2024-06-03 17:03:19 +02:00
$imageid = $image->ContentItemId;
$imageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Image-MEDIUM-' . $imageid . '.jpg', $url);
$tempcontent = file_get_contents($imageurl);
file_put_contents('temp/' . $imageid, $tempcontent);
//$epub->AddImage('temp/' . $imageid, false, false );
$pagecontent .= '<div><img src="' . $imageurl . '"></div>';
if ($image->HtmlText)
{
$pagecontent .= $image->HtmlText;
}
}
2024-06-03 17:03:19 +02:00
}
2024-05-27 13:28:36 +02:00
$pagecontent .= $articlebody->HtmlText;
$epub->AddPage($pagecontent, false, strip_tags($articlebody->Title));
}
}
2024-05-27 13:28:36 +02:00
if ( ! $epub->error ) {
$epub->CreateEPUB();
2024-05-27 13:28:36 +02:00
if ( ! $epub->error ) {
2024-06-03 17:23:39 +02:00
echo 'Success: ' . $epub->epub_file . ' created.<br>';
2024-05-27 13:28:36 +02:00
}
else
{
echo $epub->error;
}
2024-05-27 13:28:36 +02:00
} else {
echo $epub->error;
}
}
2024-06-03 17:03:19 +02:00
// list existing files
$files = glob('epub/*');
foreach ($files as $file)
{
2024-06-03 17:19:38 +02:00
echo '<a href="' . $file . '">' . $file . '</a><br>';
2024-06-03 17:03:19 +02:00
}
2024-05-27 13:28:36 +02:00
?>
<form method="post">
2024-06-03 17:23:39 +02:00
GetPublicationContentItems url: <input name="url" value="<?php echo $url; ?>"><br>
2024-05-27 13:28:36 +02:00
<input type="submit">
</form>
</body>
</html>