function broadcast_oauth($notice, $flink) { $user = $flink->getUser(); $statustxt = format_status($notice); $params = twitter_update_params($notice); $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); $status = null; try { $status = $client->statusesUpdate($statustxt, $params); if (!empty($status)) { Notice_to_status::saveNew($notice->id, twitter_id($status)); } } catch (OAuthClientException $e) { return process_error($e, $flink, $notice); } if (empty($status)) { // This could represent a failure posting, // or the Twitter API might just be behaving flakey. $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . 'trying to post notice %d for User %s (user id %d).', $notice->id, $user->nickname, $user->id); common_log(LOG_WARNING, $errmsg); return false; } // Notice crossed the great divide $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' . 'OAuth for User %s (user id %d).', $notice->id, $user->nickname, $user->id); common_log(LOG_INFO, $msg); return true; }
function linkify($status) { $text = $status->text; if (empty($status->entities)) { $statusId = twitter_id($status); common_log(LOG_WARNING, "No entities data for {$statusId}; trying to fake up links ourselves."); $text = common_replace_urls_callback($text, 'common_linkify'); $text = preg_replace('/(^|\\"\\;|\'|\\(|\\[|\\{|\\s+)#([\\pL\\pN_\\-\\.]{1,64})/e', "'\\1#'.TwitterStatusFetcher::tagLink('\\2')", $text); $text = preg_replace('/(^|\\s+)@([a-z0-9A-Z_]{1,64})/e', "'\\1@'.TwitterStatusFetcher::atLink('\\2')", $text); return $text; } // Move all the entities into order so we can // replace them and escape surrounding plaintext // in order $toReplace = array(); if (!empty($status->entities->urls)) { foreach ($status->entities->urls as $url) { $toReplace[$url->indices[0]] = array(self::URL, $url); } } if (!empty($status->entities->hashtags)) { foreach ($status->entities->hashtags as $hashtag) { $toReplace[$hashtag->indices[0]] = array(self::HASHTAG, $hashtag); } } if (!empty($status->entities->user_mentions)) { foreach ($status->entities->user_mentions as $mention) { $toReplace[$mention->indices[0]] = array(self::MENTION, $mention); } } // sort in forward order by key ksort($toReplace); $result = ''; $cursor = 0; foreach ($toReplace as $part) { list($type, $object) = $part; $start = $object->indices[0]; $end = $object->indices[1]; if ($cursor < $start) { // Copy in the preceding plaintext $result .= $this->twitEscape(mb_substr($text, $cursor, $start - $cursor)); $cursor = $start; } $orig = $this->twitEscape(mb_substr($text, $start, $end - $start)); switch ($type) { case self::URL: $linkText = $this->makeUrlLink($object, $orig); break; case self::HASHTAG: $linkText = $this->makeHashtagLink($object, $orig); break; case self::MENTION: $linkText = $this->makeMentionLink($object, $orig); break; default: $linkText = $orig; continue; } $result .= $linkText; $cursor = $end; } $last = $this->twitEscape(mb_substr($text, $cursor)); $result .= $last; return $result; }
function getTimeline($flink, $timelineUri = 'home_timeline') { if (empty($flink)) { common_log(LOG_ERR, $this->name() . " - Can't retrieve Foreign_link for foreign ID {$fid}"); return; } common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri . ' timeline for Twitter user ' . $flink->foreign_id); $client = null; if (TwitterOAuthClient::isPackedToken($flink->credentials)) { $token = TwitterOAuthClient::unpackToken($flink->credentials); $client = new TwitterOAuthClient($token->key, $token->secret); common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.'); } else { common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " . $flink->foreign_id . " since not OAuth."); } $timeline = null; $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri); common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" . $flink->foreign_id . "' and timeline '" . $timelineUri . "'"); try { $timeline = $client->statusesTimeline($lastId, $timelineUri); } catch (Exception $e) { common_log(LOG_ERR, $this->name() . ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id . ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage()); } if (empty($timeline)) { common_log(LOG_DEBUG, $this->name() . " - Empty '" . $timelineUri . "' timeline."); return; } common_log(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' . ' - for user ' . $flink->user_id); if (!empty($timeline)) { $qm = QueueManager::get(); // Reverse to preserve order foreach (array_reverse($timeline) as $status) { $data = array('status' => $status, 'for_user' => $flink->foreign_id); $qm->enqueue($data, 'tweetin'); } $lastId = twitter_id($timeline[0]); Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId); 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 $flink->last_noticesync = common_sql_now(); $flink->update(); }