Safer, better script for automatically updating Facebook statuses

This commit is contained in:
Zach Copley 2009-02-07 18:16:34 -08:00
parent c2905085c1
commit 4b4ed63190
1 changed files with 42 additions and 48 deletions

View File

@ -34,22 +34,19 @@ require_once INSTALLDIR . '/lib/facebookutil.php';
$last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated'; $last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated';
// Lock file name // Lock file name
$tmp_file = INSTALLDIR . '/scripts/update_facebook.lock'; $lock_file = INSTALLDIR . '/scripts/update_facebook.lock';
// Make sure only one copy of the script is running at a time // Make sure only one copy of the script is running at a time
if (!($tmp_file = @fopen($tmp_file, "w"))) $lock_file = @fopen($lock_file, "w+");
{ if (!flock( $lock_file, LOCK_EX | LOCK_NB, &$wouldblock) || $wouldblock) {
die("Can't open lock file. Script already running?"); die("Can't open lock file. Script already running?\n");
} }
$facebook = getFacebook(); $facebook = getFacebook();
$current_time = time(); $current_time = time();
$since = getLastUpdated(); $since = getLastUpdated();
updateLastUpdated($current_time);
$notice = getFacebookNotices($since); $notice = getFacebookNotices($since);
$cnt = 0; $cnt = 0;
while($notice->fetch()) { while($notice->fetch()) {
@ -73,9 +70,18 @@ while($notice->fetch()) {
// Avoid a Loop // Avoid a Loop
if ($notice->source != 'Facebook') { if ($notice->source != 'Facebook') {
updateStatus($fbuid, $content);
try {
$facebook->api_client->users_setStatus($content,
$fbuid, false, true);
updateProfileBox($facebook, $flink, $notice); updateProfileBox($facebook, $flink, $notice);
$cnt++; $cnt++;
} catch(FacebookRestClientException $e) {
print "Couldn't sent notice $notice->id!\n";
print $e->getMessage();
// Remove flink?
}
} }
} }
} }
@ -83,16 +89,11 @@ while($notice->fetch()) {
if ($cnt > 0) { if ($cnt > 0) {
print date('r', $current_time) . print date('r', $current_time) .
": Found $cnt new notices to send to Facebook since last run at " . ": Found $cnt new notices for Facebook since last run at " .
date('Y-m-d H:i:s', $since) . "\n"; date('r', $since) . "\n";
} }
#Save the last updated time. It needs to do this even if there were no fclose($lock_file);
#changes made, otherwise it will never create it and thus never send
#any updates at all.
updateLastUpdated($current_time);
exit(0); exit(0);
@ -111,37 +112,30 @@ function userCanUpdate($fbuid) {
return $result; return $result;
} }
function updateStatus($fbuid, $content) {
global $facebook;
try {
$result = $facebook->api_client->users_setStatus($content, $fbuid, false, true);
} catch(FacebookRestClientException $e){
print_r($e);
}
}
function getLastUpdated(){ function getLastUpdated(){
global $last_updated_file, $current_time; global $last_updated_file, $current_time;
$last = $current_time;
$file = fopen($last_updated_file, 'r'); if (file_exists($last_updated_file) &&
($file = fopen($last_updated_file, 'r'))) {
if ($file) {
$last = fgets($file); $last = fgets($file);
} else { } else {
print "Unable to read $last_updated_file. Using current time.\n"; print "$last_updated_file doesn't exit. Trying to create it...\n";
return $current_time; $file = fopen($last_updated_file, 'w+') or
die("Can't open $last_updated_file for writing!\n");
print 'Success. Using current time (' . date('r', $last) .
") to look for new notices.\n";
} }
fclose($file); fclose($file);
return $last; return $last;
} }
function updateLastUpdated($time){ function updateLastUpdated($time){
global $last_updated_file; global $last_updated_file;
$file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); $file = fopen($last_updated_file, 'w') or
die("Can't open $last_updated_file for writing!");
fwrite($file, $time); fwrite($file, $time);
fclose($file); fclose($file);
} }