trac750 Automatically update linked Facebook users' statuses

darcs-hash:20090105010407-7b5ce-559da13720b18e3c570e730326f5e5ef2b2dc1ab.gz
This commit is contained in:
Zach Copley 2009-01-04 20:04:07 -05:00
parent d57c06b417
commit fd6f9b9d76
17 changed files with 550 additions and 57 deletions

View File

@ -112,4 +112,6 @@ Evan Prodromou <evan@prodromou.name>**20081230211444]
[better serialization of arrays and booleans in config output
Evan Prodromou <evan@prodromou.name>**20081230211957]
[wrapper element for config.xml
Evan Prodromou <evan@prodromou.name>**20081230212202]
Evan Prodromou <evan@prodromou.name>**20081230212202]
[trac750 Automatically update linked Facebook users' statuses
Zach Copley <zach@controlyourself.ca>**20090105010407]

View File

@ -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

View File

@ -34,7 +34,7 @@ class FacebookinviteAction extends FacebookAction
function display()
{
$facebook = $this->get_facebook();
$facebook = get_facebook();
$fbuid = $facebook->require_login();

View File

@ -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
.'</fb:if-section-not-added>';
$fbml .= '<fb:prompt-permission perms="status_update"><h2>Allow Identi.ca to update my Facebook status</h2></fb:prompt-permission>';
$this->show_header('Settings');
echo $fbml;

View File

@ -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);
}
}

View File

@ -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)
{

View File

@ -0,0 +1,40 @@
<?php
/*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}

View File

@ -0,0 +1,102 @@
#!/usr/bin/env php
<?php
/*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
# 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);
}

View File

@ -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 .= '<fb:prompt-permission perms="status_update"><h2>Allow Identi.ca to update my Facebook status</h2></fb:prompt-permission>';
+
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
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+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
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+# 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);
+}

View File

@ -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

View File

@ -34,7 +34,7 @@ class FacebookinviteAction extends FacebookAction
function display()
{
$facebook = $this->get_facebook();
$facebook = get_facebook();
$fbuid = $facebook->require_login();

View File

@ -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
.'</fb:if-section-not-added>';
$fbml .= '<fb:prompt-permission perms="status_update"><h2>Allow Identi.ca to update my Facebook status</h2></fb:prompt-permission>';
$this->show_header('Settings');
echo $fbml;

View File

@ -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);
}
}

View File

@ -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)
{

40
lib/facebookutil.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}

102
scripts/update_facebook.php Executable file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env php
<?php
/*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
# 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);
}