presse/mediapart.php

89 lines
2.1 KiB
PHP
Raw Normal View History

2024-05-23 17:00:20 +02:00
<?php
$sessionid = 'value of cookie MPSESSID';
2024-05-27 14:29:24 +02:00
require('TPEpubCreator.php');
2024-05-23 17:00:20 +02:00
$feedurl = 'https://www.mediapart.fr/articles/feed';
$opts = [
'http' => [
'method' => "GET",
'header' => "Accept-language: en\nCookie: MPSESSID=" . $sessionid,
]
];
$context = stream_context_create($opts);
2024-05-27 14:29:24 +02:00
$epub = new TPEpubCreator();
$epub->temp_folder = 'temp/';
$epub->epub_file = 'mediapart.epub';
$epub->title = 'Mediapart';
2024-05-23 17:00:20 +02:00
// load feeds
$feed = file_get_contents($feedurl);
$xml = new SimpleXMLElement($feed);
$items = $xml->xpath("/rss/channel/item");
setlocale(LC_ALL, 'fr_FR.UTF8', 'fr_FR','fr','fr','fra','fr_FR@euro');
$localedate = strftime("%A %d %B %Y");
foreach ($items as $item)
{
$title = $item->title;
2024-05-27 14:29:24 +02:00
$category = $item->xpath('dc:subject')[0];
$author = $item->xpath('dc:creator')[0];
$summary = $item->description;
2024-05-27 14:29:24 +02:00
$article = file_get_contents($item->link, false, $context);
$doc = new DOMDocument();
$doc->loadHTML($article);
$finder = new DomXPath($doc);
2024-05-27 14:29:24 +02:00
// clean images
$figures = $finder->query('//figure');
foreach ($figures as $figure)
{
$figure->parentNode->removeChild($figure);
}
2024-05-27 14:29:24 +02:00
// clean "à lire aussi" and inline quotes
$asides = $finder->query('//aside');
foreach ($asides as $aside)
{
$aside->parentNode->removeChild($aside);
}
2024-05-27 14:29:24 +02:00
// improve section titles
$asides = $finder->query('//h2');
foreach ($asides as $aside)
{
$aside->textContent = "\n*" . $aside->textContent . "*\n";
2024-05-27 14:29:24 +02:00
// todo keep h2
2024-05-23 17:00:20 +02:00
}
2024-05-27 14:29:24 +02:00
$maintext = $finder->query('//div[contains(@class, "paywall-restricted-content")]');
2024-05-27 14:29:24 +02:00
$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;
2024-05-23 17:00:20 +02:00
}
2024-05-27 14:29:24 +02:00
if ( ! $epub->error )
{
$epub->CreateEPUB();
2024-05-23 17:00:20 +02:00
2024-05-27 14:29:24 +02:00
if ( ! $epub->error ) {
echo 'Success: Download your book <a href="' . $epub->epub_file . '">here</a>.';
}
2024-05-23 17:00:20 +02:00
2024-05-27 14:29:24 +02:00
} else {
echo $epub->error;
}
2024-05-23 13:59:38 +02:00
?>