/**
  * @param array $keys
  * @param User  $user
  * @param int   $profile_id
  *
  * @return array
  */
 public static function getByKeysAsArray($keys, $user, $profile_id)
 {
     $config = Config::create()->where('key', $keys[0])->get(1);
     $return = array();
     $available_configs = $config->available_config->get()->all_to_array();
     foreach ($available_configs as $available_config) {
         $tokens = Access_token::getAllByTypeAndUserIdAndProfileIdAsArray($available_config['type'], $user->id, $profile_id);
         $config = new Config($available_config['config_id']);
         $config = $config->to_array();
         foreach ($tokens as $token) {
             $_element = array('token' => $token, 'config' => $config, 'values' => array());
             foreach ($keys as $key) {
                 $_element['values'][$key] = $user->ifUserHasConfigValue($key, $token['id']);
             }
             $return[$available_config->type][] = $_element;
         }
     }
     return $return;
 }
 /**
  * Follow new follower in Instagram.
  *
  * @access public
  * @param $args
  */
 public function followNewFollowers($args)
 {
     $user_id = (int) $args['user_id'];
     $user = new User($user_id);
     $access_token_id = $args['id'];
     if (!$user->ifUserHasConfigValue('auto_follow', $access_token_id)) {
         return;
     }
     $instagram = $this->inicializeInstagramSocializer($user_id, $args);
     /* @var Instagram_follower[] $followers */
     $followers = $user->instagram_follower->where('need_follow', true)->where('access_token_id', $access_token_id)->get();
     foreach ($followers as $follower) {
         $answer = $instagram->modifyUserRelationship($follower->follower_id, 'follow');
         if ($answer->meta->code != 200) {
             echo $answer->meta->error_message;
             if ($answer->meta->error_type == 'OAuthPermissionsException') {
                 break;
             }
         } else {
             $follower->setNeedFollow(false);
             $follower->save();
         }
     }
 }
