From fd6f9b9d7622f280f92810770deb217eb7301c14 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sun, 4 Jan 2009 20:04:07 -0500 Subject: [PATCH] trac750 Automatically update linked Facebook users' statuses darcs-hash:20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz --- _darcs/inventory | 4 +- ...59da13720b18e3c570e730326f5e5ef2b2dc1ab.gz | Bin 0 -> 2082 bytes _darcs/pristine/actions/facebookhome.php | 2 +- _darcs/pristine/actions/facebookinvite.php | 2 +- _darcs/pristine/actions/facebooksettings.php | 4 +- _darcs/pristine/classes/Foreign_link.php | 32 +-- _darcs/pristine/lib/facebookaction.php | 10 +- _darcs/pristine/lib/facebookutil.php | 40 ++++ _darcs/pristine/scripts/update_facebook.php | 102 ++++++++ _darcs/tentative_pristine | 219 +++++++++++++++++- actions/facebookhome.php | 2 +- actions/facebookinvite.php | 2 +- actions/facebooksettings.php | 4 +- classes/Foreign_link.php | 32 +-- lib/facebookaction.php | 10 +- lib/facebookutil.php | 40 ++++ scripts/update_facebook.php | 102 ++++++++ 17 files changed, 550 insertions(+), 57 deletions(-) create mode 100644 _darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz create mode 100644 _darcs/pristine/lib/facebookutil.php create mode 100644 _darcs/pristine/scripts/update_facebook.php create mode 100644 lib/facebookutil.php create mode 100755 scripts/update_facebook.php diff --git a/_darcs/inventory b/_darcs/inventory index e7100599a4..8c94c559c8 100644 --- a/_darcs/inventory +++ b/_darcs/inventory @@ -112,4 +112,6 @@ Evan Prodromou **20081230211444] [better serialization of arrays and booleans in config output Evan Prodromou **20081230211957] [wrapper element for config.xml -Evan Prodromou **20081230212202] \ No newline at end of file +Evan Prodromou **20081230212202] +[trac750 Automatically update linked Facebook users' statuses +Zach Copley **20090105010407] \ No newline at end of file diff --git a/_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz b/_darcs/patches/20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz new file mode 100644 index 0000000000000000000000000000000000000000..5eb98e9e0e5477562e6e4bccaa23b260b3e80a9c GIT binary patch literal 2082 zcmV+-2;KJ|iwFP!000001MOIQQyR$@|64xA2}>=50z*uUS)*oI#SOPmvw)lAK5D9& z>4CmB(`?vs8m%@nrM7frN0w@0w-d|=>iUa$3K4bMWm(R3lU32dD!&H z@%Hv^tMxw6l)(F3|%zS#-m6s&)Urrd3g`Su|GvQ@tLy{k2v$8;!A41CtSUObw92PH$?gqBAwm6y&6Cg_owo2H1Nf4VxS2+ zDhT7qmVqgfpB7ix#NAV;Vw%}wK^-diaKn6B!^Yfs0$alLWusLuUo6X9QXR_(qL}mC z`@O4^#Ggjz8l1Cibrlz~26UBmjGbVfhs;^eQcmFRN-doB!(7 zx9NHPP2~H#1@aAHdSamVvr5@)&Fj`W<2f7UlQfyEmUtIUm-M%oN$QEnr%v}`Fl_gF zr`>+D>f)#=^5qYPwQ9!s!>mQVSTmlpY%s|5e`0^JkPG;bRzLxE$c$YhiXVY5rc)7l z2;?CfJJpP*T8C;vV+>gGDT>oeKhBrXtng)dqw^dhoYaPTCN!hJEs-Dy8K-v~&LXld z7}*&-FYO$@HAFk^HkQpc^az@WYYsGkK){riY!qwUa>R^A4TXz~kI2P*DMnn3#|V~) zXo6c_Ty++bjVBtMlN#V!^SX}j?8ZZXZ~Q7^RflfmHx0367$SfJLfu%BDd1x$DFr1$ zJtvYL0Io^klL(`+Gz6Fi5(Tc1Fckp{7gi9yjH7@`&=U&UU@?f${_*q06|}>UN&!F9 zh)TlYeS#}JoK2CUKvaS>3SkaLi9Ex3W{er6#vnYR0%B^V4wT`!gd5!UOzphLbfpsO zAf%Pz5N&*r7(2sQL&F!q5&Co9heJ^eJ&UaP38nxaP$;@PXPm=`nw>L@dELrDYWUC{ zzPr2{Li^$qd}#Ok?Tg{3H%P4~0#E4;O=Pj@j58FCzDW}41-f8GoOk*s?~tecQ@7V0 zenL;+tUJ8u3E<8IpCqq9mX`EnlP@G zLHL9TS7;~?U_x#vCf=v)2F)bcKeNSG=hO-#Twrk;&p{W0i{^Gih=i_#LbDGiTF(w# zcRrst$5GrAa*Ul~r6~8e$_Lhr=YK10o>i}v;6{gs>?F&D(5_*M zp*DOwW+rdl)6&k`f#s$0oQh>Yz)d%m%5T!ltm>fC>zoWzwasm)LUV}`)wBNPd3tu| zm$h~WqT}z6g`}pzW z0emoZpVo#6A!k$Uf)Rb2kfj6j!kTh$fYBLfNy+pSbLMNu1fmYDIy`T+>~H*MK+z0S z8#2^)Yg9f22zf-m4G4Cg00KpSHUYp+ari4=vYA$H@_FGE>oCKBM@S45eD}GJFna4v1zv(GX zC2!E_|Jv!-;QQ~vM0(+2e|z*ZCRaJte&?5~&S2=BcQ9wCI3#lV{8Mx$S)`$Ct0U=c zn8wP?MUZinXgBdNg9qEfpbGtGR8EHsD$-6q`T`o_sZ(8o8>++^%4bARoPf!QOsV5} zXWd@M^J>t9s@rTod{u|?yj-g#@U;~%>Xc7NRMmD`1J6uv!XVK@IOaQ8WXj%_^Wu2yF3lYK zv6M8@p0Nx`%&+Vy-7JpQuPo+eF|bZ8VVh6TFehbh9EVi<6Q@?Vil~HkAvs#PxU$H< zyo|}Xm#aw~c5_$`N3q>i5=p2dzr4t|k2H75?VOWvU>_!O6GLryQo0pKR$`LS+JIzq51x->J<^S1yL5WWtVxN= zEL7%(8Y$|K84(h0Fl*V)_L~#un@LEvoAjyDCsuUlvrjFYL50?Go3`wr@}yItS@>`B z;H~UjtF>UtHuL&k}ZQh50QB5 z7AS@xCrjt&KRj%U0-fq3F^Ie3da6 zP4{-zwN+@V@6kBE`MR*pt2Kl7cc;jjfokA;E}6MWe`ZI>R%rfAJ;AhP9dXaY+tO|6 M4+zG|6?7Q@0N+6om;e9( literal 0 HcmV?d00001 diff --git a/_darcs/pristine/actions/facebookhome.php b/_darcs/pristine/actions/facebookhome.php index dcdb102e65..a23d1aa07f 100644 --- a/_darcs/pristine/actions/facebookhome.php +++ b/_darcs/pristine/actions/facebookhome.php @@ -36,7 +36,7 @@ class FacebookhomeAction extends FacebookAction $user = null; - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); # check to see whether there's already a Facebook link for this user diff --git a/_darcs/pristine/actions/facebookinvite.php b/_darcs/pristine/actions/facebookinvite.php index 48a820e44b..00efa654b1 100644 --- a/_darcs/pristine/actions/facebookinvite.php +++ b/_darcs/pristine/actions/facebookinvite.php @@ -34,7 +34,7 @@ class FacebookinviteAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); diff --git a/_darcs/pristine/actions/facebooksettings.php b/_darcs/pristine/actions/facebooksettings.php index 38542d4b6f..17812850c0 100644 --- a/_darcs/pristine/actions/facebooksettings.php +++ b/_darcs/pristine/actions/facebooksettings.php @@ -34,7 +34,7 @@ class FacebooksettingsAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); @@ -44,6 +44,8 @@ class FacebooksettingsAction extends FacebookAction .''; + $fbml .= '

