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('foursquare', true); $current_owner = $owner_dao->getByEmail(Session::getLoggedInUser()); $instances = $instance_dao->getAllActiveInstancesStalestFirstByNetwork('foursquare'); // Check the client id and secret are set or we can't crawl if (isset($options['foursquare_client_id']->option_value) && isset($options['foursquare_client_secret']->option_value)) { // For each instance of foursquare on this install foreach ($instances as $instance) { if (!$owner_instance_dao->doesOwnerHaveAccessToInstance($current_owner, $instance)) { // Owner doesn't have access to this instance; let's not crawl it. continue; } // Set the user name in the log $logger->setUsername(ucwords($instance->network) . ' | ' . $instance->network_username); // Write to the log that we have started to collect data $logger->logUserSuccess("Starting to collect data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__); // Get the OAuth tokens for this user $tokens = $owner_instance_dao->getOAuthTokens($instance->id); // Set the access token $access_token = $tokens['oauth_access_token']; // Update the last time we crawled $instance_dao->updateLastRun($instance->id); // Create a new crawler $crawler = new FoursquareCrawler($instance, $access_token); // Check the OAuth tokens we have are valid try { $logger->logInfo("About to initializeInstanceUser", __METHOD__ . ',' . __LINE__); $user = $crawler->initializeInstanceUser($access_token, $current_owner->id); if (isset($user) && $user instanceof User) { $logger->logInfo("User initialized", __METHOD__ . ',' . __LINE__); } // Get the data we want and store it in the database $logger->logInfo("About to fetchInstanceUserCheckins", __METHOD__ . ',' . __LINE__); $crawler->fetchInstanceUserCheckins(); } catch (Exception $e) { // Catch any errors that happen when we check the validity of the OAuth tokens $logger->logUserError('EXCEPTION: ' . $e->getMessage(), __METHOD__ . ',' . __LINE__); } $logger->logInfo("About to cache dashboard modules", __METHOD__ . ',' . __LINE__); // Cache the insights to improve the page load speed of the dashboard $dashboard_module_cacher = new DashboardModuleCacher($instance); $dashboard_module_cacher->cacheDashboardModules(); $instance_dao->save($crawler->instance, 0, $logger); Reporter::reportVersion($instance); // Tell the user crawling was sucessful $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 setUpFoursquareInteractions(array $options) { // Get the client ID and secret $client_id = $options['foursquare_client_id']->option_value; $client_secret = $options['foursquare_client_secret']->option_value; // Set up the redirect URL // Get a new configuration instance $config = Config::getInstance(); // Get the root path of our install $site_root_path = $config->getValue('site_root_path'); // If the server supports ssl add an s to our URL path $ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != '' ? 's' : ''; // Generate the redirect URL $redirect_uri = urlencode('http' . $ssl . '://' . $_SERVER['SERVER_NAME'] . $site_root_path . 'account/?p=foursquare'); // Create the OAuth link based on foursquares instructions here: https://developer.foursquare.com/overview/auth $oauth_link = "https://foursquare.com/oauth2/authenticate?client_id=" . $client_id . "&response_type=code"; $oauth_link .= "&redirect_uri=" . $redirect_uri; // Add the link for the user to click to the page $this->addToView('oauth_link', $oauth_link); // If we are here because they have been redirect back by foursquare with a OAuth token if (isset($_GET['code'])) { // Get the code foursquare provided from the URL $code = $_GET['code']; // Create a new crawler, as this class contains the method for retriving our tokens $crawler = new FoursquareCrawler(null, null); // Get the OAuth Tokens $tokens = $crawler->getOAuthTokens($client_id, $client_secret, $redirect_uri, $code); // If foursquare return an error if (isset($tokens->error)) { // Tell the user something went wrong $this->addErrorMessage("Oops! Something went wrong while obtaining OAuth tokens. foursquare says \"" . $tokens->error . ".\" Please double-check your settings and try again.", 'authorization'); } else { // If we got some OAuth tokens back, check they are valid $foursquare_api_accessor = new FoursquareAPIAccessor(); // Make a query for the users details on foursquare $foursquare_user = $foursquare_api_accessor->apiRequest('users/self', $tokens->access_token); // If foursquare returned an error after that request if (isset($foursquare_user->error) || !isset($foursquare_user->response->user->id) || !isset($foursquare_user->response->user->contact->email)) { $this->addErrorMessage("Oops! Something went wrong querying the foursquare API.\n foursquare says \"" . Utils::varDumpToString($foursquare_user) . ".\" Please double-check your settings and try again.", 'authorization'); } else { // Everything went fine so store the details in the database // Set the user ID and username based on details returned by foursquare $foursquare_user_id = $foursquare_user->response->user->id; $foursquare_username = $foursquare_user->response->user->contact->email; // Save the tokens in the database $this->saveAccessTokens($foursquare_user_id, $foursquare_username, $tokens->access_token); } } } // Create a new instance DAO $instance_dao = DAOFactory::getDAO('InstanceDAO'); // Get the owner of this instance $owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'foursquare'); // Add all owners of foursquare instances to the view $this->addToView('owner_instances', $owner_instances); }
public function testFetchInstanceUserCheckinsFullArchive() { $builders = self::buildData(); // Tell ThinkUp we haven't loaded old posts yet $this->profile1_instance->is_archive_loaded_posts = false; // Create a new foursquare crawler for this instance with a valid access token $fsc = new FoursquareCrawler($this->profile1_instance, 'secret', 10); // Make a request for this users checkins $fsc->fetchInstanceUserCheckins(); // Get a new post dao $post_dao = new PostMySQLDAO(); // Get a new place DAO $place_dao = new PlaceMySQLDAO(); // Get a new link DAO $link_dao = new LinkMySQLDAO(); // Get the first checkin from the database that the fetchInstanceUserCheckins method should have saved $post = $post_dao->getPost('4efa01068b81ef98d2e9cd0b', 'foursquare', true); // Get the first place information from the database that fetchInstanceUserCheckins method should have saved $place = $place_dao->getPlaceByID('4e22eac31838712abe8186e3'); // Check the post was actually set $this->assertIsA($post, 'Post'); // Check these values were set as blank, as they can't be null but we dont use them $this->assertEqual($post->reply_count_cache, 1); $this->assertEqual($post->favlike_count_cache, 0); $this->assertEqual($post->retweet_count_cache, 0); $this->assertEqual($post->author_follower_count, 0); // Check the source was set $this->assertEqual($post->source, 'foursquare for iPhone'); // Check the username was set $this->assertEqual($post->author_username, '*****@*****.**'); // Check the fullname was set $this->assertEqual($post->author_fullname, 'Bob Cats'); // Check the avatar was set $this->assertEqual($post->author_avatar, 'https://foursquare.com/img/100x100/blank_boy.png'); // Check the author used id was set $this->assertEqual($post->author_user_id, '113612142759476883204'); // Check the publication date was set //$this->assertEqual($post->pub_date, '2011-12-27 17:31:50'); $this->assertEqual($post->pub_date, date('Y-m-d H:i:s', '1325007110')); // Check the checkin was set as public $this->assertFalse($post->is_protected); // Check the place name was set $this->assertEqual($post->place, 'Bedworth Sloughs'); // Check the location was set $this->assertEqual($post->location, 'Bedworth, CV12 0AS'); // Check the place ID was set $this->assertEqual($post->place_id, '4e22eac31838712abe8186e3'); // Check the geo co ordinates were set //$this->assertEqual($post->geo, '52.477241961421,-1.4845029364055'); $this->assertPattern('/52.4772419614/', $post->geo); $this->assertPattern('/-1.4845029364/', $post->geo); // Now check the details about the place were stored in the places table // Check all the fields were returned $this->assertEqual(sizeof($place), 12); // Check the place type was set $this->assertEqual($place['place_type'], 'Field'); // Check the place name was set $this->assertEqual($place['name'], 'Bedworth Sloughs'); // Check the fullname was set $this->assertEqual($place['full_name'], 'Bedworth Sloughs'); // Check the network was set $this->assertEqual($place['network'], 'foursquare'); // Build the map url to check against like this to meet code style guidelines $map_url = "http://maps.googleapis.com/maps/api/staticmap?size=150x150&zoom=15&maptype=roadmap&markers"; $map_url .= "=color:blue%7C" . $post->geo . "&sensor=false"; // Check the map image was set $this->assertEqual($place['map_image'], $map_url); // Check the lat long co ordinates were set $this->assertPattern('/POINT\\(52.4772419614/', $place['longlat']); $this->assertPattern('/ -1.4845029364/', $place['longlat']); // Check the comment for this post was set // Get the comment from the database $comment = $post_dao->getPost('4f4135f9e4b028f640ef42eb', 'foursquare', true); // Check the post was actually set $this->assertIsA($comment, 'Post'); // Check these values were set as blank, as they can't be null but we dont use them $this->assertEqual($comment->reply_count_cache, 0); $this->assertEqual($comment->favlike_count_cache, 0); $this->assertEqual($comment->retweet_count_cache, 0); $this->assertEqual($comment->author_follower_count, 0); $this->assertEqual($comment->source, ''); // Check the username was set $this->assertEqual($comment->author_username, '*****@*****.**'); // Check the fullname was set $this->assertEqual($comment->author_fullname, 'Bob Cats'); // Check the avatar was set $this->assertEqual($comment->author_avatar, 'https://foursquare.com/img/100x100/blank_boy.png'); // Check the author used id was set $this->assertEqual($comment->author_user_id, '113612142759476883204'); // Check the publication date was set //$this->assertEqual($comment->pub_date, '2012-02-19 17:48:41'); $this->assertEqual($comment->pub_date, date('Y-m-d H:i:s', '1329673721')); // Check the checkin was set as public $this->assertFalse($comment->is_protected); // Check the place name was set $this->assertEqual($comment->place, 'Bedworth Sloughs'); // Check the location was set $this->assertEqual($comment->location, 'Bedworth, CV12 0AS'); // Check the place ID was set $this->assertEqual($comment->place_id, '4e22eac31838712abe8186e3'); // Check the geo co ordinates were set //$this->assertEqual($comment->geo, '52.477241961421,-1.4845029364055'); $this->assertPattern('/52.4772419614/', $comment->geo); $this->assertPattern('/-1.484502936/', $comment->geo); // Check the link for this checkin was set // Link string $link_string = 'https://img-s.foursquare.com/pix/noTc5a0afTgqTuLlsi3a33tLR0iUOZaa2hLm7LsNn1Q.jpg'; $link = $link_dao->getLinkByUrl($link_string); // Check the expanded url was set correctly // Expanded URL string $expanded_url = 'https://img-s.foursquare.com/pix/noTc5a0afTgqTuLlsi3a33tLR0iUOZaa2hLm7LsNn1Q.jpg'; $this->assertEqual($link->expanded_url, $expanded_url); // Check the title was set $this->assertEqual($link->title, ' '); // Check the description was set $this->assertEqual($link->description, ' '); // Check the img_src was set // img_src string $img_src = 'https://img-s.foursquare.com/pix/noTc5a0afTgqTuLlsi3a33tLR0iUOZaa2hLm7LsNn1Q.jpg'; $this->assertEqual($link->image_src, $img_src); // Check the caption was set $this->assertEqual($link->caption, ' '); // Check the post key is correct $this->assertEqual($link->post_key, 1); // Check the checkin from the second page of results was inserted correctly // Get the first checkin from the database that the fetchInstanceUserCheckins method should have saved $post = $post_dao->getPost('4efa01068b81ef98d679cd0b', 'foursquare', true); // Get the first place information from the database that fetchInstanceUserCheckins method should have saved $place = $place_dao->getPlaceByID('4e22eac31838712aui8186e3'); // Check the post was actually set $this->assertIsA($post, 'Post'); // Check these values were set as blank, as they can't be null but we dont use them $this->assertEqual($post->reply_count_cache, 0); $this->assertEqual($post->favlike_count_cache, 0); $this->assertEqual($post->retweet_count_cache, 0); $this->assertEqual($post->author_follower_count, 0); // Check the source was set $this->assertEqual($post->source, 'foursquare for iPhone'); // Check the username was set $this->assertEqual($post->author_username, '*****@*****.**'); // Check the fullname was set $this->assertEqual($post->author_fullname, 'Bob Cats'); // Check the avatar was set $this->assertEqual($post->author_avatar, 'https://foursquare.com/img/100x100/blank_boy.png'); // Check the author used id was set $this->assertEqual($post->author_user_id, '113612142759476883204'); // Check the publication date was set //$this->assertEqual($post->pub_date, '2011-12-27 17:31:59'); $this->assertEqual($post->pub_date, date('Y-m-d H:i:s', '1325007119')); // Check the checkin was set as public $this->assertFalse($post->is_protected); // Check the place name was set $this->assertEqual($post->place, 'Empire State Building'); // Check the location was set $this->assertEqual($post->location, 'New York'); // Check the place ID was set $this->assertEqual($post->place_id, '4e22eac31838712aui8186e3'); // Check the geo co ordinates were set //$this->assertEqual($post->geo, '82.476241961421,-6.4845029364055'); $this->assertPattern('/82.4762419614/', $post->geo); $this->assertPattern('/-6.4845029364/', $post->geo); // Now check the details about the place were stored in the places table // Check all the fields were returned $this->assertEqual(sizeof($place), 12); // Check the place type was set $this->assertEqual($place['place_type'], 'Building'); // Check the place name was set $this->assertEqual($place['name'], 'Empire State Building'); // Check the fullname was set $this->assertEqual($place['full_name'], 'Empire State Building'); // Check the network was set $this->assertEqual($place['network'], 'foursquare'); // Check we set the old posts loaded bit $this->assertTrue($this->profile1_instance->is_archive_loaded_posts); }