public function crawl() { $logger = Logger::getInstance(); $config = Config::getInstance(); $instance_dao = DAOFactory::getDAO('InstanceDAO'); $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO'); $owner_dao = DAOFactory::getDAO('OwnerDAO'); $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO'); $options = $plugin_option_dao->getOptionsHash('googleplus', true); //get cached $current_owner = $owner_dao->getByEmail(Session::getLoggedInUser()); //crawl Google+ users $instances = $instance_dao->getActiveInstancesStalestFirstForOwnerByNetworkNoAuthError($current_owner, 'google+'); if (isset($options['google_plus_client_id']->option_value) && isset($options['google_plus_client_secret']->option_value)) { foreach ($instances as $instance) { $logger->setUsername(ucwords($instance->network) . ' | ' . $instance->network_username); $logger->logUserSuccess("Starting to collect data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__); $tokens = $owner_instance_dao->getOAuthTokens($instance->id); $access_token = $tokens['oauth_access_token']; $refresh_token = $tokens['oauth_access_token_secret']; $instance_dao->updateLastRun($instance->id); $google_plus_crawler = new GooglePlusCrawler($instance, $access_token); $dashboard_module_cacher = new DashboardModuleCacher($instance); try { $google_plus_crawler->initializeInstanceUser($options['google_plus_client_id']->option_value, $options['google_plus_client_secret']->option_value, $access_token, $refresh_token, $current_owner->id); $google_plus_crawler->fetchInstanceUserPosts(); } catch (Exception $e) { $logger->logUserError('EXCEPTION: ' . $e->getMessage(), __METHOD__ . ',' . __LINE__); } $dashboard_module_cacher->cacheDashboardModules(); $instance_dao->save($google_plus_crawler->instance, 0, $logger); $logger->logUserSuccess("Finished collecting data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__); } } }
/** * Add user auth link or process incoming auth requests. * @param array $options Plugin options array */ protected function setUpGPlusInteractions(array $options) { //get options $client_id = $options['google_plus_client_id']->option_value; $client_secret = $options['google_plus_client_secret']->option_value; //prep redirect URI $config = Config::getInstance(); $site_root_path = $config->getValue('site_root_path'); $redirect_uri = urlencode(Utils::getApplicationURL() . 'account/?p=google%2B'); //create OAuth link $oauth_link = "https://accounts.google.com/o/oauth2/auth?client_id=" . $client_id . "&redirect_uri=" . $redirect_uri . "&scope=https://www.googleapis.com/auth/plus.me&response_type=code&access_type=offline&approval_prompt=force"; $this->addToView('oauth_link', $oauth_link); // Google provided a code to get an access token if (isset($_GET['code'])) { $code = $_GET['code']; $crawler_plugin_registrar = new GooglePlusCrawler(null, null); $tokens = $crawler_plugin_registrar->getOAuthTokens($client_id, $client_secret, $code, 'authorization_code', $redirect_uri); if (isset($tokens->error)) { $this->addErrorMessage("Oops! Something went wrong while obtaining OAuth tokens.<br>Google says \"" . $tokens->error . ".\" Please double-check your settings and try again.", 'authorization'); } else { if (isset($tokens->access_token) && isset($tokens->access_token)) { //Get user data $gplus_api_accessor = new GooglePlusAPIAccessor(); $gplus_user = $gplus_api_accessor->apiRequest('people/me', $tokens->access_token, null); if (isset($gplus_user->error)) { if ($gplus_user->error->code == "403" && $gplus_user->error->message == 'Access Not Configured') { $this->addErrorMessage("Oops! Looks like Google+ API access isn't turned on. " . "<a href=\"http://code.google.com/apis/console#access\">In the Google APIs console</a>, " . "in Services, flip the Google+ API Status switch to 'On' and try again.", 'authorization'); } else { $this->addErrorMessage("Oops! Something went wrong querying the Google+ API.<br>" . "Google says \"" . $gplus_user->error->code . ": " . $gplus_user->error->message . ".\" Please double-check your settings and try again.", 'authorization'); } } else { if (isset($gplus_user->id) && isset($gplus_user->displayName)) { $gplus_user_id = $gplus_user->id; $gplus_username = $gplus_user->displayName; //Process tokens $this->saveAccessTokens($gplus_user_id, $gplus_username, $tokens->access_token, $tokens->refresh_token); } else { $this->addErrorMessage("Oops! Something went wrong querying the Google+ API.<br>" . "Google says \"" . Utils::varDumpToString($gplus_user) . ".\" Please double-check your settings and try again.", 'authorization'); } } } else { $this->addErrorMessage("Oops! Something went wrong while obtaining OAuth tokens.<br>Google says \"" . Utils::varDumpToString($tokens) . ".\" Please double-check your settings and try again.", 'authorization'); } } } $instance_dao = DAOFactory::getDAO('InstanceDAO'); $owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'google+'); $this->addToView('owner_instances', $owner_instances); }
public function testFetchInstanceUserPosts() { $builders = self::buildData(); $gpc = new GooglePlusCrawler($this->profile1_instance, 'fauxaccesstoken', 10); $gpc->fetchInstanceUserPosts(); $post_dao = new PostMySQLDAO(); $post = $post_dao->getPost('z12is5v4snurihgdl22iiz3pjrnws3lle', 'google+', true); $this->assertIsA($post, 'Post'); $this->assertEqual($post->post_text, 'I've got a date with the G+ API this weekend to make a ThinkUp plugin!'); $this->assertEqual($post->reply_count_cache, 24); $this->assertEqual($post->favlike_count_cache, 159); $this->assertEqual($post->retweet_count_cache, 29); $this->assertIsA($post->links[0], 'Link'); $this->assertEqual($post->links[0]->url, 'http://googleplusplatform.blogspot.com/2011/09/getting-started-on-google-api.html'); $this->assertEqual($post->links[0]->title, 'Getting Started on the Google+ API - Google+ Platform Blog'); $this->assertEqual($post->links[0]->description, 'Official source of information about the Google+ platform'); $this->assertEqual($post->links[0]->image_src, ''); //test reshare with annotation $post = $post_dao->getPost('z12pcfdr2wvyzjfff22iiz3pjrnws3lle', 'google+', true); $this->assertIsA($post, 'Post'); $this->assertEqual($post->post_text, 'Really fun episode this week.'); //test reshare without annotation $post = $post_dao->getPost('z12pxlfjxpujivy3e230t3aqawfoz1qf1', 'google+', true); $this->assertIsA($post, 'Post'); $this->assertEqual($post->post_text, ''); //now crawl on updated data and assert counts and post text get updated in database $gpc->api_accessor->setDataLocation('new_counts/'); $gpc->fetchInstanceUserPosts(); $post = $post_dao->getPost('z12is5v4snurihgdl22iiz3pjrnws3lle', 'google+', true); $this->assertEqual($post->reply_count_cache, 64); $this->assertEqual($post->favlike_count_cache, 199); $this->assertEqual($post->retweet_count_cache, 69); $this->assertEqual($post->post_text, "I've got a date with the G+ API this weekend to make a ThinkUp plugin! Updated: New text here!"); }