Allow Identi.ca to update my Facebook status

'; + $this->show_header('Settings'); echo $fbml; diff --git a/_darcs/pristine/classes/Foreign_link.php b/_darcs/pristine/classes/Foreign_link.php index 9027ab9015..c89124d19c 100644 --- a/_darcs/pristine/classes/Foreign_link.php +++ b/_darcs/pristine/classes/Foreign_link.php @@ -4,7 +4,7 @@ */ require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; -class Foreign_link extends Memcached_DataObject +class Foreign_link extends Memcached_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -13,7 +13,7 @@ class Foreign_link extends Memcached_DataObject public $user_id; // int(4) primary_key not_null public $foreign_id; // int(4) primary_key not_null public $service; // int(4) primary_key not_null - public $credentials; // varchar(255) + public $credentials; // varchar(255) public $noticesync; // tinyint(1) not_null default_1 public $friendsync; // tinyint(1) not_null default_2 public $profilesync; // tinyint(1) not_null default_1 @@ -29,7 +29,8 @@ class Foreign_link extends Memcached_DataObject // XXX: This only returns a 1->1 single obj mapping. Change? Or make // a getForeignUsers() that returns more than one? --Zach - static function getByUserID($user_id, $service) { + static function getByUserID($user_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->user_id = $user_id; @@ -39,10 +40,11 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - - static function getByForeignID($foreign_id, $service) { + + static function getByForeignID($foreign_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->foreign_id = $foreign_id; @@ -52,28 +54,28 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - + # Convenience methods function getForeignUser() - { + { $fuser = new Foreign_user(); $fuser->service = $this->service; $fuser->id = $this->foreign_id; - + $fuser->limit(1); - + if ($fuser->find(true)) { return $fuser; } - - return null; + + return null; } - + function getUser() { return User::staticGet($this->user_id); } - + } diff --git a/_darcs/pristine/lib/facebookaction.php b/_darcs/pristine/lib/facebookaction.php index d5b7ed0fb8..ee9f783c40 100644 --- a/_darcs/pristine/lib/facebookaction.php +++ b/_darcs/pristine/lib/facebookaction.php @@ -19,7 +19,8 @@ if (!defined('LACONICA')) { exit(1); } -require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); +require_once(INSTALLDIR.'/lib/facebookutil.php'); + class FacebookAction extends Action { @@ -29,13 +30,6 @@ class FacebookAction extends Action parent::handle($args); } - function get_facebook() - { - $apikey = common_config('facebook', 'apikey'); - $secret = common_config('facebook', 'secret'); - return new Facebook($apikey, $secret); - } - function update_profile_box($facebook, $fbuid, $user) { diff --git a/_darcs/pristine/lib/facebookutil.php b/_darcs/pristine/lib/facebookutil.php new file mode 100644 index 0000000000..fc0e41e5bf --- /dev/null +++ b/_darcs/pristine/lib/facebookutil.php @@ -0,0 +1,40 @@ +. + */ + +require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); + +// Gets all the notices from users with a Facebook link since a given ID +function get_facebook_notices($since) +{ + $qry = 'SELECT notice.* ' . + 'FROM notice ' . + 'JOIN foreign_link ' . + 'WHERE notice.profile_id = foreign_link.user_id ' . + 'AND foreign_link.service = 2'; + + // XXX: What should the limit be? + return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); +} + +function get_facebook() +{ + $apikey = common_config('facebook', 'apikey'); + $secret = common_config('facebook', 'secret'); + return new Facebook($apikey, $secret); +} diff --git a/_darcs/pristine/scripts/update_facebook.php b/_darcs/pristine/scripts/update_facebook.php new file mode 100644 index 0000000000..d2440b163c --- /dev/null +++ b/_darcs/pristine/scripts/update_facebook.php @@ -0,0 +1,102 @@ +#!/usr/bin/env php +. + */ + +# Abort if called from a web server +if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { + print "This script must be run from the command line\n"; + exit(); +} + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); +define('LACONICA', true); + +require_once(INSTALLDIR . '/lib/common.php'); +require_once(INSTALLDIR . '/lib/facebookutil.php'); + +// For storing the last run date-time +$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; + +// Lock file name +$tmp_file = "/tmp/update_facebook.lock"; + +// Make sure only one copy of the script is running at a time +if (!($tmp_file = @fopen($tmp_file, "w"))) +{ + die("Can't open lock file. Script already running?"); +} + +$facebook = get_facebook(); + +$current_time = time(); + +$notice = get_facebook_notices(get_last_updated()); + +while($notice->fetch()) { + + $flink = Foreign_link::getByUserID($notice->profile_id, 2); + $fbuid = $flink->foreign_id; + + update_status($fbuid, $notice); + +} + +update_last_updated($current_time); + +exit(0); + + + +function update_status($fbuid, $notice) { + global $facebook; + + try { + + $result = $facebook->api_client->users_setStatus($notice->content, $fbuid, false, true); + + } catch(FacebookRestClientException $e){ + + print_r($e); + } + +} + +function get_last_updated(){ + global $last_updated_file, $current_time; + + $file = fopen($last_updated_file, 'r'); + + if ($file) { + $last = fgets($file); + } else { + print "Unable to read $last_updated_file. Using current time.\n"; + return $current_time; + } + + fclose($file); + + return $last; +} + +function update_last_updated($time){ + global $last_updated_file; + $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); + fwrite($file, $time); + fclose($file); +} diff --git a/_darcs/tentative_pristine b/_darcs/tentative_pristine index 6a29f612c1..15afe3e099 100644 --- a/_darcs/tentative_pristine +++ b/_darcs/tentative_pristine @@ -1,4 +1,215 @@ -hunk ./actions/twitapilaconica.php 118 -+ common_element_start('config'); -hunk ./actions/twitapilaconica.php 135 -+ common_element_end('config'); +hunk ./actions/facebookhome.php 39 +- $facebook = $this->get_facebook(); ++ $facebook = get_facebook(); +hunk ./actions/facebookinvite.php 37 +- $facebook = $this->get_facebook(); ++ $facebook = get_facebook(); +hunk ./actions/facebooksettings.php 37 +- $facebook = $this->get_facebook(); ++ $facebook = get_facebook(); +hunk ./actions/facebooksettings.php 47 ++ $fbml .= '