Example #3
0
 protected function grabberments($mention_keyword_array)
 {
     try {
         $mention_keyword_id = Arr::get($mention_keyword_array, 'id');
         $mention_keyword = new Mention_keyword($mention_keyword_id);
         $error_info = 'mkwid: ' . Arr::get($mention_keyword_array, 'social', 'no soc') . '/' . $mention_keyword_id;
         if (!$mention_keyword->exists()) {
             throw new Exception($error_info . ' doesn\'t exist.');
         }
         if ($mention_keyword->is_deleted) {
             throw new Exception($error_info . ' is set for deletion.');
         }
         if (!$mention_keyword->user_id) {
             throw new Exception($error_info . ' has no user id.');
         }
         $user = new User($mention_keyword->user_id);
         if (!$user->exists()) {
             throw new Exception($error_info . ' has no user');
         }
         $social = Arr::get($mention_keyword_array, 'social');
         if (is_null($social)) {
             throw new Exception($error_info . ' invalid social');
         }
         $user_socials = Access_token::inst()->get_user_socials($mention_keyword->user_id);
         if (!in_array($social, $user_socials)) {
             throw new Exception($error_info . ' invalid social');
         }
         $this->load->library('mentioner');
         $mentioner = Mentioner::factory($user->id);
         $mention_keyword_data = array_merge($mention_keyword_array, array('keyword' => $mention_keyword->keyword, 'exact' => $mention_keyword->exact, 'other_fields' => $mention_keyword->other_fields));
         if ($social === 'facebook') {
             $data = $mentioner->posts($mention_keyword_data, $mention_keyword_array);
         } else {
             if ($social === 'twitter') {
                 $data = $mentioner->tweets($mention_keyword_data, $mention_keyword_array);
             } else {
                 if ($social === 'google') {
                     $data = $mentioner->activities($mention_keyword_data, $mention_keyword_array);
                 } else {
                     if ($social === 'instagram') {
                         $data = $mentioner->tags($mention_keyword_data, $mention_keyword_array);
                     } else {
                         $data = array();
                     }
                 }
             }
         }
         if (!is_array($data)) {
             throw new Exception($error_info . ' no results for mentions, not an array. mkwid: ');
         }
         if ($user->ifUserHasConfigValue('auto_follow_twitter')) {
             $autoFollowTwitter = true;
             $radar = $this->get('core.radar');
             $conditions = Influencers_condition::allToOptionsArray();
         } else {
             $autoFollowTwitter = false;
         }
         foreach ($data as $original_id => $row) {
             $mention = new Mention();
             $mention->where(array('mention_keyword_id' => $mention_keyword->id, 'original_id' => $original_id))->get(1);
             $mention->social = $social;
             $mention->original_id = Arr::get($row, 'original_id');
             $mention->created_at = Arr::get($row, 'created_at');
             $message = Arr::get($row, 'message');
             $trimMessage = strlen($message) > 4000 ? substr($message, 0, 4000) : $message;
             $mention->message = $trimMessage;
             $mention->creator_id = Arr::get($row, 'creator_id');
             $mention->creator_name = Arr::get($row, 'creator_name');
             $mention->creator_image_url = Arr::get($row, 'creator_image_url');
             $mention->other_fields = serialize(Arr::get($row, 'other_fields', array()));
             $mention->source = Arr::get($row, 'source');
             $relations = array('user' => $user, 'mention_keyword' => $mention_keyword);
             $saved = $mention->save($relations);
             if (!$saved) {
                 log_message('TASK_ERROR', __FUNCTION__ . ' > ' . 'Mention not saved for mkwid: ' . $error_info . ' grabbed: ' . $mention->error->string);
             } else {
                 switch ($social) {
                     case 'twitter':
                         $followers = (int) Arr::path($row, 'other_fields.creator_followers_count');
                         $retweetCount = (int) Arr::path($row, 'other_fields.retweet_count');
                         if ($followers || $retweetCount) {
                             $mentionTwitter = new Mention_twitter();
                             $mentionTwitter->followers_count = $followers;
                             $mentionTwitter->retweet_count = $retweetCount;
                             $mentionTwitter->mention_id = $mention->id;
                             $mentionTwitter->save();
                         }
                         break;
                     case 'facebook':
                         $friendsCount = (int) Arr::path($row, 'other_fields.friends_count');
                         $commentsCount = (int) Arr::path($row, 'other_fields.comments');
                         $likesCount = (int) Arr::path($row, 'other_fields.likes');
                         if ($friendsCount || $commentsCount || $likesCount) {
                             $mentionTwitter = new Mention_facebook();
                             $mentionTwitter->friends_count = $friendsCount;
                             $mentionTwitter->comments_count = $commentsCount;
                             $mentionTwitter->likes_count = $likesCount;
                             $mentionTwitter->mention_id = $mention->id;
                             $mentionTwitter->save();
                         }
                         break;
                     case 'google':
                         $peopleCount = (int) Arr::path($row, 'other_fields.people_count');
                         $commentsCount = (int) Arr::path($row, 'other_fields.comments');
                         $plusonersCount = (int) Arr::path($row, 'other_fields.plusoners');
                         $resharersCount = (int) Arr::path($row, 'other_fields.resharers');
                         if ($peopleCount || $commentsCount || $plusonersCount || $resharersCount) {
                             $mentionTwitter = new Mention_google();
                             $mentionTwitter->people_count = $peopleCount;
                             $mentionTwitter->comments_count = $commentsCount;
                             $mentionTwitter->plusoners_count = $plusonersCount;
                             $mentionTwitter->resharers_count = $resharersCount;
                             $mentionTwitter->mention_id = $mention->id;
                             $mentionTwitter->save();
                         }
                         break;
                 }
             }
         }
         // get socials that were already grabbed
         $grabbed_socials = $mention_keyword->get_grabbed_socials_as_array();
         if (!in_array($social, $grabbed_socials)) {
             $grabbed_socials[] = $social;
             $now = date('U');
             $mention_keyword->grabbed_socials = implode(',', $grabbed_socials);
             $mention_keyword->grabbed_at = $now;
             $saved = $mention_keyword->save();
             if (!$saved) {
                 log_message('TASK_ERROR', __FUNCTION__ . ' > ' . 'Mention keyword not saved for mkwid: ' . $error_info . ' grabbed: ' . $mention->error->string);
             }
         }
         log_message('TASK_SUCCESS', __FUNCTION__ . ' > ' . 'Mentions for mkwid: ' . $error_info . ' grabbed');
     } catch (Exception $e) {
         log_message('TASK_ERROR', __FUNCTION__ . ' > ' . $e->getMessage());
         return;
         // throw $e;
     }
 }
