first attempt to get mediapart as epub

This commit is contained in:
quenousimporte 2024-05-27 14:29:24 +02:00
parent 1155335bf1
commit d92509a9f5
2 changed files with 35 additions and 28 deletions

View File

@ -17,7 +17,7 @@
<h2>Quotidiens</h2> <h2>Quotidiens</h2>
<p> <p>
<a href="https://mediapart.fr">Mediapart</a> <a href="https://mediapart.fr">Mediapart</a>
- <a href="./mediapart.php">10 derniers articles en version texte</a><br> - <a href="./mediapart.php">Epub (10 derniers)</a><br>
<a href="https://lesjours.fr">Les jours</a><br> <a href="https://lesjours.fr">Les jours</a><br>
<a href="https://reporterre.net/">Reporterre</a><br> <a href="https://reporterre.net/">Reporterre</a><br>
<a href="https://basta.media">Basta!</a><br> <a href="https://basta.media">Basta!</a><br>

View File

@ -1,6 +1,7 @@
<?php <?php
$sessionid = 'value of cookie MPSESSID'; $sessionid = 'value of cookie MPSESSID';
require('TPEpubCreator.php');
$feedurl = 'https://www.mediapart.fr/articles/feed'; $feedurl = 'https://www.mediapart.fr/articles/feed';
$opts = [ $opts = [
'http' => [ 'http' => [
@ -10,6 +11,11 @@ $opts = [
]; ];
$context = stream_context_create($opts); $context = stream_context_create($opts);
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
$epub->epub_file = 'mediapart.epub';
$epub->title = 'Mediapart';
// load feeds // load feeds
$feed = file_get_contents($feedurl); $feed = file_get_contents($feedurl);
$xml = new SimpleXMLElement($feed); $xml = new SimpleXMLElement($feed);
@ -18,65 +24,66 @@ $items = $xml->xpath("/rss/channel/item");
setlocale(LC_ALL, 'fr_FR.UTF8', 'fr_FR','fr','fr','fra','fr_FR@euro'); setlocale(LC_ALL, 'fr_FR.UTF8', 'fr_FR','fr','fr','fra','fr_FR@euro');
$localedate = strftime("%A %d %B %Y"); $localedate = strftime("%A %d %B %Y");
$header = "Mediapart - " . $localedate . "\n\nSommaire:\n";
$result = '';
foreach ($items as $item) foreach ($items as $item)
{ {
$title = $item->title; $title = $item->title;
//$category = $item->category;
$category = $item->xpath('dc:subject')[0]; $category = $item->xpath('dc:subject')[0];
$author = $item->xpath('dc:creator')[0]; $author = $item->xpath('dc:creator')[0];
$summary = $item->description; $summary = $item->description;
// Add to content
$header .= '- ' . $title . "\n";
$article = file_get_contents($item->link, false, $context); $article = file_get_contents($item->link, false, $context);
$doc = new DOMDocument(); $doc = new DOMDocument();
$doc->loadHTML($article); $doc->loadHTML($article);
$finder = new DomXPath($doc); $finder = new DomXPath($doc);
// clean images // clean images
$figures = $finder->query('//figure'); $figures = $finder->query('//figure');
foreach ($figures as $figure) foreach ($figures as $figure)
{ {
$figure->parentNode->removeChild($figure); $figure->parentNode->removeChild($figure);
} }
// clean "à lire aussi" and inline quotes // clean "à lire aussi" and inline quotes
$asides = $finder->query('//aside'); $asides = $finder->query('//aside');
foreach ($asides as $aside) foreach ($asides as $aside)
{ {
$aside->parentNode->removeChild($aside); $aside->parentNode->removeChild($aside);
} }
// improve section titles // improve section titles
$asides = $finder->query('//h2'); $asides = $finder->query('//h2');
foreach ($asides as $aside) foreach ($asides as $aside)
{ {
$aside->textContent = "\n*" . $aside->textContent . "*\n"; $aside->textContent = "\n*" . $aside->textContent . "*\n";
// todo keep h2
} }
// Output
$result .= "\n------\n\n";
$result .= "Article : " . $item->title . "\n";
$result .= "Date : " . $item->pubDate . "\n";
$result .= 'Catégorie : ' . str_replace("\n", '', $category) . "\n";
$result .= 'Auteur.ice : ' . $author . "\n";
$result .= "Introduction : " . $summary . "\n";
$result .= "\n";
// article main text
$maintext = $finder->query('//div[contains(@class, "paywall-restricted-content")]'); $maintext = $finder->query('//div[contains(@class, "paywall-restricted-content")]');
$result .= $maintext->item(0)->textContent; $result = '<h1>' . $title . '</h1>';
$result .= '<p>' . $author . '</p>';
$result .= '<p>' . $item->pubDate . '</p>';
$result .= '<p><b>' . $summary . '</b></p>';
// todo use html instead
$result .= '<div>' . str_replace("\n", '<br>', $maintext->item(0)->textContent) . '</div>';
//echo $result;
$epub->AddPage($result, false, $title);
//break;
} }
$filename = 'mediapart' . str_replace(' ', '', $localedate) . '.txt'; if ( ! $epub->error )
{
$epub->CreateEPUB();
header('Content-Type: application/text; charset=utf-8'); if ( ! $epub->error ) {
header('Content-Disposition: attachment; filename=' . $filename); echo 'Success: Download your book <a href="' . $epub->epub_file . '">here</a>.';
echo $header . "\n" . $result; }
} else {
echo $epub->error;
}
?> ?>