Allow Identi.ca to update my Facebook status

'; ++ +hunk ./classes/Foreign_link.php 7 +-class Foreign_link extends Memcached_DataObject ++class Foreign_link extends Memcached_DataObject +hunk ./classes/Foreign_link.php 16 +- public $credentials; // varchar(255) ++ public $credentials; // varchar(255) +hunk ./classes/Foreign_link.php 32 +- static function getByUserID($user_id, $service) { ++ static function getByUserID($user_id, $service) ++ { +hunk ./classes/Foreign_link.php 43 +- return null; ++ return null; +hunk ./classes/Foreign_link.php 45 +- +- static function getByForeignID($foreign_id, $service) { ++ ++ static function getByForeignID($foreign_id, $service) ++ { +hunk ./classes/Foreign_link.php 57 +- return null; ++ return null; +hunk ./classes/Foreign_link.php 59 +- ++ +hunk ./classes/Foreign_link.php 62 +- { ++ { +hunk ./classes/Foreign_link.php 66 +- ++ +hunk ./classes/Foreign_link.php 68 +- ++ +hunk ./classes/Foreign_link.php 72 +- +- return null; ++ ++ return null; +hunk ./classes/Foreign_link.php 75 +- ++ +hunk ./classes/Foreign_link.php 80 +- ++ +hunk ./lib/facebookaction.php 22 +-require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); ++require_once(INSTALLDIR.'/lib/facebookutil.php'); ++ +hunk ./lib/facebookaction.php 32 +- +- function get_facebook() +- { +- $apikey = common_config('facebook', 'apikey'); +- $secret = common_config('facebook', 'secret'); +- return new Facebook($apikey, $secret); +- } +addfile ./lib/facebookutil.php +hunk ./lib/facebookutil.php 1 ++. ++ */ ++ ++require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); ++ ++// Gets all the notices from users with a Facebook link since a given ID ++function get_facebook_notices($since) ++{ ++ $qry = 'SELECT notice.* ' . ++ 'FROM notice ' . ++ 'JOIN foreign_link ' . ++ 'WHERE notice.profile_id = foreign_link.user_id ' . ++ 'AND foreign_link.service = 2'; ++ ++ // XXX: What should the limit be? ++ return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); ++} ++ ++function get_facebook() ++{ ++ $apikey = common_config('facebook', 'apikey'); ++ $secret = common_config('facebook', 'secret'); ++ return new Facebook($apikey, $secret); ++} +addfile ./scripts/update_facebook.php +hunk ./scripts/update_facebook.php 1 ++#!/usr/bin/env php ++. ++ */ ++ ++# Abort if called from a web server ++if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { ++ print "This script must be run from the command line\n"; ++ exit(); ++} ++ ++define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); ++define('LACONICA', true); ++ ++require_once(INSTALLDIR . '/lib/common.php'); ++require_once(INSTALLDIR . '/lib/facebookutil.php'); ++ ++// For storing the last run date-time ++$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; ++ ++// Lock file name ++$tmp_file = "/tmp/update_facebook.lock"; ++ ++// Make sure only one copy of the script is running at a time ++if (!($tmp_file = @fopen($tmp_file, "w"))) ++{ ++ die("Can't open lock file. Script already running?"); ++} ++ ++$facebook = get_facebook(); ++ ++$current_time = time(); ++ ++$notice = get_facebook_notices(get_last_updated()); ++ ++while($notice->fetch()) { ++ ++ $flink = Foreign_link::getByUserID($notice->profile_id, 2); ++ $fbuid = $flink->foreign_id; ++ ++ update_status($fbuid, $notice); ++ ++} ++ ++update_last_updated($current_time); ++ ++exit(0); ++ ++ ++ ++function update_status($fbuid, $notice) { ++ global $facebook; ++ ++ try { ++ ++ $result = $facebook->api_client->users_setStatus($notice->content, $fbuid, false, true); ++ ++ } catch(FacebookRestClientException $e){ ++ ++ print_r($e); ++ } ++ ++} ++ ++function get_last_updated(){ ++ global $last_updated_file, $current_time; ++ ++ $file = fopen($last_updated_file, 'r'); ++ ++ if ($file) { ++ $last = fgets($file); ++ } else { ++ print "Unable to read $last_updated_file. Using current time.\n"; ++ return $current_time; ++ } ++ ++ fclose($file); ++ ++ return $last; ++} ++ ++function update_last_updated($time){ ++ global $last_updated_file; ++ $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); ++ fwrite($file, $time); ++ fclose($file); ++} diff --git a/actions/facebookhome.php b/actions/facebookhome.php index dcdb102e65..a23d1aa07f 100644 --- a/actions/facebookhome.php +++ b/actions/facebookhome.php @@ -36,7 +36,7 @@ class FacebookhomeAction extends FacebookAction $user = null; - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); # check to see whether there's already a Facebook link for this user diff --git a/actions/facebookinvite.php b/actions/facebookinvite.php index 48a820e44b..00efa654b1 100644 --- a/actions/facebookinvite.php +++ b/actions/facebookinvite.php @@ -34,7 +34,7 @@ class FacebookinviteAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); diff --git a/actions/facebooksettings.php b/actions/facebooksettings.php index 38542d4b6f..17812850c0 100644 --- a/actions/facebooksettings.php +++ b/actions/facebooksettings.php @@ -34,7 +34,7 @@ class FacebooksettingsAction extends FacebookAction function display() { - $facebook = $this->get_facebook(); + $facebook = get_facebook(); $fbuid = $facebook->require_login(); @@ -44,6 +44,8 @@ class FacebooksettingsAction extends FacebookAction .''; + $fbml .= '