Example #4
0
 /**
  * Search users and follow them
  *
  * @param $args
  */
 public function searchUsers($args)
 {
     $user_id = (int) $args['user_id'];
     $user = new User($user_id);
     $access_token_id = $args['id'];
     if (!$user->ifUserHasConfigValue('auto_follow_users_by_search', $access_token_id)) {
         return;
     }
     log_message('TASK_DEBUG', __FUNCTION__ . 'Twitter: start search users to follow.' . $this->getDebugInfo($user, $access_token_id));
     $date = new DateTime('UTC 00:00:00');
     $user_timezone = new DateTimeZone(User_timezone::get_user_timezone($user_id));
     $timezone_offset = $user_timezone->getOffset($date) / 3600;
     $twitter = $this->inicializeTwitterSocializer($user_id, $args);
     $user_search_keywords = $user->getUserSearchKeywords($args['profile_id']);
     $number_of_added_users = $user->getDateToAddUserTwitter($access_token_id);
     $max_daily_auto_follow_users_by_search = (int) $user->ifUserHasConfigValue('max_daily_auto_follow_users_by_search', $access_token_id);
     $old_date = DateTime::createFromFormat('!Y-m-d', $number_of_added_users->date);
     if (!$number_of_added_users->id) {
         $number_of_added_users->date = $date->format('Y-m-d');
         $number_of_added_users->setUserId($user_id);
         $number_of_added_users->token_id = $access_token_id;
         $number_of_added_users->count = 0;
     } elseif ($old_date < $date) {
         $number_of_added_users = new Number_of_added_users_twitter();
         $number_of_added_users->date = $date->format('Y-m-d');
         $number_of_added_users->setUserId($user_id);
         $number_of_added_users->count = 0;
         $number_of_added_users->token_id = $access_token_id;
         $number_of_added_users->save();
     } elseif ($old_date > $date) {
         \log_message('TASK_SUCCESS', __FUNCTION__ . 'Twitter: ' . 'Twitter followers already added.' . "\n" . $this->getDebugInfo($user, $access_token_id));
         return;
     }
     unset($old_date);
     $age_of_account = $user->ifUserHasConfigValue('age_of_account', $access_token_id);
     if (!$age_of_account) {
         $age_of_account = 0;
     } else {
         $age_of_account_splited = preg_split('/,/', $age_of_account);
         if (count($age_of_account_splited)) {
             if (count($age_of_account_splited) > 1) {
                 $age_of_account = $age_of_account_splited;
             }
         }
     }
     $tweets_count = $user->ifUserHasConfigValue('number_of_tweets', $access_token_id);
     if (!$tweets_count) {
         $tweets_count = 0;
     } else {
         $tweets_count_splited = preg_split('/,/', $tweets_count);
         if (count($tweets_count_splited)) {
             if (count($tweets_count_splited) > 1) {
                 $tweets_count = $tweets_count_splited;
             }
         }
     }
     foreach ($user_search_keywords as $user_search_keyword) {
         $other_field = $user_search_keyword->get_other_fields();
         $query = $twitter->create_query($user_search_keyword->keyword, $other_field['include'], $other_field['exclude'], $user_search_keyword->exact);
         $queryArgs = ['min_followers' => $user_search_keyword->min_followers, 'max_followers' => $user_search_keyword->max_followers, 'max_id' => $user_search_keyword->max_id, 'age_of_account' => $age_of_account, 'tweets_count' => $tweets_count];
         $users = $twitter->search_users($query, $queryArgs);
         foreach ($users['users'] as $twitter_user_id) {
             if (!$user->isUserHasTwitterFollower($twitter_user_id, $access_token_id)) {
                 if ($max_daily_auto_follow_users_by_search && $max_daily_auto_follow_users_by_search > $number_of_added_users->count || !$max_daily_auto_follow_users_by_search) {
                     $date = DateTime::createFromFormat('!Y-m-d', $number_of_added_users->date);
                     $number_of_added_users->count += 1;
                     $number_of_added_users->save();
                 } else {
                     $date = DateTime::createFromFormat('!Y-m-d', $number_of_added_users->date);
                     $date->modify('+1 days');
                     $number_of_added_users = new Number_of_added_users_twitter();
                     $number_of_added_users->date = $date->format('Y-m-d');
                     $number_of_added_users->setUserId($user_id);
                     $number_of_added_users->token_id = $access_token_id;
                     $number_of_added_users->count = 1;
                     $number_of_added_users->save();
                 }
                 $twitter_follower = new Twitter_follower();
                 $twitter_follower->setFollowerId($twitter_user_id);
                 $twitter_follower->setUserId($user_id);
                 $twitter_follower->setAccessTokenId($access_token_id);
                 $start_date = clone $date;
                 $start_date->modify($timezone_offset * -1 . ' hours');
                 $end_date = clone $date;
                 $end_date->modify($timezone_offset * -1 . ' hours');
                 if ($end_date <= $start_date) {
                     $end_date->modify('1 days');
                 }
                 $twitter_follower->setStartFollowTime($user_search_keyword->getStartDateTime($start_date)->getTimestamp());
                 $twitter_follower->setEndFollowTime($user_search_keyword->getEndDateTime($end_date)->getTimestamp());
                 unset($start_date);
                 unset($end_date);
                 $twitter_follower->setNeedFollow(true);
                 $twitter_follower->save();
             }
         }
         log_message('TASK_SUCCESS', __FUNCTION__ . 'Twitter: ' . 'By keywords ' . $query . ' add ' . count($users['users']) . ' users.' . "\n" . $this->getDebugInfo($user, $access_token_id));
         if ($user_search_keyword->max_id != $users['max_id']) {
             $user_search_keyword->max_id = $users['max_id'];
         } else {
             $user_search_keyword->max_id = null;
         }
         $user_search_keyword->save();
     }
 }