presse/epub.php

413 lines
11 KiB
PHP
Raw Normal View History

2024-06-04 09:42:41 +02:00
<!DOCTYPE html>
2024-05-27 13:28:36 +02:00
<html>
2024-06-04 09:42:41 +02:00
<head>
2024-06-04 13:51:27 +02:00
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>Revue de presse - Epub</title>
<style>
body {
font-family: helvetica, sans-serif;
}
</style>
<link rel="icon" type="image/png" href="1F4F0_color.png" /></head>
2024-05-27 13:28:36 +02:00
<body>
<?php
2024-06-03 17:03:19 +02:00
require 'settings.php';
2024-06-03 17:29:30 +02:00
require 'TPEpubCreator.php';
2024-06-03 17:03:19 +02:00
2024-06-04 09:42:41 +02:00
function write_epub($epub)
{
if ( ! $epub->error ) {
$epub->CreateEPUB();
if ( ! $epub->error ) {
echo 'Success: ' . $epub->epub_file . ' created.<br>';
}
else
{
echo $epub->error;
}
} else {
echo $epub->error;
}
}
2024-06-04 09:21:33 +02:00
date_default_timezone_set('Europe/Paris');
2024-06-03 17:03:19 +02:00
$date = (new DateTime('today'))->format('Ymd');
2024-06-03 17:29:30 +02:00
// Le Monde
if (isset($_POST['lemonde']) && $_POST['lemonde'])
{
2024-06-05 12:10:54 +02:00
// hardcoded for now
$includeimages = false;
$includecover = false;
2024-06-03 17:29:30 +02:00
$url = $_POST['lmurl'];
// 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 21:28:52 +02:00
$epub->epub_file = 'epub/lemonde.epub';
2024-06-03 17:03:19 +02:00
$epub->title = 'Le Monde ' . $date ;
2024-06-05 12:10:54 +02:00
if ($includecover)
2024-06-03 17:03:19 +02:00
{
// 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-05 12:10:54 +02:00
if ($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);
2024-06-03 21:28:52 +02:00
2024-06-03 17:03:19 +02:00
$tempcontent = file_get_contents($imageurl);
file_put_contents('temp/' . $imageid, $tempcontent);
2024-06-03 21:28:52 +02:00
2024-06-03 17:03:19 +02:00
//$epub->AddImage('temp/' . $imageid, false, false );
2024-06-03 21:28:52 +02:00
2024-06-03 17:03:19 +02:00
$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-06-04 09:42:41 +02:00
write_epub($epub);
2024-05-27 13:28:36 +02:00
}
2024-06-03 17:38:46 +02:00
// Mediapart
if (isset($_POST['mediapart']) && $_POST['mediapart'])
{
$feedurl = 'https://www.mediapart.fr/articles/feed';
$opts = [
'http' => [
'method' => "GET",
2024-06-03 21:28:52 +02:00
'header' => "Accept-language: en\nCookie: MPSESSID=" . $mp_sessionid,
2024-06-03 17:38:46 +02:00
]
];
$context = stream_context_create($opts);
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
2024-06-03 21:28:52 +02:00
$epub->epub_file = 'epub/mediapart.epub';
$epub->title = 'Mediapart ' . $date;
2024-06-03 17:38:46 +02:00
// load feeds
$feed = file_get_contents($feedurl);
$xml = new SimpleXMLElement($feed);
$items = $xml->xpath("/rss/channel/item");
foreach ($items as $item)
{
$title = $item->title;
$category = $item->xpath('dc:subject')[0];
$author = $item->xpath('dc:creator')[0];
$summary = $item->description;
$article = file_get_contents($item->link, false, $context);
$doc = new DOMDocument();
$doc->loadHTML($article);
$finder = new DomXPath($doc);
// strip images
$toremove = $finder->query('//svg');
foreach ($toremove as $elt)
{
$elt->parentNode->removeChild($elt);
}
$toremove = $finder->query('//figure');
foreach ($toremove as $elt)
{
$elt->parentNode->removeChild($elt);
}
$toremove = $finder->query('//span[@class="screen-reader-only"]');
foreach ($toremove as $elt)
{
$elt->parentNode->removeChild($elt);
}
$result = '<h1>' . $title . '</h1>';
$result .= '<p>' . $author . '</p>';
$result .= '<p>' . $item->pubDate . '</p>';
$result .= '<p><b>' . $summary . '</b></p>';
$nodes = $finder->query('//div[contains(@class, "paywall-restricted-content")]');
if (!$nodes->length)
{
// articles accès libre
$nodes = $finder->query('//div[contains(@class, "news__body__center__article")]');
}
if (!$nodes->length)
{
echo 'warning: could not get content of "' . $title . '"<br>';
}
else
{
$node = $nodes->item(0);
$innerHTML = '';
foreach ($node->childNodes as $childNode){
$innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
}
$result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>';
$epub->AddPage($result, false, $title);
}
}
2024-06-04 09:42:41 +02:00
write_epub($epub);
2024-06-03 17:38:46 +02:00
}
2024-06-03 21:28:52 +02:00
// New York Times
if (isset($_POST['nyt']) && $_POST['nyt'])
{
$feedurl = 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml';
$opts = [
'http' => [
'method' => "GET",
'header' => "Accept-language: en\nCookie: NYT-S=" . $nyt_sessionid,
]
];
$context = stream_context_create($opts);
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
$epub->epub_file = 'epub/newyorktimes.epub';
$epub->title = 'The New York Times ' . $date;
// load feeds
$feed = file_get_contents($feedurl);
$xml = new SimpleXMLElement($feed);
$items = $xml->xpath("/rss/channel/item");
foreach ($items as $item)
{
$title = $item->title;
$category = $item->xpath('dc:subject')[0];
$author = $item->xpath('dc:creator')[0];
$summary = $item->description;
$article = file_get_contents($item->link, false, $context);
$doc = new DOMDocument();
$doc->loadHTML($article);
$finder = new DomXPath($doc);
// strip images
/*$toremove = $finder->query('//svg');
foreach ($toremove as $elt)
{
$elt->parentNode->removeChild($elt);
}
$toremove = $finder->query('//figure');
foreach ($toremove as $elt)
{
$elt->parentNode->removeChild($elt);
}
$toremove = $finder->query('//span[@class="screen-reader-only"]');
foreach ($toremove as $elt)
{
$elt->parentNode->removeChild($elt);
}*/
$result = '<h1>' . $title . '</h1>';
$result .= '<p>' . $author . '</p>';
$result .= '<p>' . $item->pubDate . '</p>';
$result .= '<p><b>' . $summary . '</b></p>';
$nodes = $finder->query('//section[@name="articleBody"]');
if (!$nodes->length)
{
echo 'warning: could not get content of "' . $title . '"<br>';
}
else
{
$node = $nodes->item(0);
$innerHTML = '';
foreach ($node->childNodes as $childNode){
$innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
}
$result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>';
$epub->AddPage($result, false, $title);
}
}
2024-06-04 09:42:41 +02:00
write_epub($epub);
2024-06-03 21:28:52 +02:00
}
2024-06-05 12:14:44 +02:00
// New York Times today's paper
if (isset($_POST['nyttoday']) && $_POST['nyttoday'])
{
$todaypage = file_get_contents("https://www.nytimes.com/section/todayspaper");
$opts = [
'http' => [
'method' => "GET",
'header' => "Accept-language: en\nCookie: NYT-S=" . $nyt_sessionid,
]
];
$context = stream_context_create($opts);
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
$epub->epub_file = 'epub/newyorktimestoday.epub';
$epub->title = 'The New York Times today\'s paper ' . $date;
// grab today's paper
$start = strpos($todaypage, "window.__preloadedData = ") + 25;
$end = strpos($todaypage, "};", $start) + 1;
$json = str_replace(":undefined", ":null", substr($todaypage, $start, $end - $start));
$data = json_decode($json);
foreach($data->initialState as $item)
{
if ($item->__typename == "Article")
{
$headline = ($data->initialState->{ $item->headline->id });
$title = $headline->default;
$summary = $item->summary;
$result = '<h1>' . $title . '</h1>';
2024-06-05 12:51:50 +02:00
// authors
$result .= '<p>';
$creators = ($data->initialState->{ $item->bylines[0]->id })->creators;
foreach ($creators as $creator)
{
$author = $data->initialState->{ $creator->id };
$result .= $author->displayName . '&nbsp;';
}
$result .= '</p>';
2024-06-05 12:51:50 +02:00
$result .= '<p>' . $item->lastMajorModification . '</p>';
$result .= '<p><b>' . $summary . '</b></p>';
2024-06-03 21:28:52 +02:00
$article = file_get_contents($item->url, false, $context);
$doc = new DOMDocument();
$doc->loadHTML($article);
$finder = new DomXPath($doc);
$nodes = $finder->query('//section[@name="articleBody"]');
if (!$nodes->length)
{
echo 'warning: could not get content of "' . $title . '"<br>';
}
else
{
$node = $nodes->item(0);
$innerHTML = '';
foreach ($node->childNodes as $childNode){
$innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
}
$result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>';
$epub->AddPage($result, false, $title);
}
}
}
write_epub($epub);
}
?>
<h3>Generate epub</h3>
2024-06-03 21:28:52 +02:00
<div>
2024-05-27 13:28:36 +02:00
<form method="post">
Parameters:
<br>
GetPublicationContentItems url: <input name="lmurl"><br>
2024-06-05 12:10:54 +02:00
<!--Update MPSESSID: <input name="mpsessid"><br>
Update NYT-S: <input name="nyts"><br>-->
<br>
Newspapers:
<br>
<input id="lemonde" name="lemonde" type="checkbox">
<label for="lemonde">Le Monde</label>
<br>
<input id="mediapart" name="mediapart" type="checkbox">
2024-06-05 11:46:03 +02:00
<label for="mediapart">Mediapart (RSS)</label>
<br>
<input id="nyt" name="nyt" type="checkbox">
2024-06-05 11:46:03 +02:00
<label for="nyt">The New York Times (RSS)</label>
<br>
<input id="nyttoday" name="nyttoday" type="checkbox">
2024-06-05 11:46:03 +02:00
<label for="nyttoday">The New York Times (today's paper)</label>
<br>
2024-05-27 13:28:36 +02:00
<input type="submit">
</form>
2024-06-03 21:28:52 +02:00
</div>
2024-06-05 12:10:54 +02:00
<br>
<a href="../epub">Fichiers générés</a><br>
<a href="../">Revue de presse</a>
2024-05-27 13:28:36 +02:00
</body>
</html>