Allow Identi.ca to update my Facebook status

'; + $this->show_header('Settings'); echo $fbml; diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php index 9027ab9015..c89124d19c 100644 --- a/classes/Foreign_link.php +++ b/classes/Foreign_link.php @@ -4,7 +4,7 @@ */ require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; -class Foreign_link extends Memcached_DataObject +class Foreign_link extends Memcached_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -13,7 +13,7 @@ class Foreign_link extends Memcached_DataObject public $user_id; // int(4) primary_key not_null public $foreign_id; // int(4) primary_key not_null public $service; // int(4) primary_key not_null - public $credentials; // varchar(255) + public $credentials; // varchar(255) public $noticesync; // tinyint(1) not_null default_1 public $friendsync; // tinyint(1) not_null default_2 public $profilesync; // tinyint(1) not_null default_1 @@ -29,7 +29,8 @@ class Foreign_link extends Memcached_DataObject // XXX: This only returns a 1->1 single obj mapping. Change? Or make // a getForeignUsers() that returns more than one? --Zach - static function getByUserID($user_id, $service) { + static function getByUserID($user_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->user_id = $user_id; @@ -39,10 +40,11 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - - static function getByForeignID($foreign_id, $service) { + + static function getByForeignID($foreign_id, $service) + { $flink = new Foreign_link(); $flink->service = $service; $flink->foreign_id = $foreign_id; @@ -52,28 +54,28 @@ class Foreign_link extends Memcached_DataObject return $flink; } - return null; + return null; } - + # Convenience methods function getForeignUser() - { + { $fuser = new Foreign_user(); $fuser->service = $this->service; $fuser->id = $this->foreign_id; - + $fuser->limit(1); - + if ($fuser->find(true)) { return $fuser; } - - return null; + + return null; } - + function getUser() { return User::staticGet($this->user_id); } - + } diff --git a/lib/facebookaction.php b/lib/facebookaction.php index d5b7ed0fb8..ee9f783c40 100644 --- a/lib/facebookaction.php +++ b/lib/facebookaction.php @@ -19,7 +19,8 @@ if (!defined('LACONICA')) { exit(1); } -require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); +require_once(INSTALLDIR.'/lib/facebookutil.php'); + class FacebookAction extends Action { @@ -29,13 +30,6 @@ class FacebookAction extends Action parent::handle($args); } - function get_facebook() - { - $apikey = common_config('facebook', 'apikey'); - $secret = common_config('facebook', 'secret'); - return new Facebook($apikey, $secret); - } - function update_profile_box($facebook, $fbuid, $user) { diff --git a/lib/facebookutil.php b/lib/facebookutil.php new file mode 100644 index 0000000000..fc0e41e5bf --- /dev/null +++ b/lib/facebookutil.php @@ -0,0 +1,40 @@ +. + */ + +require_once(INSTALLDIR.'/extlib/facebook/facebook.php'); + +// Gets all the notices from users with a Facebook link since a given ID +function get_facebook_notices($since) +{ + $qry = 'SELECT notice.* ' . + 'FROM notice ' . + 'JOIN foreign_link ' . + 'WHERE notice.profile_id = foreign_link.user_id ' . + 'AND foreign_link.service = 2'; + + // XXX: What should the limit be? + return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since); +} + +function get_facebook() +{ + $apikey = common_config('facebook', 'apikey'); + $secret = common_config('facebook', 'secret'); + return new Facebook($apikey, $secret); +} diff --git a/scripts/update_facebook.php b/scripts/update_facebook.php new file mode 100755 index 0000000000..d2440b163c --- /dev/null +++ b/scripts/update_facebook.php @@ -0,0 +1,102 @@ +#!/usr/bin/env php +. + */ + +# Abort if called from a web server +if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { + print "This script must be run from the command line\n"; + exit(); +} + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); +define('LACONICA', true); + +require_once(INSTALLDIR . '/lib/common.php'); +require_once(INSTALLDIR . '/lib/facebookutil.php'); + +// For storing the last run date-time +$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; + +// Lock file name +$tmp_file = "/tmp/update_facebook.lock"; + +// Make sure only one copy of the script is running at a time +if (!($tmp_file = @fopen($tmp_file, "w"))) +{ + die("Can't open lock file. Script already running?"); +} + +$facebook = get_facebook(); + +$current_time = time(); + +$notice = get_facebook_notices(get_last_updated()); + +while($notice->fetch()) { + + $flink = Foreign_link::getByUserID($notice->profile_id, 2); + $fbuid = $flink->foreign_id; + + update_status($fbuid, $notice); + +} + +update_last_updated($current_time); + +exit(0); + + + +function update_status($fbuid, $notice) { + global $facebook; + + try { + + $result = $facebook->api_client->users_setStatus($notice->content, $fbuid, false, true); + + } catch(FacebookRestClientException $e){ + + print_r($e); + } + +} + +function get_last_updated(){ + global $last_updated_file, $current_time; + + $file = fopen($last_updated_file, 'r'); + + if ($file) { + $last = fgets($file); + } else { + print "Unable to read $last_updated_file. Using current time.\n"; + return $current_time; + } + + fclose($file); + + return $last; +} + +function update_last_updated($time){ + global $last_updated_file; + $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); + fwrite($file, $time); + fclose($file); +} -- 2.39.5