예제 #1
0
 /**
  * Get crm activities
  *
  * @param $directoryArray
  * @return array
  */
 public function getCrmActivities($directoryArray)
 {
     log_message('TASK_SUCCESS', __FUNCTION__ . ' > ' . ' called in mentioner');
     if (empty($directoryArray['instagram_link'])) {
         log_message('TASK_ERROR', __FUNCTION__ . ' > ' . 'no link for directory id=' . $directoryArray['id']);
         return;
     }
     $access_token = Access_token::getOneByTypeAndUserIdAndProfileIdAsArray('instagram', $this->userId, $this->profileId);
     /* @var Socializer_Instagram $instagram */
     $instagram = $this->socializer->factory('Instagram', $this->userId, $access_token);
     $params = array('limit' => $this->limit, 'since' => strtotime('yesterday'), 'until' => strtotime('tomorrow'));
     $params['continue_from'] = isset($directoryArray['continue_from']) ? $directoryArray['continue_from'] : null;
     $userId = $instagram->getUserFromLink($directoryArray['instagram_link']);
     if (!$userId) {
         log_message('TASK_ERROR', __FUNCTION__ . ' > ' . 'invalid username in directory id=' . $directoryArray['id']);
         return;
     }
     $result = array();
     while (true) {
         sleep(1);
         try {
             $data = $instagram->activities($userId, $params);
         } catch (Exception $e) {
             log_message('TASK_ERROR', __FUNCTION__ . ' > ' . $e->getMessage());
             $data = null;
         }
         if (!is_array($data)) {
             break;
         }
         list($parsed, $parsed_count) = $this->parseActivities($data);
         $result = array_merge($result, $parsed);
         // check if per task limit reached
         if (count($result) >= $this->maxPerRequest - $this->limit) {
             // if reached create another task
             $lastResult = end($result);
             $directoryArray['continue_from'] = $lastResult->id;
             if (!$this->test) {
                 log_message('TASK_SUCCESS', __FUNCTION__ . ' > ' . 'loop; extra queue');
                 $this->jobQueue->addJob('tasks/mentions_task/grabber', $directoryArray, array('thread' => CLI_controller::CRM_THREAD));
             }
             break;
         }
         if (count($data) < $this->limit) {
             break;
         }
     }
     log_message('TASK_SUCCESS', __FUNCTION__ . ' fetched - ' . count($result));
     return $result;
 }