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);
             // Tell the user crawling was sucessful
             $logger->logUserSuccess("Finished collecting data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__);
         }
     }
 }
示例#2
0
 public function testInitializeInstanceUser()
 {
     // Create a new foursquare crawler for this instance with the access token fauxaccesstoken
     $fsc = new FoursquareCrawler($this->profile1_instance, 'secret', 10);
     // Call the initaliseInstanceUser method with this users id and access token
     $fsc->initializeInstanceUser('secret', 1);
     // Get a new user DAO
     $user_dao = new UserMySQLDAO();
     // Get the foursquare users object
     $user = $user_dao->getUserByName('*****@*****.**', 'foursquare');
     // Check that we got something back from the database
     $this->assertTrue(isset($user));
     // Check the username is correct
     $this->assertEqual($user->username, '*****@*****.**');
     // Check the full name is correct
     $this->assertEqual($user->full_name, 'Bob Cats');
     // Check the user ID is correct
     $this->assertEqual($user->user_id, '113612142759476883204');
     // Check the location is correct
     $this->assertEqual($user->location, "Harefield, UK");
     // Check the URL was set correctly
     $this->assertEqual($user->url, 'http://www.foursquare.com/user/113612142759476883204');
     // Check the user isn't protected
     $this->assertFalse($user->is_protected);
 }