/** * Retrives all of a users friends from the Instagram API and stores them in the database * @return null */ private function storeFriends() { if ($this->instance->network != 'instagram') { return; } //Retrieve friends via the Instagram API $user_id = $this->instance->network_user_id; $access_token = $this->access_token; $network = $user_id == $this->instance->network_user_id ? $this->instance->network : 'instagram'; try { $friends = InstagramAPIAccessor::apiRequest('friends', $user_id, $access_token); } catch (Instagram\Core\ApiException $e) { $this->logger->logInfo(get_class($e) . " Error fetching friends from Instagram API, error was " . $e->getMessage(), __METHOD__ . ',' . __LINE__); return; } if (isset($friends)) { //store relationships in follows table $follows_dao = DAOFactory::getDAO('FollowDAO'); $count_dao = DAOFactory::getDAO('CountHistoryDAO'); $user_dao = DAOFactory::getDAO('UserDAO'); foreach ($friends as $friend) { $follower_id = null; try { $follower_id = $friend->getId(); } catch (Instagram\Core\ApiException $e) { $this->logger->logInfo(get_class($e) . " Error fetching " . Utils::varDumpToString($friend) . "'s details from Instagram API, error was " . $e->getMessage(), __METHOD__ . ',' . __LINE__); } if (isset($follower_id)) { if ($follows_dao->followExists($user_id, $follower_id, $network)) { // follow relationship already exists $follows_dao->update($user_id, $follower_id, $network); } else { // follow relationship does not exist yet $follows_dao->insert($user_id, $follower_id, $network); } $follower_details = $friend; if (isset($follower_details)) { $follower_details->network = $network; } $follower = $this->parseUserDetails($follower_details); $follower_object = new User($follower); if (isset($follower_object)) { $user_dao->updateUser($follower_object); } } } //totals in follower_count table $count_dao->insert($user_id, $network, $friends->count(), null, 'followers'); } else { throw new Instagram\Core\ApiAuthException('Error retrieving friends'); } $this->logger->logInfo("Ending", __METHOD__ . ',' . __LINE__); }
/** * Page back through friends starting at the beginning (cursor is null) or at cursor * until either * A. No more next cursor * if A mark next cursor null * or B. out of API calls * if B store follows_next_cursor for pickup next time * @param str $follows_next_cursor * @return void */ public function pageThroughFriends($follows_next_cursor = null) { $this->logger->logInfo("Start paging through friends", __METHOD__ . ',' . __LINE__); //Retrieve friends via the Instagram API $user_id = $this->instance->network_user_id; $network = $user_id == $this->instance->network_user_id ? $this->instance->network : 'instagram'; $api_param = null; if (isset($follows_next_cursor)) { $api_param = array('next_cursor' => $follows_next_cursor); } try { $this->logger->logInfo("Make api call", __METHOD__ . ',' . __LINE__); $friends = $this->api_accessor->apiRequest('friends', $api_param); } catch (Exception $e) { $this->logger->logInfo(get_class($e) . " Error fetching followers from Instagram API, error was " . $e->getMessage(), __METHOD__ . ',' . __LINE__); return; } $follows_dao = DAOFactory::getDAO('FollowDAO'); $continue = isset($friends); while ($continue) { foreach ($friends as $friend) { //store user in users table $this->fetchUser($friend->id, 'Friends', $friend->username, $friend->full_name, $friend->profile_picture); //store relationships in follows table if ($follows_dao->followExists($friend->id, $user_id, $network)) { // follow relationship already exists $follows_dao->update($friend->id, $user_id, $network); } else { // follow relationship does not exist yet $follows_dao->insert($friend->id, $user_id, $network); } } $this->logger->logInfo("Done with " . $friends->count() . " followers", __METHOD__ . ',' . __LINE__); $next_cursor = $friends->getNext(); $friends = null; if (isset($next_cursor)) { $this->logger->logInfo("Next cursor is set to " . $next_cursor, __METHOD__ . ',' . __LINE__); //Persist the next cursor in the instance $this->instance->followed_by_next_cursor = $next_cursor; $api_param['cursor'] = $next_cursor; try { $friends = $this->api_accessor->apiRequest('friends', $api_param); if (!isset($friends) || $friends->count() == 0) { $this->logger->logInfo("No friends returned, marking archive loaded", __METHOD__ . ',' . __LINE__); $this->instance->is_archive_loaded_friends = true; $this->instance->follows_next_cursor = null; $continue = false; } } catch (Exception $e) { $this->instance->follows_next_cursor = $next_cursor; $this->logger->logInfo("Stopping due to " . get_class($e) . "; saving cursor " . $next_cursor, __METHOD__ . ',' . __LINE__); $continue = false; } } else { $this->logger->logInfo("No next cursor available", __METHOD__ . ',' . __LINE__); $this->instance->is_archive_loaded_follows = true; $this->instance->follows_next_cursor = null; $continue = false; } } $this->logger->logInfo("Done paging through friends", __METHOD__ . ',' . __LINE__); }