Add Twitter mention import support

This commit is contained in:
Jean Baptiste Favre 2013-02-18 23:16:06 +01:00
parent 9361c07bb3
commit dd97468c72
2 changed files with 41 additions and 27 deletions

View File

@ -136,7 +136,8 @@ class TwitterStatusFetcher extends ParallelizingDaemon
// a new connection if there isn't one already // a new connection if there isn't one already
$conn = &$flink->getDatabaseConnection(); $conn = &$flink->getDatabaseConnection();
$this->getTimeline($flink); $this->getTimeline($flink, 'home_timeline');
$this->getTimeline($flink, 'mentions');
$flink->last_friendsync = common_sql_now(); $flink->last_friendsync = common_sql_now();
$flink->update(); $flink->update();
@ -149,49 +150,58 @@ class TwitterStatusFetcher extends ParallelizingDaemon
unset($_DB_DATAOBJECT['CONNECTIONS']); unset($_DB_DATAOBJECT['CONNECTIONS']);
} }
function getTimeline($flink) function getTimeline($flink, $timelineUri = 'home_timeline')
{ {
if (empty($flink)) { if (empty($flink)) {
common_log(LOG_WARNING, $this->name() . common_log(LOG_ERR, $this->name() .
" - Can't retrieve Foreign_link for foreign ID $fid"); " - Can't retrieve Foreign_link for foreign ID $fid");
return; return;
} }
common_debug($this->name() . ' - Trying to get timeline for Twitter user ' . common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
$flink->foreign_id); ' timeline for Twitter user ' . $flink->foreign_id);
$client = null; $client = null;
if (TwitterOAuthClient::isPackedToken($flink->credentials)) { if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
$token = TwitterOAuthClient::unpackToken($flink->credentials); $token = TwitterOAuthClient::unpackToken($flink->credentials);
$client = new TwitterOAuthClient($token->key, $token->secret); $client = new TwitterOAuthClient($token->key, $token->secret);
common_debug($this->name() . ' - Grabbing friends timeline with OAuth.'); common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
} else { } else {
common_debug("Skipping friends timeline for $flink->foreign_id since not OAuth."); common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
$flink->foreign_id . " since not OAuth.");
} }
$timeline = null; $timeline = null;
$lastId = Twitter_synch_status::getLastId($flink->foreign_id, 'home_timeline'); $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
common_debug("Got lastId value '{$lastId}' for foreign id '{$flink->foreign_id}' and timeline 'home_timeline'"); common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
$flink->foreign_id . "' and timeline '" . $timelineUri. "'");
try { try {
$timeline = $client->statusesHomeTimeline($lastId); $timeline = $client->statusesTimeline($lastId, $timelineUri);
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_WARNING, $this->name() . common_log(LOG_ERR, $this->name() .
' - Twitter client unable to get friends timeline for user ' . ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
$flink->user_id . ' - code: ' . ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
$e->getCode() . 'msg: ' . $e->getMessage());
} }
if (empty($timeline)) { if (empty($timeline)) {
common_log(LOG_WARNING, $this->name() . " - Empty timeline."); common_log(LOG_WARNING, $this->name() . " - Empty '" . $timelineUri . "' timeline.");
return; return;
} }
common_debug(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from Twitter.'); common_log(LOG_INFO, $this->name() .
' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
' - for user ' . $flink->user_id);
/* @fixme make TwitterBridge use queueing system
* to speed up timeline processing
* as well as giving oportunity to use
* usefull feature such as realtime
* brodcast to clients
*/
$importer = new TwitterImport(); $importer = new TwitterImport();
// Reverse to preserve order // Reverse to preserve order
@ -203,11 +213,13 @@ class TwitterStatusFetcher extends ParallelizingDaemon
Inbox::insertNotice($flink->user_id, $notice->id); Inbox::insertNotice($flink->user_id, $notice->id);
} }
} }
/* @fixme ends here */
if (!empty($timeline)) { if (!empty($timeline)) {
$lastId = twitter_id($timeline[0]); $lastId = twitter_id($timeline[0]);
Twitter_synch_status::setLastId($flink->foreign_id, 'home_timeline', $lastId); Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline 'home_timeline'"); common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
$timelineUri . "'");
} }
// Okay, record the time we synced with Twitter for posterity // Okay, record the time we synced with Twitter for posterity
@ -233,5 +245,5 @@ if (have_option('d') || have_option('debug')) {
$debug = true; $debug = true;
} }
$fetcher = new TwitterStatusFetcher($id, 60, 2, $debug); $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);
$fetcher->runOnce(); $fetcher->runOnce();

View File

@ -190,19 +190,21 @@ class TwitterOAuthClient extends OAuthClient
/** /**
* Calls Twitter's /statuses/home_timeline API method * Calls Twitter's /statuses/home_timeline API method
* *
* @param int $since_id show statuses after this id * @param int $since_id show statuses after this id
* @param int $max_id show statuses before this id * @param string $timelineUri timeline to poll statuses from
* @param int $cnt number of statuses to show * @param int $max_id show statuses before this id
* @param int $page page number * @param int $cnt number of statuses to show
* @param int $page page number
* *
* @return mixed an array of statuses * @return mixed an array of statuses
*/ */
function statusesHomeTimeline($since_id = null, $max_id = null, function statusesTimeline($since_id = null, $timelineUri = 'home_timeline',
$cnt = null, $page = null) $max_id = null, $cnt = 200, $page = null)
{ {
$url = 'https://api.twitter.com/1/statuses/home_timeline.json'; $url = 'https://api.twitter.com/1/statuses/'.$timelineUri.'.json';
$params = array('include_entities' => 'true'); $params = array('include_entities' => 'true',
'include_rts' => 'true');
if (!empty($since_id)) { if (!empty($since_id)) {
$params['since_id'] = $since_id; $params['since_id'] = $since_id;