public function updateData($import = false) { // Get service propertie $config = Zend_Registry::get("configuration"); $pages = $import ? 50 : 1; $count = $import ? 200 : 50; // Get application properties $app_properties = new Properties(array(Stuffpress_Db_Properties::KEY => Zend_Registry::get("shard"))); // Get twitter user properties $username = $this->getProperty('username'); $uid = $this->getProperty('uid', 0); if (!$username) { throw new Stuffpress_Exception("Update failed, connector not properly configured"); } // Get twitter consumer tokens and user secrets $consumer_key = $config->twitter->consumer_key; $consumer_secret = $config->twitter->consumer_secret; $oauth_token = $app_properties->getProperty('twitter_oauth_token'); $oauth_token_secret = $app_properties->getProperty('twitter_oauth_token_secret'); if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) { throw new Stuffpress_Exception("Missing twitter credentials. Please configure your twitter account in the <a href='/admin/sns/'>Configure -> Social Networks</a> section."); } // Fetch the data from twitter $result = array(); $connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); $connection->host = "https://api.twitter.com/1.1/"; $max_id = false; $params = array('screen_name' => $username, 'count' => $count); for ($page = 1; $page <= $pages; $page++) { if ($max_id) { $params['max_id'] = $max_id; } $response = $connection->get('statuses/user_timeline', $params); if ($response && isset($response->errors) && count($response->errors) > 0) { throw new Stuffpress_Exception($response->errors[0]->message); } if (count($response) == 0) { break; } $max_id = $response[count($response) - 1]->id_str; $items = $this->processItems($response); if (count($items) == 0) { break; } $result = array_merge($result, $items); } // Mark as updated (could have been with errors) $this->markUpdated(); return $result; }
protected function updateTwitter($items) { // Get the user $users = new Users(); $shortUrl = new ShortUrl(); $user = $users->getUser($this->getUserID()); // Get twitter consumer tokens and user secrets $config = Zend_Registry::get("configuration"); $consumer_key = $config->twitter->consumer_key; $consumer_secret = $config->twitter->consumer_secret; // Get twitter credentials $properties = new Properties(array(Properties::KEY => $user->id)); $auth = $properties->getProperty('twitter_auth'); $services = $properties->getProperty('twitter_services'); $oauth_token = $properties->getProperty('twitter_oauth_token'); $oauth_token_secret = $properties->getProperty('twitter_oauth_token_secret'); if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) { return; } $has_preamble = $properties->getProperty('preamble', true); // Return if not all conditions are met if (!$auth || !in_array($this->getID(), unserialize($services))) { return; } // Get an item $count = count($items); $data = new Data(); $source_id = $this->_source['id']; if ($count <= 3) { foreach($items as $id) { $item = $data->getItem($source_id, $id); $title = strip_tags($item->getTitle()); $service = $this->getServiceName(); if (($item->getType() == SourceItem::STATUS_TYPE ) && strlen($title) < 140) { $tweet = $title; } else { $preamble = $has_preamble ? $item->getPreamble() : ""; $tweet = $preamble . $title; $url = $users->getUrl($user->id, "s/" . $shortUrl->shorten($item->getSlug())); if (strlen($tweet) + strlen($url) > 140) { $tweet = substr($tweet, 0, 140 - strlen($url) - 5) . "... $url"; } else { $tweet = "$tweet $url"; } } try { $connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); $response = $connection->post('statuses/update', array('status' => $tweet)); } catch (Exception $e) {} } } else { $url = $users->getUrl($user->id); $tweet = sprintf($this->_update_tweet, $count, $url); try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) {} } }
private function notifyTwitter($item) { // Get twitter consumer tokens and user secrets $consumer_key = $this->_config->twitter->consumer_key; $consumer_secret = $this->_config->twitter->consumer_secret; $oauth_token = $this->_properties->getProperty('twitter_oauth_token'); $oauth_token_secret = $this->_properties->getProperty('twitter_oauth_token_secret'); if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) { $this->addErrorMessage("Missing twitter OAuth credentials to continue"); } // Should we append a text before the tweet ? $has_preamble = $this->_properties->getProperty('preamble', true); // Get item $preamble = $has_preamble ? $item->getPreamble() : ""; $title = $preamble . $item->getTitle(); // Assemble tweet depending on type if (($item->getType() == SourceItem::STATUS_TYPE ) && strlen($title) < 140) { $tweet = $title; } else { $users = new Users(); $shortUrl = new ShortUrl(); $url = $users->getUrl($this->_application->user->id, "s/" . $shortUrl->shorten($item->getSlug())); if (strlen($title) + strlen($url) > 140) { $tweet = substr($title, 0, 140 - strlen($url) - 5) . "... $url"; } else { $tweet = "$title $url"; } } try { $connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); $response = $connection->post('statuses/update', array('status' => $tweet)); if (isset($response->error)) { $this->addErrorMessage("Failed posting to Twitter with error " . $response->error); } } catch (Exception $e) { $this->addErrorMessage("Failed posting to Twitter with unknwon error"); } }
public function callbackAction() { /* Get the saved tokens */ $oauth_token = $this->_properties->getProperty('twitter_oauth_token'); $oauth_token_secret = $this->_properties->getProperty('twitter_oauth_token_secret'); if (!isset($oauth_token) && !isset($oauth_token_secret)) { $this->addErrorMessage("Missing temporary OAuth tokens"); $this->_forward('index'); return; } /* Get the consumer key and secret from the config */ if (! isset($this->_config->twitter->consumer_key) && !isset($this->_config->twitter->consumer_secret)) { $this->addErrorMessage("Missing OAuth consumer key and secret"); $this->_forward('index'); return; } $consumer_key = $this->_config->twitter->consumer_key; $consumer_secret = $this->_config->twitter->consumer_secret; $oauth_callback = $this->getStaticUrl() . "/admin/sns/callback"; /* If the oauth_token is old redirect to the connect page. */ if (isset($_REQUEST['oauth_token'])) { if ($oauth_token != $_REQUEST['oauth_token']) { $this->_properties->deleteProperty("twitter_auth"); die("Session should be cleared"); } } /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */ $connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); /* Request access tokens from twitter */ $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); /* Save the access tokens. Normally these would be saved in a database for future use. */ $this->_properties->setProperty('twitter_oauth_token', $access_token['oauth_token']); $this->_properties->setProperty('twitter_oauth_token_secret', $access_token['oauth_token_secret']); $this->_properties->setProperty('twitter_user_id', $access_token['user_id']); $this->_properties->setProperty('twitter_username', $access_token['screen_name']); /* If HTTP response is 200 continue otherwise send to connect page to retry */ if (200 == $connection->http_code) { /* The user has been verified and the access tokens can be saved for future use */ $this->_properties->setProperty('twitter_auth', true); } else { /* Save HTTP status for error dialog on connnect page.*/ die("Error, We should clear the session."); } $this->_forward('index'); }