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; } }
/** * Remove single mention keyword with all collected mentions * * @param array $mention_keyword_array - mention keyword model arrayed * * @throws Exception */ public function remove_deleted($mention_keyword_array) { try { $mention_keyword_id = isset($mention_keyword_array['id']) ? $mention_keyword_array['id'] : null; $mention_keyword = new Mention_keyword($mention_keyword_id); if (!$mention_keyword->exists()) { throw new Exception('mkwid: ' . $mention_keyword_id . ' doesn\'t exist.'); } $mention_keyword_mentions = Mention::inst()->get_by_mention_keyword_id($mention_keyword_id); $mention_keyword_mentions->delete_all(); $mention_keyword->delete(); log_message('TASK_SUCCESS', __FUNCTION__ . ' > ' . 'Mention keyword and mentions for mkwid: ' . $mention_keyword_array['id'] . ' deleted'); } catch (Exception $e) { log_message('TASK_ERROR', __FUNCTION__ . ' > ' . $e->getMessage()); return; // throw $e; } }