define('MAXCHILDREN', 2);
define('POLL_INTERVAL', 60); // in seconds
+$shortoptions = 'di::';
+$longoptions = array('id::', 'debug');
+
$helptext = <<<END_OF_TRIM_HELP
Batch script for retrieving Twitter messages from foreign service.
-END_OF_TRIM_HELP;
+ -i --id Identity (default 'generic')
+ -d --debug Debug (lots of log output)
-require_once INSTALLDIR.'/scripts/commandline.inc';
+END_OF_TRIM_HELP;
-require_once INSTALLDIR . '/lib/common.php';
+require_once INSTALLDIR .'/scripts/commandline.inc';
require_once INSTALLDIR . '/lib/daemon.php';
/**
{
private $_children = array();
+ function __construct($id=null, $daemonize=true)
+ {
+ parent::__construct($daemonize);
+
+ if ($id) {
+ $this->set_id($id);
+ }
+ }
+
/**
* Name of this daemon
*
function name()
{
- return ('twitterstatusfetcher.generic');
+ return ('twitterstatusfetcher.'.$this->_id);
}
/**
function run()
{
+ if (defined('SCRIPT_DEBUG')) {
+ common_debug($this->name() .
+ ': debugging log output enabled.');
+ }
+
do {
$flinks = $this->refreshFlinks();
foreach ($flinks as $f) {
- // We have to disconnect from the DB before forking so
- // each sub-process will open its own connection and
- // avoid stomping on the others
-
- $conn = &$f->getDatabaseConnection();
- $conn->disconnect();
-
$pid = pcntl_fork();
if ($pid == -1) {
} else {
// Child
+
+ // Each child ps needs its own DB connection
+
+ // Note: DataObject::getDatabaseConnection() creates
+ // a new connection if there isn't one already
+
+ global $_DB_DATAOBJECT;
+ $conn = &$f->getDatabaseConnection();
+
$this->getTimeline($f);
+
+ $conn->disconnect();
+
+ // XXX: Couldn't find a less brutal way to blow
+ // away a cached connection
+
+ unset($_DB_DATAOBJECT['CONNECTIONS']);
+
exit();
}
function refreshFlinks()
{
+ global $_DB_DATAOBJECT;
+
$flink = new Foreign_link();
+ $conn = &$flink->getDatabaseConnection();
- $flink->service = 1; // Twitter
+ $flink->service = TWITTER_SERVICE;
$flink->orderBy('last_noticesync');
$flink->free();
unset($flink);
+ $conn->disconnect();
+ unset($_DB_DATAOBJECT['CONNECTIONS']);
+
return $flinks;
}
function getTimeline($flink)
{
- if (empty($flink)) {
+ if (empty($flink)) {
common_log(LOG_WARNING,
"Can't retrieve Foreign_link for foreign ID $fid");
return;
}
- $fuser = $flink->getForeignUser();
-
- if (empty($fuser)) {
- common_log(LOG_WARNING, "Unmatched user for ID " .
- $flink->user_id);
- return;
- }
-
if (defined('SCRIPT_DEBUG')) {
common_debug('Trying to get timeline for Twitter user ' .
- "$fuser->nickname ($flink->foreign_id).");
+ $flink->foreign_id);
}
// XXX: Biggest remaining issue - How do we know at which status
// to start importing? How many statuses? Right now I'm going
// with the default last 20.
- $url = 'http://twitter.com/statuses/friends_timeline.json';
+ $client = new TwitterOAuthClient($flink->token, $flink->credentials);
- $timeline_json = get_twitter_data($url, $fuser->nickname,
- $flink->credentials);
+ $timeline = null;
- $timeline = json_decode($timeline_json);
+ try {
+ $timeline = $client->statuses_friends_timeline();
+ } catch (OAuthClientCurlException $e) {
+ common_log(LOG_WARNING,
+ 'OAuth client unable to get friends timeline for user ' .
+ $flink->user_id . ' - code: ' .
+ $e->getCode() . 'msg: ' . $e->getMessage());
+ }
if (empty($timeline)) {
common_log(LOG_WARNING, "Empty timeline.");
function saveStatus($status, $flink)
{
$id = $this->ensureProfile($status->user);
+
$profile = Profile::staticGet($id);
- if (!$profile) {
+ if (empty($profile)) {
common_log(LOG_ERR,
'Problem saving notice. No associated Profile.');
return null;
// check to see if we've already imported the status
- if (!$notice) {
+ if (empty($notice)) {
$notice = new Notice();
$notice->rendered = common_render_content($notice->content, $notice);
$notice->source = 'twitter';
$notice->reply_to = null; // XXX lookup reply
- $notice->is_local = NOTICE_GATEWAY;
+ $notice->is_local = Notice::GATEWAY;
if (Event::handle('StartNoticeSave', array(&$notice))) {
$id = $notice->insert();
$profileurl = 'http://twitter.com/' . $user->screen_name;
$profile = Profile::staticGet('profileurl', $profileurl);
- if ($profile) {
+ if (!empty($profile)) {
if (defined('SCRIPT_DEBUG')) {
common_debug("Profile for $profile->nickname found.");
}
// check for remote profile
$remote_pro = Remote_profile::staticGet('uri', $profileurl);
- if (!$remote_pro) {
+ if (empty($remote_pro)) {
$remote_pro = new Remote_profile();
declare(ticks = 1);
-$fetcher = new TwitterStatusFetcher();
+if (have_option('i')) {
+ $id = get_option_value('i');
+} else if (have_option('--id')) {
+ $id = get_option_value('--id');
+} else if (count($args) > 0) {
+ $id = $args[0];
+} else {
+ $id = null;
+}
+
+if (have_option('d') || have_option('debug')) {
+ define('SCRIPT_DEBUG', true);
+}
+
+$fetcher = new TwitterStatusFetcher($id);
$fetcher->runOnce();