Create a feed from database, save to static file


#1

I am working on an app for twitter. Basically it is a daily aggregator of my followings’ timelines. I got to the phase where I can display the rss feed which was created from the data I saved in the database. Now I want to save the feed to a static file! Would anyone give me a hand on this? I spent hours and hours on this! I am using php. Following is the part of the code I use:

function createRSSshell ($data) 
{
	foreach($data as $key => $value)
	{	$data[$key] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');	}

$base_rss = <<<BASE_RSS
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>{$data['title']}</title>
		<link>{$data['link']}</link>
		<description>DwuLab's Daily Broadcast</description>
		<language>en</language>
		<pubDate>{$data['last_update']}</pubDate>
	</channel>
</rss>
BASE_RSS;
	$xml = new SimpleXMLElement ($base_rss);
	return $xml;
}
function formatTweetAsXHTML ($data)
{
	foreach($data as $key => $value)
	{	$data[$key] = htmlspecialchars ($value, ENT_QUOTES, 'UTF-8');	}
	
	$xhtml = <<<RSS_TWEET
<div name='{$data['status_id']}'>
	<div class='attribution'>
		<div class='avatar'>
			<a href='{$data['author_url']}'><img
				alt='{$data['author_username']} avatar' 
				src='{$data['author_avatar']}' /></a>
		</div>
		<div class='username'>
			<a href='{$data['author_url']}'>{$data['author_username']}</a>
		</div>
		<div class='author'>
			{$data['author_fullname']}
		</div>
	</div>
	<div class='tweet'>
			{$data['tweet_text']} . <a href='{$data['tweet_url']}'>{$data['tweet_url']}</a>
		<div class='meta'>
			<span>
				<a href='{$data['status_url']}'>{$data['pub_date']}</a>
			</span>
		</div>
	</div>
</div>
RSS_TWEET;
	return $xhtml;
}
function addItem ($xml, $child, $data)
{
	$item = $xml->addChild ($child);
	foreach ($data as $key => $value)
	{
		$value = htmlspecialchars ($value, ENT_QUOTES, 'UTF-8');
		$item->addChild ($key, $value);
	}
	
	return $item;
}
$xml_variables		= array (
				'title'		=> "DwuLab - " . $app_title,
				'link' 		=> $this_url,
				'pubDate' 	=> $last_update,
			); 
$xml			= createXMLshell ();

/*
 *   Add Daily Updates
 *
 *		Loop through the data set to aggregate individual tweets into daily 
 *		snapshots, inserting them into the XML shell before output.
 */

$count			= 0;
$this_day		= '';
$this_tweet_format	= '';
$this_tweet_authors	= array ();
foreach ($tweets_stored as $this_tweet)
{
	if (($this_day != '') && ($this_day != $this_tweet['tweet_day'])) 
	{
		$this_tweet_format 	= preg_replace('/&(?!\w+;)/', '&amp;', $this_tweet_format);

		$item			= array (
						'link'			=> $this_tweet['status_url'],
						'pubDate'		=> $this_tweet_date,
						'dc:creator'		=> implode (', ',array_keys ($this_tweet_authors)),
						'category'		=> '<![CDATA[DwuLab]]>',
						'description'		=> $this_tweet_format,
						'content:encoded' 	=> '<![CDATA['.$this_tweet_format.']]>',
						'guid'			=> $this_tweet['status_url']
					);
				
		$tweet			= addItem ($xml->channel, 'item', $item);
		$this_tweet_format 	= '';				# Reset the aggregated description
		$this_tweet_authors	= array();
		$count			+= 1;
	}
	
	# Process this tweet for the next aggregation
	$this_tweet['author_url']	= 'https://twitter.com/' . $this_tweet['author_username'];
	$this_tweet['status_url']	= $this_tweet['author_url'] . '/status/' . $this_tweet['status_id'];
	$this_tweet_authors[$this_tweet['author_fullname']] = 1;
	$this_tweet_date 		= $this_tweet['pub_date'];
	$this_title 			= date("l, F dS, Y", strtotime ($this_tweet['pub_date']));
	$this_tweet_format 		.= formatTweetAsXHTML ($this_tweet);
	$this_day 			= $this_tweet['tweet_day'];
}


#2

wewwwww


#3

if you have the complete feed source-code, it might be quite simple
… like this:

$feedcode = createRSSshell($inputdata); // ---- get Feed code from data source

$rss_file = ‘./mydata/myfeedname.rsst’; // ---- path and name for new RSS file

$handle = fopen($rss_file,“w”, 0755); // ---- open (create) new file
fwrite($handle,$feedcode); // ---- write content into file
fclose($handle); // ---- close file handler

echo "
New RSS is: " .$rss_file. " … have fun!";

the $rss_file you can name like this: year-month-day.rss

$year = date(‘Y’);
$month = date(‘m’);
$day = date(‘d’);

$rss_file = ‘./mydata/’.$year.’-’.$month.’-’.$day.’.rss’;

so you get a single file for each day (becomes lot of data to store after a while !!!)


#4