refactor and add nyt

This commit is contained in:
quenousimporte 2024-06-03 21:28:52 +02:00
parent 1eb8d51d3c
commit 15a4e1de04
2 changed files with 117 additions and 11 deletions

127
epub.php
View File

@ -22,7 +22,7 @@
$epub = new TPEpubCreator(); $epub = new TPEpubCreator();
$epub->temp_folder = 'temp/'; $epub->temp_folder = 'temp/';
$epub->epub_file = 'epub/lemonde' . $date . '.epub'; $epub->epub_file = 'epub/lemonde.epub';
$epub->title = 'Le Monde ' . $date ; $epub->title = 'Le Monde ' . $date ;
if ($lm_includecover) if ($lm_includecover)
@ -78,12 +78,12 @@
{ {
$imageid = $image->ContentItemId; $imageid = $image->ContentItemId;
$imageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Image-MEDIUM-' . $imageid . '.jpg', $url); $imageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Image-MEDIUM-' . $imageid . '.jpg', $url);
$tempcontent = file_get_contents($imageurl); $tempcontent = file_get_contents($imageurl);
file_put_contents('temp/' . $imageid, $tempcontent); file_put_contents('temp/' . $imageid, $tempcontent);
//$epub->AddImage('temp/' . $imageid, false, false ); //$epub->AddImage('temp/' . $imageid, false, false );
$pagecontent .= '<div><img src="' . $imageurl . '"></div>'; $pagecontent .= '<div><img src="' . $imageurl . '"></div>';
if ($image->HtmlText) if ($image->HtmlText)
{ {
@ -116,21 +116,19 @@
// Mediapart // Mediapart
if (isset($_POST['mediapart']) && $_POST['mediapart']) if (isset($_POST['mediapart']) && $_POST['mediapart'])
{ {
$sessionid = $_POST['mpsessid'];
$feedurl = 'https://www.mediapart.fr/articles/feed'; $feedurl = 'https://www.mediapart.fr/articles/feed';
$opts = [ $opts = [
'http' => [ 'http' => [
'method' => "GET", 'method' => "GET",
'header' => "Accept-language: en\nCookie: MPSESSID=" . $sessionid, 'header' => "Accept-language: en\nCookie: MPSESSID=" . $mp_sessionid,
] ]
]; ];
$context = stream_context_create($opts); $context = stream_context_create($opts);
$epub = new TPEpubCreator(); $epub = new TPEpubCreator();
$epub->temp_folder = 'temp/'; $epub->temp_folder = 'temp/';
$epub->epub_file = 'epub/mediapart' . $date . '.epub'; $epub->epub_file = 'epub/mediapart.epub';
$epub->title = 'Mediapart'; $epub->title = 'Mediapart ' . $date;
// load feeds // load feeds
$feed = file_get_contents($feedurl); $feed = file_get_contents($feedurl);
@ -213,22 +211,129 @@
} else { } else {
echo $epub->error; echo $epub->error;
} }
} }
// 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)
{
// 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);
}
$outerHTML = $node->ownerDocument->saveHTML($node);
$textcontent = $node->textContent;
$result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>';
$epub->AddPage($result, false, $title);
}
}
if ( ! $epub->error ) {
$epub->CreateEPUB();
if ( ! $epub->error ) {
echo 'Success: ' . $epub->epub_file . ' created.<br>';
}
else
{
echo $epub->error;
}
} else {
echo $epub->error;
}
}
// list existing files // list existing files
$files = glob('epub/*'); $files = glob('epub/*');
foreach ($files as $file) foreach ($files as $file)
{ {
echo '<a href="' . $file . '">' . $file . '</a><br>'; echo '<a href="' . $file . '">' . $file . '</a> ' . date('F d Y H:i:s', filemtime($file)) . '<br>';
} }
?> ?>
<div>
Generate epub:<br>
<form method="post"> <form method="post">
<input input name="lemonde" type="checkbox">Le Monde. GetPublicationContentItems url: <input name="lmurl"><br> <input input name="lemonde" type="checkbox">Le Monde. GetPublicationContentItems url: <input name="lmurl"><br>
<input input name="mediapart" type="checkbox">Mediapart.<br> <input input name="mediapart" type="checkbox">Mediapart.<br>
<input input name="nyt" type="checkbox">The New York Times.<br>
<input type="submit"> <input type="submit">
</form> </form>
</div>
</body> </body>
</html> </html>

View File

@ -2,4 +2,5 @@
$lm_includeimages = false; $lm_includeimages = false;
$lm_includecover = false; $lm_includecover = false;
$mp_sessionid = ''; $mp_sessionid = '';
$nyt_sessionid = ''
?> ?>