Esempio n. 1
0
 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('instagram', true);
     //get cached
     $max_crawl_time = isset($options['max_crawl_time']) ? $options['max_crawl_time']->option_value : 20;
     //convert to seconds
     $max_crawl_time = $max_crawl_time * 60;
     $current_owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     //crawl instagram user profiles and pages
     $instances = $instance_dao->getActiveInstancesStalestFirstForOwnerByNetworkNoAuthError($current_owner, 'instagram');
     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'];
         $instance_dao->updateLastRun($instance->id);
         $instagram_crawler = new InstagramCrawler($instance, $access_token, $max_crawl_time);
         $dashboard_module_cacher = new DashboardModuleCacher($instance);
         try {
             $instagram_crawler->fetchPostsAndReplies();
         } catch (Instagram\Core\ApiAuthException $e) {
             //The access token is invalid, save in owner_instances table
             $owner_instance_dao->setAuthError($current_owner->id, $instance->id, $e->getMessage());
             //Send email alert
             $this->sendInvalidOAuthEmailAlert($current_owner->email, $instance->network_username);
             $logger->logUserError(get_class($e) . ' ' . $e->getMessage(), __METHOD__ . ',' . __LINE__);
         } catch (Exception $e) {
             $logger->logUserError(get_class($e) . ' ' . $e->getMessage(), __METHOD__ . ',' . __LINE__);
         }
         $dashboard_module_cacher->cacheDashboardModules();
         $instance_dao->save($instagram_crawler->instance, 0, $logger);
         Reporter::reportVersion($instance);
         $logger->logUserSuccess("Finished collecting data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__);
     }
 }
 public function crawl()
 {
     $logger = Logger::getInstance();
     $config = Config::getInstance();
     $instance_dao = DAOFactory::getDAO('InstagramInstanceDAO');
     $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash('instagram', true);
     //get cached
     $max_api_calls = isset($options['max_api_calls']) ? $options['max_api_calls']->option_value : 2500;
     $current_owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     //crawl instagram user profiles and pages
     $instances = $instance_dao->getActiveInstancesStalestFirstForOwnerByNetworkNoAuthError($current_owner, 'instagram');
     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'];
         $instance_dao->updateLastRun($instance->id);
         $dashboard_module_cacher = new DashboardModuleCacher($instance);
         try {
             /**
              * 1. Fetch user info, media + its likes and comments.
              * 2. Fetch user's likes.
              * 3. Fetch user's friends, and update stale relationships.
              * 4. Fetch user's followers, and update stale relationships.
              * 5. Update stale friends' profiles.
              */
             $instagram_crawler = new InstagramCrawler($instance, $access_token, $max_api_calls);
             $instagram_crawler->fetchPostsAndReplies();
             $instagram_crawler->fetchLikes();
             $instagram_crawler->fetchFriends();
             $instagram_crawler->fetchFollowers();
             $instagram_crawler->updateStaleFriendsProfiles();
         } catch (Instagram\Core\ApiAuthException $e) {
             //The access token is invalid, save in owner_instances table
             $owner_instance_dao->setAuthErrorByTokens($instance->id, $access_token, '', $e->getMessage());
             //Send email alert
             //Get owner by auth tokens first, then send to that person
             $owner_email_to_notify = $owner_instance_dao->getOwnerEmailByInstanceTokens($instance->id, $access_token, '');
             $email_attempt = $this->sendInvalidOAuthEmailAlert($owner_email_to_notify, $instance->network_username);
             if ($email_attempt) {
                 $logger->logUserInfo('Sent reauth email to ' . $owner_email_to_notify, __METHOD__ . ',' . __LINE__);
             } else {
                 $logger->logInfo('Didn\'t send reauth email to ' . $owner_email_to_notify, __METHOD__ . ',' . __LINE__);
             }
             $logger->logUserError(get_class($e) . ' ' . $e->getMessage(), __METHOD__ . ',' . __LINE__);
         } catch (Exception $e) {
             $logger->logUserError(get_class($e) . ' ' . $e->getMessage(), __METHOD__ . ',' . __LINE__);
         }
         $dashboard_module_cacher->cacheDashboardModules();
         $instance_dao->save($instagram_crawler->instance, 0, $logger);
         Reporter::reportVersion($instance);
         $logger->logUserSuccess("Finished collecting data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__);
     }
 }
 public function testUpdateStaleFollows()
 {
     //Insert stale follows for 502993749
     $builders[] = FixtureBuilder::build('follows', array('user_id' => '502993749', 'first_seen' => '-5d', 'follower_id' => '123-both-follow', 'network' => 'instagram', 'last_seen' => '-5d', 'active' => 1));
     $builders[] = FixtureBuilder::build('follows', array('user_id' => '123-both-follow', 'first_seen' => '-5d', 'follower_id' => '502993749', 'network' => 'instagram', 'last_seen' => '-5d', 'active' => 1));
     $builders[] = FixtureBuilder::build('follows', array('user_id' => '502993749', 'first_seen' => '-5d', 'follower_id' => '123-source-follows', 'network' => 'instagram', 'last_seen' => '-5d', 'active' => 1));
     $builders[] = FixtureBuilder::build('follows', array('user_id' => '123-target-follows', 'first_seen' => '-5d', 'follower_id' => '502993749', 'network' => 'instagram', 'last_seen' => '-5d', 'active' => 1));
     //Run the crawl
     $ic = new InstagramCrawler($this->profile3_instance, 'fauxaccesstoken', 120);
     $ic->updateStaleFollows();
     $follow_dao = new FollowMySQLDAO();
     //Assert source follows but target doesn't
     //Target follows source - false
     $result = $follow_dao->followExists($user_id = '502993749', $follower_id = '123-source-follows', 'instagram', $is_active = true);
     $this->assertFalse($result);
     //Source follows target - true
     $result = $follow_dao->followExists($user_id = '123-source-follows', $follower_id = '502993749', 'instagram', $is_active = true);
     $this->assertTrue($result);
     //Assert target follows but source doesn't
     //Target follows source - true
     $result = $follow_dao->followExists($user_id = '502993749', $follower_id = '123-target-follows', 'instagram', $is_active = true);
     $this->assertTrue($result);
     //Source follows target - false
     $result = $follow_dao->followExists($user_id = '123-target-follows', $follower_id = '502993749', 'instagram', $is_active = true);
     $this->assertFalse($result);
     //Assert both follow
     //Target follows source - true
     $result = $follow_dao->followExists($user_id = '502993749', $follower_id = '123-both-follow', 'instagram', $is_active = true);
     $this->assertTrue($result);
     //Source follows target - true
     $result = $follow_dao->followExists($user_id = '123-both-follow', $follower_id = '502993749', 'instagram', $is_active = true);
     $this->assertTrue($result);
 }