Tweeps KML/Stream

So if you’ve been over to the “Stuff” page, you already know that I’ve started keeping up with the Tweeps that I’ve helped using a Google Earth map. I recently added a “tweetstream” of the people on the map. I thought of using a list to acquire the tweets, but I didn’t want to enter the usernames in 2 places, so I decided to instead parse the names out of the KML file. I used the native PHP extension simplexml to parse the KML file, and then used SimplePie to create a new RSS feed from all the users tweetstreams.

Updated: I have modified the script heavily, and added more comments in the code. Now I only retrieve one tweet from each user, then sort those tweets by date. The WordPress function I wrote now retrieves tweets until a certain number of characters is obtained, attempting to fill an area with tweets, regardless of individual tweet length.

Here is the code that creates the RSS feed:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
<channel>
<title>Twitter Helped KML</title>
<link>http://blog.perfectedperspectives.com/stuff</link>
<description>
Stream of tweets from tweeps I've helped with web development
</description>
<atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline/14388935.rss" rel="self"/>
<language>en-us</language>
<?php
//setup sortbydate
function sortbysub($a, $b) {
if($a['date'] > $b['date'])
return 1;
if($a['date'] < $b['date'])
return -1;
return 0;
}
//load existing KML file
$xml = simplexml_load_file('http://blog.perfectedperspectives.com/wp-content/uploads/TwitterHelped.kml');
//
foreach ($xml->Document->Folder as $folder) {
//parse each placemark for the TwitterUsername
foreach ($folder->Placemark as $placemark) {
$trimtweepurl = 'http://twitter.com/statuses/user_timeline/' . (string)trim($placemark->name, " @") . '.rss';
$tweepurls[] = $trimtweepurl;
}
}
//include SimplePie
include 'simplepie.inc';
//Setup error Checking
$firstfeed = 0;
//call SimplePie for each Tweeps RSS feed
foreach ($tweepurls as $simpleurl) {
//SimplePie parameters and setup
$feed = new SimplePie();
$feed->set_feed_url($simpleurl);
$feed->set_cache_duration (1000);
$feed->init();
$feed->handle_content_type();
//counter so that we only get the latest tweet from each tweep
$itemlimit = 0;
//retrieve the latest tweet from each feed
foreach ($feed->get_items() as $item) {
//count n tweet(s) from each user
if($itemlimit==1){break;}
//load tweet into array
$tweet['title'] = $item->get_title();
$tweet['link'] = $item->get_permalink();
$tweet['date'] = $item->get_date();
$tweet['guid'] = $item->get_id();
$tweet['description'] = $item->get_description();
//load each tweet as array element
$tweeptweets[] = $tweet;
//increment counter so we only get n tweets
$itemlimit = $itemlimit + 1;
//end foreach
}
//this if statement prevents the next from running on ALL RSS queries, this prevents having to wait for the entire script to run if the twitter ratelimit is exceeded from just the first RSS query
if($firstfeed > 0){break;}
if(in_array('<error>', $tweeptweets)){
//this is the error message displayed if Twitter denies our RSS query
?>
<item>
<title>Rate Limit Exceeded</title>
<link></link>
<pubDate></pubDate>
<guid></guid>
<description>Twitter HATES you!</description>
</item>
<?php
}
}
//here we sort the tweets by date, using the function declared earlier
usort($tweeptweets, 'sortbysub');
//flip into descending order
$tweeptweets = array_reverse($tweeptweets);
//create each item for the final rss feed
foreach ($tweeptweets as $tweeptweet) {
?>
<item>
<title><?php echo $tweeptweet['title']; ?></title>
<link><?php echo $tweeptweet['link']; ?></link>
<pubDate><?php echo $tweeptweet['date']; ?></pubDate>
<guid><?php echo $tweeptweet['guid']; ?></guid>
<description><?php echo $tweeptweet['description']; ?></description>
</item>
<?php }
//close the rss file
?>
</channel>
</rss>

I then created a shortcode in my functions.php file in my WordPress theme, using this code:

function display_helped_tweets() {
include_once(ABSPATH.WPINC.'/simplepie.inc');
$tweepstream = '<div id="tweepstream"><ul id="tweepstreamlist">';
$feed_url = 'http://tools.qtekso.com/pie/kmltwits.php';
$feed = new simplepie($feed_url);
foreach($feed->get_items() as $item) :
$tweetparts = explode(':', $item->get_title(), 2);
$tweepstream .= '<li class="tweepstreamtweet">';
$tweepstream .= '<a href="http://twitter.com/' . $tweetparts[0] . '" title="' . $item->get_title() . '">';
$tweepstream .= '@' . $tweetparts[0];
$tweepstream .= '</a>"' . $tweetparts[1] . '"</li>';
endforeach;
$tweepstream .= '</ul></div>';
return $tweepstream;
}
add_shortcode('tweepstream', 'display_helped_tweets')
Reblog this post [with Zemanta]
  • Share/Bookmark
This entry was posted in IT and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
blog comments powered by Disqus