blob: 6fc85b661a99432f32471b3e6d81ac371828728c [file] [log] [blame]
<?php
require_once("nest_functions.php");
if( $_GET['debug'] == 'true' ){
echo 'turning on error reporting<br>';
error_reporting(255);
}
global $birdsnestdata;
// Check Cached Followers Feed
if(file_exists($birdsnestdata->writable_path . 'followers.data')){
$modtime = filemtime ($birdsnestdata->writable_path . 'followers.data');
$now = time();
if ($now - $modtime > 120 or $_GET['debug'] == 'true'){
$followersCount = getCacheFollowers("http://".$birdsnestdata->twitter_account.":".$birdsnestdata->twitter_pass."@twitter.com/statuses/followers.xml", $birdsnestdata->writable_path);
}
}else{
$followersCount = getCacheFollowers("http://".$birdsnestdata->twitter_account.":".$birdsnestdata->twitter_pass."@twitter.com/statuses/followers.xml", $birdsnestdata->writable_path);
}
// Check Cached Tweeter Data
if (file_exists($birdsnestdata->writable_path . 'lastID')) // Check to see if file exists
{
$modtime = filemtime ($birdsnestdata->writable_path . 'lastID'); // Check to see when it was last modified
$now = time();
if ($now - $modtime > 120 or $_GET['debug'] == 'true'){ // More then 5 minutes have passed, we need to check for updates
//Open tweetData File for writing
$statusDataHandle = fopen($birdsnestdata->writable_path . 'tweetstatus.data', 'r');
$statusData = fread($statusDataHandle, filesize($birdsnestdata->writable_path . 'tweetstatus.data'));
$statusData = unserialize($statusData);
fclose ($statusDataHandle);
//Open lastID File for keeping things moving
$lastIDHandle = fopen ($birdsnestdata->writable_path . 'lastID', 'w');
$lastID = fread($lastIDHandle, 128);
$searchURL = "http://".$birdsnestdata->twitter_account.":".$birdsnestdata->twitter_pass."@twitter.com/statuses/replies.xml";
// We use last ID to make sure were getting only things that we havent parsed yet.
if ($lastID != ""){
$searchURL .= '&since_ID='.$lastID;
}
//Now lets get the data from twitter
$newLastID = getCacheReplies($searchURL, $statusData, $birdsnestdata->writable_path);
if ($newLastID != $lastID){ // If lastID has changed then we need to save things
fwrite ($lastIDHandle, $newLastID);
}else{
fwrite ($lastIDHandle, $lastID);
}
//Close File handles
fclose ($lastIDHandle);
}
}else{
//Since the file doesnt exist lets create them
$lastIDHandle = fopen ($birdsnestdata->writable_path . 'lastID', 'w+');
//Now lets get the data from twitter
$searchURL = "http://".$birdsnestdata->twitter_account.":".$birdsnestdata->twitter_pass."@twitter.com/statuses/replies.xml?since_ID=1336792232";
//Now lets get the data from twitter
$statusData = array();
$lastID = getCacheReplies($searchURL, $statusData, $birdsnestdata->writable_path);
fwrite ($lastIDHandle, $lastID);
//Close File handles
fclose ($lastIDHandle);
}
function getCacheFollowers($url, $writable) {
global $birdsnestdata;
// Function to build a followers file that has more then 100 entries
$followers = @file_get_contents($url);
$xml = @simplexml_load_string($followers);
$followersArray = array();
$count = 0;
$masterCount = 0;
$pageCount = 2; //start at 2
while (1 and sizeof($xml->user)) {
foreach($xml->user as $user){
$screen_name = $user->screen_name;
$followersArray[(string)$screen_name]['description'] = (string)$user->description;
$followersArray[(string)$screen_name]['profile_image_url'] = (string)$user->profile_image_url;
$count++;
}
if ($count == 100){
$masterCount += $count;
$count = 0;
$followers = @file_get_contents($url . '?page='. $pageCount);
$xml = @simplexml_load_string($followers);
$pageCount++;
}elseif(sizeof($followersArray)){
$handle = fopen ($writable . 'followers.data','w+');
fwrite ($handle, serialize($followersArray));
fclose($handle);
break;
}
}
}
function getCacheReplies($replyurl, &$statusArray, $writable){
global $birdsnestdata;
// Function to build a followers file that has more then 100 entries
if($_GET['debug'] == 'true'){
print $replyurl."<br>";
}
$replies = file_get_contents($replyurl);
$xml = @simplexml_load_string($replies);
if ($xml->status[0]->id != NULL){
$lastID = $xml->status[0]->id;
$count = 0;
$masterCount = 0;
$pageCount = 2; //start at 2
while (1) {
foreach($xml->status as $status){
if($_GET['debug'] == 'true'){
print_pre($status);
}
$screen_name = $status->user->screen_name;
$text = $status->text;
$findConfHash = stripos($text, '#'.$birdsnestdata->twitter_hash);
if ($findConfHash !== FALSE){
//We found twitter hash tag, now lets check for other role tags
$findSpeaker = stripos($text, '#session');
$findExhibitor = stripos($text, '#exhibitor');
$findAttendee = stripos($text, '#attendee');
if ($findSpeaker !== FALSE){
$url = substr($text, stripos($text, 'http://'), strlen($text));
if (strpos($url, ' ') != 0)
$url = substr($url, 0, strpos($url, ' '));
$url = trim($url, '[]');
$statusArray[(string)$screen_name]['role'] = "Speaker";
$statusArray[(string)$screen_name]['url'] = $url;
}elseif ($findExhibitor !== FALSE){
$statusArray[(string)$screen_name]['role'] = "Exhibitor";
}elseif ($findAttendee !== FALSE){
$statusArray[(string)$screen_name]['role'] = "Attendee";
}
}
$count++;
}
if ($count == 20){
$masterCount += $count;
$count = 0;
$replies = @file_get_contents($replyurl . '&page='. $pageCount);
$xml = @simplexml_load_string($replies);
$pageCount++;
}else{
$handle = fopen ($writable . 'tweetstatus.data','w+');
fwrite ($handle, serialize($statusArray));
fclose($handle);
break;
}
}
}
return $lastID;
}
?>