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 testPageThroughFollowers() { $ic = new InstagramCrawler($this->profile3_instance, 'fauxaccesstoken', 120); $ic->fetchPostsAndReplies(); $ic->fetchFollowers(); //@TODO Finish writing tests here //Test no next page, assert archive loaded is set //Test next page till end, assert archive loaded is set //Test enough pages to exceed max API limit, assert last cursor is set //Test archive is loaded, and updateStaleFollows runs as expected }