1 contributor
<?php
include_once( 'news-constants.php' );
$rss_content = file_get_contents(NEWS_RSS_NYT);
$orgStrings = array( 'media:content');
$newStrings = array( 'mediacontent');
$rss_content = str_replace($orgStrings, $newStrings, $rss_content);
$xml = simplexml_load_string($rss_content);
if ($xml === false) {
echo 'Failed to read RSS';
} else {
$channel = array();
$channel['name'] = "nyt";
$channel['title'] = $xml->channel->title;
$channel['link'] = $xml->channel->link;
$channel['description'] = $xml->channel->description;
$channel['pubDate'] = $xml->channel->pubDate;
$channel['timestamp'] = strtotime($xml->channel->pubDate);
echo '<h4>' . $channel['title'].PHP_EOL;
echo '<button id="html-btn" onclick="printHTMLAll(\''.$channel['name'].'.html\')" style="display:inline;"><img src="img/html5.png" width="24px" height="24px"></button>'.PHP_EOL;
echo '<button id="pdf-btn" onclick="printPDFAll(\''.$channel['name'].'.pdf\')" style="display:inline;"><img src="img/pdf.png" width="24px" height="24px"></button>'.PHP_EOL;
echo '</h4>'.PHP_EOL;
$cpt=0;
foreach ($xml->channel->item as $item) {
$article = array();
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['link'] = re_remove($article['link'], '/\?partner=rss&emc=rss/');
$article['pubDate'] = $item->pubDate;
$article['timestamp'] = strtotime($item->pubDate);
$article['description'] = $item->description;
$article['image']=$item->mediacontent['url'];
$articles[$cpt] = $article;
echo '<div onclick="onArticle('.$cpt.')" style="display:inline;">'.PHP_EOL;
echo '<div class="img-menu"><img id="img-list-nyt" src="'.$article['image'].'" style="display:block;"></div><br>'.PHP_EOL;
echo '<div id="nav-up" style="display:inline;"><a href="#top"><i class="fa fa-home fa-2x"></i></a></div> '.PHP_EOL;
echo '<div id="nav-up" style="display:inline;"><a href="#article-top"><i class="fa fa-chevron-down fa-2x"></i></a></div> '.PHP_EOL;
echo $article['title'].' '.PHP_EOL;
echo '<div id="nav-source" style="display:inline;"><a href="'.$article['link'].'" target="new-'.$cpt.'"><i class="fa fa-link fa-2x"></i></a><br></div></div>'.PHP_EOL;
$cpt++;
if( $cpt > $NEWS_RSS_MAX_ITEMS ) {
break;
}
}
}
echo '</div><!-- ./col-4 -->'.PHP_EOL;
echo '<div id="article-display" class="col-6">'.PHP_EOL;
echo '<a name="article-top"></a><div id="article-current"></div>'.PHP_EOL;
echo '</div><!-- ./col-6 -->'.PHP_EOL;
$cpt=0;
foreach ($articles as $article ) {
$cpt_prev=$cpt-1;
$cpt_next=$cpt+1;
echo '<!-- ==================== article '.$cpt.'============== -->';
echo "<div class=\"article\" id=\"article-$cpt\" style=\"display: none;\">\n";
echo "<hr>";
echo "<a name=\"article-$cpt\">";
$article_content = file_get_contents($article['link']);
$article_content_utf8 = mb_convert_encoding($article_content, 'HTML-ENTITIES', "UTF-8");
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$libxml_previous_state = libxml_use_internal_errors(true);
//$doc->loadHTML($article_content);
$doc->loadHTML($article_content_utf8);
libxml_clear_errors();
libxml_use_internal_errors($libxml_previous_state);
$articles = $doc->getElementsByTagName('article');
$article_only="";
foreach ( $articles as $node) {
$article_only = DOMinnerHTML($node);
}
//Clean some sections
//$article_only = re_remove($article_only, '//');
$article_only = re_remove($article_only, '/<div id="(.+?)-slug" class="ResponsiveAd-(.+?)"><p>Advertisement<\/p><\/div>/');
$article_only = re_remove($article_only, '/<aside class="css-(.+?)"><span><\/span><\/aside>/');
$article_only = re_remove($article_only, '/<li class="css-(.+?)">(.+?)<\/li>/');
$article_only = re_remove($article_only, '/<svg(.+?)>(.+?)<\/svg>/');
$article_only = re_remove($article_only, '/<button id="desktop-sections-button" (.+?)>(.+?)<\/button>/');
$article_only = re_remove($article_only, '/<div id="sponsor-slug" (.+?)><p>Supported by<\/p><\/div>/');
$article_only = re_remove($article_only, '/<div class="ad top-wrapper" style="text-align:center;height:100%;display:block;min-height:250px"><div id="top"><\/div><\/div>/');
$article_only = re_remove($article_only, '/<div class="ad sponsor-wrapper" style="text-align:center;height:100%;display:block"><div id="sponsor"><\/div><\/div>/');
$article_only = re_remove($article_only, '/<div data-testid="lazyimage-container" style="(.+?)"><\/div>/');
$article_only = re_remove($article_only, '/<span class="(.+?)">Image<\/span>/');
$article_only = re_remove($article_only, '/<div><\/div>/');
$article_only = re_remove($article_only, '/<aside class="(.+?)"><\/aside>/');
$article_only = re_remove($article_only, '/<div id="top-slug" class="(.+?)"><p>Advertisement<\/p><\/div>/');
$article_only = re_remove($article_only, '/<span class="(.+?)">Video<\/span>/');
$article_only = re_remove($article_only, '/<span>\. <a href="http:\/\/www\.nytreprints\.com\/">Order Reprints<\/a> \| <a href="http:\/\/www\.nytimes\.com\/pages\/todayspaper\/index\.html">Today’s Paper<\/a> \| <a href="https:\/\/www\.nytimes\.com\/subscriptions\/Multiproduct\/(.+?)">Subscribe<\/a><\/span>/');
$article_only = re_remove($article_only, '/<div id="bottom-slug" class="(.+?)"><p>Advertisement<\/p><\/div>/');
$article_only = re_remove($article_only, '/define\((.+?)}\);/');
$article_only = re_remove($article_only, '/var _gaq = _gaq \|\| \[\];/');
$article_only = re_remove($article_only, '/<div class="ad sponsor-wrapper" style="text-align:center;height:100%;display:block"><div id="sponsor" class="" data-position="sponsor"><\/div><\/div>/');
$article_only = re_remove($article_only, '/<div class="ad top-wrapper" style="text-align:center;height:100%;display:block;min-height:250px"><div id="top" class="place-ad" data-position="top"><\/div><\/div>/');
//$article_only = re_remove($article_only, '//');
//$article_only = re_remove($article_only, '//');
//$article_only = re_remove($article_only, '//');
//Some little replacements
$re = '/<div id="top-wrapper" class="ResponsiveAd-(.+?)">/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<li><time class="css-(.+?)>(.+?)<\/time><\/li>/';
$article_only = preg_replace($re, '<time>\2</time>', $article_only);
$re = '/<p class="css-(.+?) (.+?)">/';
$article_only = preg_replace($re, '<p>', $article_only);
$re = '/<h1 class=(.+?)><span>(.+?)<\/span><\/h1>/';
$article_only = preg_replace($re, '<h1>\2</h1>', $article_only);
$re = '/<h2 class="(.+?)>(.+?)<\/h2>/';
$article_only = preg_replace($re, '<h2>\2</h2>', $article_only);
$re = '/<h3 class=(.+?)>(.+?)<\/h3>/';
$article_only = preg_replace($re, '<h3>\2</h3>', $article_only);
$re = '/<div class="css-(.+?) StoryBodyCompanionColumn">/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<div class="css-(.+?)">/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<div><h2 class=(.+?)>(.+?)<\/h2><\/div>/';
$article_only = preg_replace($re, '<h2>\2</h2>', $article_only);
$re = '/<div role="toolbar" aria-label="Social Media Share buttons, Save button, and Comments Panel with current comment count" class="css-(.+?)" data-testid="share-tools">/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<div class="bottom-of-article">/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<header class="css-(.+?)"><h3>(.+?)<\/h3><\/header>/';
$article_only = preg_replace($re, '', $article_only);
$re = '/<header class="css-(.+?) (.+?)"><div id="sponsor-wrapper" class="css-(.+?)">/';
$article_only = preg_replace($re, '<header><div>', $article_only);
$re = '/<li><time>(.+?)<\/time><\/li>/';
$article_only = preg_replace($re, '<em>\1</em>', $article_only);
$re = '/<\/div><\/div><\/header><section name="articleBody" itemprop="articleBody" class="css-(.+?)"><div>/';
$article_only = preg_replace($re, '</div></div></header><section><div>', $article_only);
$re = '/<header><div>/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<\/div><\/div><\/header><section><div>/';
$article_only = preg_replace($re, '</div></div><section><div>', $article_only);
$re = '/<li class="css-(.+?)">/';
$article_only = preg_replace($re, '<li style="display: none;">', $article_only);
$re = '/<a href="(.+?)amp;module=RelatedCoverage&pgtype=Article&region=Footer" class="css-(.+?)"><div>/';
$article_only = preg_replace($re, '<a style="display: none;"><div>', $article_only);
$re = '/<div><figure class="toneNews" aria-label="media" role="group" itemscope="" itemprop="associatedMedia" itemid="(.+?)-thumbLarge.jpg" itemtype="http:\/\/schema\.org\/ImageObject"><div>/';
$article_only = preg_replace($re, '<div><figure><div>', $article_only);
$re = '/<div><figure><div>/';
$article_only = preg_replace($re, '<div>', $article_only);
$re = '/<\/figure><\/div>/';
$article_only = preg_replace($re, '', $article_only);
$re = '/<div id="bottom-wrapper" class="css-(.+?)">/';
$article_only = preg_replace($re, '<div>', $article_only);
$article_only = re_remove($article_only, '/<div><button aria-haspopup="true" aria-expanded="false" (.+?)><\/button><\/div>/');
$article_only = re_remove($article_only, '/<a class="css-(.+?)" href="#site-content">Skip to content<\/a><a class="css-(.+?)" href="#site-index">Skip to site index<\/a>/');
$article_only = re_remove($article_only, '/<div><span class=""><i class="OpenCommentsButton-icon--(.+?)"><span class="OpenCommentsButton-text--(.+?)"><\/span><\/i><\/span><\/div>/');
$article_only = re_remove($article_only, '/<p><\/p>/');
$article_only = re_remove($article_only, '/<div><h2>(.+?)<\/h2><\/div>/');
$article_only = re_remove($article_only, '/<time class="css-(.+?) (.+?)" datetime="(.+?)">(.+?)<\/time>/');
$article_only = re_remove($article_only, '/<div><button type="button" class="css-(.+?)">Show All<\/button><\/div>/');
$article_only = re_remove($article_only, '/<figcaption itemprop="caption description" class="css-(.+?) (.+?)"><\/figcaption>/');
$article_only = re_remove($article_only, '/<div class="ad bottom-wrapper" style="text-align:center;height:100%;display:block;min-height:90px"><div id="bottom" class="" data-position="bottom"><\/div><\/div>/');
//$article_only = re_remove($article_only, '//');
//Finally remove empty lines
$article_only = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $article_only);
echo '<div id="nav-up" style="display:inline;"><a href="#top"><i class="fa fa-home fa-2x"></i></a></div> '.PHP_EOL;
echo '<div id="nav-source" style="display:inline;"><a href="'.$article['link'].'" target="new-'.$cpt.'"><i class="fa fa-link fa-2x"></i></a></div> '.PHP_EOL;
echo '<button id="html-btn" onclick="printHTML()" style="display:inline;"><img src="img/html5.png" width="24px" height="24px"></button>'.PHP_EOL;
echo '<button id="pdf-btn" onclick="printPDF()" style="display:inline;"><img src="img/pdf.png" width="24px" height="24px"></button>'.PHP_EOL;
echo '<div id="nav-prev" onclick="onArticle('.$cpt_prev.')" style="display:inline;"><i class="fa fa-chevron-left fa-2x"></i></div> '.PHP_EOL;
echo '<div id="nav-next" onclick="onArticle('.$cpt_next.')" style="display:inline;"><i class="fa fa-chevron-right fa-2x"></i></div>'.PHP_EOL;
echo '<div class="extract-content" id="'.$cpt.'">'.$article_only.'</div>'.PHP_EOL;
echo '<div id="nav-up" style="display:inline;"><a href="#top"><i class="fa fa-home fa-2x"></i></a></div> '.PHP_EOL;
echo '<div id="nav-up" style="display:inline;"><a href="#article-top"><i class="fa fa-chevron-up fa-2x"></i></a></div> '.PHP_EOL;
echo '<div id="nav-source" style="display:inline;"><a href="'.$article['link'].'" target="new-'.$cpt.'"><i class="fa fa-link fa-2x"></i></a></div> '.PHP_EOL;
echo '<div id="nav-prev" onclick="onArticle('.$cpt_prev.')" style="display:inline;"><i class="fa fa-chevron-left fa-2x"></i></div> '.PHP_EOL;
echo '<div id="nav-next" onclick="onArticle('.$cpt_next.')" style="display:inline;"><i class="fa fa-chevron-right fa-2x"></i></div></div>'.PHP_EOL;
$cpt++;
if( $cpt > $NEWS_RSS_MAX_ITEMS ) {
break;
}
}
?>