epub_file)) { unlink($epub->epub_file); } if (!$epub->error) { $epub->CreateEPUB(); if (!$epub->error) { echo 'Success: ' . $epub->epub_file . ' created.
'; } else { echo $epub->error; } } else { echo $epub->error; } } function lm_download_image($base_url, $url_part, $id) { $image_url = preg_replace('/GetPublicationContentItems-.*\.json/', $url_part . $id . '.jpg', $base_url); $temp_img_path = './temp/' . $id . '.jpg'; $temp_content = file_get_contents($image_url); file_put_contents($temp_img_path, $temp_content); return $temp_img_path; } function inner_html($node) { $innerHTML = ''; foreach ($node->childNodes as $childNode) { $innerHTML .= $childNode->ownerDocument->saveHTML($childNode); } return $innerHTML; } date_default_timezone_set('Europe/Paris'); $date = (new DateTime('today'))->format('Ymd'); // Le Monde if (isset($_POST['lemonde']) && $_POST['lemonde']) { // parameters hardcoded for now $includeimages = false; $includepages = true; $imagesonly = false; $imagesize = $imagesonly ? "XLARGE" : "MEDIUM"; $url = $_POST['lmurl']; // extract url from curl command if (str_starts_with($url, 'curl ')) { $url = explode("'", $url)[1]; echo '

extracted url from curl command:

' . $url . '

'; } $epub = new TPEpubCreator(); $epub->temp_folder = 'temp/'; $epub->epub_file = 'epub/lemonde.epub'; $epub->title = 'Le Monde ' . $date ; // 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; }); if ($includepages) { $pageindex = json_decode(file_get_contents(str_replace('GetPublicationContentItems', 'GetPublicationPages', $url))); } $page = 0; foreach ($content as $article) { if ($includepages && $article->PageNumber > $page) { $page = $article->PageNumber; $pageobj = array_values(array_filter($pageindex->Page, function($p) { global $page; return $page == $p->PageNumber; }))[0]; $pageid = $pageobj->PublicationPageID; $path = lm_download_image($url, 'Preview-' . $imagesize . '-', $pageid); if ($page == 1) { // cover $epub->AddImage($path, 'image/jpeg', true); } else { $epub->AddPage('', false, 'Page ' . $page, true); } } $articlebody = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'text/xml'; }); $articlebody = array_values($articlebody)[0]; if ($articlebody->Title && $articlebody->HtmlText) { $pagecontent = '

'. strip_tags($articlebody->Title) . '

'; $pagecontent .= '

Page ' . $article->PageNumber . '

'; $author = array_filter($article->ContentItem, function($item) { return $item->ContentType == 'author/xml'; }); $author = array_values($author)[0]; if ($author->Author) { $pagecontent .= $author->Author; } if ($articlebody->Introduction) { $pagecontent .= '' . $articlebody->Introduction . ''; } if ($includeimages) { $images = array_values(array_filter($article->ContentItem, function($item) { return $item->ContentType == 'graphic/jpeg' || $item->ContentType == 'image/jpeg'; })); foreach ($images as $image) { $path = lm_download_image($url, 'Image-MEDIUM-', $image->ContentItemId); $pagecontent .= '

'; } } $pagecontent .= $articlebody->HtmlText; if (!$imagesonly) { $epub->AddPage($pagecontent, false, strip_tags($articlebody->Title), true); } } } write_epub($epub); } // Mediapart if (isset($_POST['mediapart']) && $_POST['mediapart']) { if (isset($_POST['forcempcookies']) && $_POST['forcempcookies']) { $mp_sessionid = get_mp_cookie(); // todo save in settings } $feedurl = 'https://www.mediapart.fr/articles/feed'; $opts = [ 'http' => [ 'method' => "GET", 'header' => "Accept-language: en\nCookie: MPSESSID=" . $mp_sessionid, ] ]; $context = stream_context_create($opts); $epub = new TPEpubCreator(); $epub->temp_folder = 'temp/'; $epub->epub_file = 'epub/mediapart.epub'; $epub->title = 'Mediapart ' . $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 = '

' . $title . '

'; $result .= '

' . $author . '

'; $result .= '

' . $item->pubDate . '

'; $result .= '

' . $summary . '

'; $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 . '"
'; } else { $node = $nodes->item(0); $innerHTML = ''; foreach ($node->childNodes as $childNode){ $innerHTML .= $childNode->ownerDocument->saveHTML($childNode); } $result .= '
' . strip_tags($innerHTML, '

') . '

'; $epub->AddPage($result, false, $title); } } write_epub($epub); } // 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 = '

' . $title . '

'; $result .= '

' . $author . '

'; $result .= '

' . $item->pubDate . '

'; $result .= '

' . $summary . '

'; $nodes = $finder->query('//section[@name="articleBody"]'); if (!$nodes->length) { echo 'warning: could not get content of "' . $title . '"
'; } else { $node = $nodes->item(0); $innerHTML = ''; foreach ($node->childNodes as $childNode){ $innerHTML .= $childNode->ownerDocument->saveHTML($childNode); } $result .= '
' . strip_tags($innerHTML, '

') . '

'; $epub->AddPage($result, false, $title); } } write_epub($epub); } // 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 = '

' . $title . '

'; // authors $result .= '

'; $creators = ($data->initialState->{ $item->bylines[0]->id })->creators; foreach ($creators as $creator) { $author = $data->initialState->{ $creator->id }; $result .= $author->displayName . ' '; } $result .= '

'; $result .= '

' . $item->lastMajorModification . '

'; $result .= '

' . $summary . '

'; $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 . '"
'; } else { $node = $nodes->item(0); $innerHTML = ''; foreach ($node->childNodes as $childNode){ $innerHTML .= $childNode->ownerDocument->saveHTML($childNode); } $result .= '
' . strip_tags($innerHTML, '

') . '

'; $epub->AddPage($result, false, $title); } } } write_epub($epub); } ?>

Generate epub

Le Monde


Mediapart



The New York Times




Fichiers générés
Revue de presse