add nyt today's paper

add epub link on index
This commit is contained in:
quenousimporte 2024-06-04 17:19:23 +02:00
parent 289a867789
commit 1135e41203
2 changed files with 98 additions and 20 deletions

116
epub.php
View File

@ -205,9 +205,6 @@
foreach ($node->childNodes as $childNode){ foreach ($node->childNodes as $childNode){
$innerHTML .= $childNode->ownerDocument->saveHTML($childNode); $innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
} }
$outerHTML = $node->ownerDocument->saveHTML($node);
$textcontent = $node->textContent;
$result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>'; $result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>';
$epub->AddPage($result, false, $title); $epub->AddPage($result, false, $title);
@ -220,7 +217,6 @@
// New York Times // New York Times
if (isset($_POST['nyt']) && $_POST['nyt']) if (isset($_POST['nyt']) && $_POST['nyt'])
{ {
$feedurl = 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml'; $feedurl = 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml';
$opts = [ $opts = [
'http' => [ 'http' => [
@ -276,11 +272,6 @@
$result .= '<p><b>' . $summary . '</b></p>'; $result .= '<p><b>' . $summary . '</b></p>';
$nodes = $finder->query('//section[@name="articleBody"]'); $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) if (!$nodes->length)
{ {
@ -294,8 +285,6 @@
foreach ($node->childNodes as $childNode){ foreach ($node->childNodes as $childNode){
$innerHTML .= $childNode->ownerDocument->saveHTML($childNode); $innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
} }
$outerHTML = $node->ownerDocument->saveHTML($node);
$textcontent = $node->textContent;
$result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>'; $result .= '<div>' . strip_tags($innerHTML, '<p><b><h2><i>') . '</div>';
@ -306,23 +295,112 @@
write_epub($epub); write_epub($epub);
} }
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);
// list existing files $epub = new TPEpubCreator();
echo '<div>'; $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>';
// todo
//$result .= '<p>' . $author . '</p>';
//$result .= '<p>' . $item->pubDate . '</p>';
$result .= '<p><b>' . $summary . '</b></p>';
$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>Existing Files</h3>
<div>
<?php
$files = glob('epub/*'); $files = glob('epub/*');
foreach ($files as $file) foreach ($files as $file)
{ {
echo '<a href="' . $file . '">' . str_replace('epub/', '', $file) . '</a> ' . date('F d Y H:i:s', filemtime($file)) . '<br>'; echo date('F d Y H:i:s', filemtime($file)) . ' <a href="' . $file . '">' . str_replace('epub/', '', $file) . '</a><br>';
} }
echo '</div>';
?> ?>
</div>
<h3>Generate epub</h3>
<div> <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="mediapart" type="checkbox">Mediapart<br> Parameters:
<input input name="nyt" type="checkbox">The New York Times<br> <br>
GetPublicationContentItems url: <input name="lmurl"><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">
<label for="mediapart">Mediapart</label>
<br>
<input id="nyt" name="nyt" type="checkbox">
<label for="nyt">The New York Times</label>
<br>
<input id="nyttoday" name="nyttoday" type="checkbox">
<label for="nyttoday">The New York Times - today's paper (beta)</label>
<br>
<input type="submit"> <input type="submit">
</form> </form>
</div> </div>

View File

@ -52,7 +52,7 @@
- <a href="https://nouveau.europresse.com/Pdf">Autre publications</a><br> - <a href="https://nouveau.europresse.com/Pdf">Autre publications</a><br>
<a href="https://www.cairn.info/">Cairn</a> <a href="https://www.cairn.info/">Cairn</a>
- <a href="https://www.cairn.info/revue-ecologie-et-politique.htm">Écologie & politique</a><br> - <a href="https://www.cairn.info/revue-ecologie-et-politique.htm">Écologie & politique</a><br>
<a href="epub.php">Fichiers epub</a> <a href="./epub">Fichiers epub</a>
</p> </p>
</body> </body>
</html> </html>