Skip to content

Instantly share code, notes, and snippets.

@ariefmulya
Created October 30, 2010 17:19
Show Gist options
  • Save ariefmulya/655537 to your computer and use it in GitHub Desktop.
Save ariefmulya/655537 to your computer and use it in GitHub Desktop.
Multiple RSS feeds reading applications
<?php
/*
*
* Copyright (C) 2010 - Arief M Utama <me@arief-mulya.com>
* All Rights Reserved.
*
* Feeds Reader - Multiple feeds reading
*
* Version: 0.9
* Requirement: PHP >= 5.2
*/
$outfile = 'myreader.xml';
// simple caching implementation
$refresh_time = 900; // 15 mins
if ($_GET['realtimemode'] != '1') {
if (file_exists($outfile) && ( (time() - filemtime($outfile)) <= $refresh_time)) {
echo file_get_contents($outfile);
exit(null);
}
}
$sources = array(
'USGS EarthQuake' => 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M5.xml',
'TEMPO' => 'http://rss.tempointeraktif.com/index.xml',
'TEMPO Fokus' => 'http://rss.tempointeraktif.com/fokus.xml',
'detikcom' => 'http://rss.detik.com',
'MetroTV' => 'http://www.metrotvnews.com/index.php/metromain/rss/nusantara',
'MetroTV' => 'http://www.metrotvnews.com/index.php/metromain/rss/internasional',
'TVONE' => 'http://www.tvone.co.id/rss/news/',
);
//
// Some RSS feeds does not give correct timezone information
// they used GMT when they mean +0700 (or Asia/Jakarta)
// Sometimes they just dont provide timezone info at all
// this makes our ordering failed or the time convert shows future time
// hence, I use this structure
//
$date_format = array( // 0 == use original pubDate timezone, 1 force JAVT on the pubDate even if it stated otherwise
'USGS EarthQuake' => 0,
'TEMPO' => 0,
'TEMPO Fokus' => 0,
'detikcom' => 0,
'MetroTV' => 1,
'MetroTV' => 1,
'TVONE' => 0,
);
// in case our php config does not set correctly
date_default_timezone_set('Asia/Jakarta');
// this one for later use (see below)
$javt = new DateTimeZone('Asia/Jakarta');
$feeds = array();
foreach ($sources as $t => $s) {
$doc = new DOMDocument();
$doc->load($s);
foreach ($doc->getElementsByTagName('item') as $node) {
$dt = trim($node->getElementsByTagName('pubDate')->item(0)->nodeValue);
if ($date_format[$t]) {
// shall handle GMT==JAVT case nicely
$dt = substr($dt, 0, 25 /* 25== date string length without timezone info */ );
}
// and we dont need to worry bout empty timezones
// php's new DateTime class take care of it
$date_object = new DateTime($dt);
// date_default_timezone_set above was used on cases where timezone info is empty
// this one function, convert the timezone, make sure we're in the right zone
$date_object->setTimezone($javt);
$converted_date = $date_object->format('D, d M Y H:i:s O'); // uses standard format on our result
$item = array(
'title' => '['. $t . '] ' . trim($node->getElementsByTagName('title')->item(0)->nodeValue),
'desc' => trim($node->getElementsByTagName('description')->item(0)->nodeValue),
'link' => trim($node->getElementsByTagName('link')->item(0)->nodeValue),
'date' => $converted_date,
'timestamp' => strtotime($converted_date),
);
array_push($feeds, $item);
unset($date_object);
}
unset($doc);
}
function timecmp($a, $b)
{
if ($a['timestamp'] == $b['timestamp']) return 1;
return ($a['timestamp'] < $b['timestamp']) ? 1 : -1;
}
usort($feeds, 'timecmp');
$n = count($feeds);
ob_start();
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo ' <rss version="2.0">' . "\n";
echo ' <channel>' . "\n";
echo ' <title>amu@reader</title>' . "\n";
echo ' <link>http://www.arief-mulya.com</link>' . "\n";
echo ' <description>Arief Mulya Utama Reading Feeds</description>' . "\n";
$i = 0;
foreach ($feeds as $item) {
$i++;
echo ' <item>' . "\n";
echo ' <title><![CDATA[' . ($item['title']) . ' - ' .$i. ' of ' .$n. ']]></title>' . "\n";
echo ' <link><![CDATA[' . ($item['link']) . ']]></link>' . "\n";
echo ' <pubDate><![CDATA[' . ($item['date']) . ']]></pubDate>' . "\n";
echo ' <description><![CDATA[' . ($item['desc']) . ']]></description>' . "\n";
echo ' </item>' . "\n";
}
echo ' </channel>' . "\n";
echo ' </rss>' . "\n";
$out = ob_get_contents();
ob_end_clean();
// cache the output
file_put_contents($outfile, $out);
echo $out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment