Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const EXCLUDE_LIKE_FIELD_KEY = 'likesCount';
- const EXCLUDE_CONVERSION_FIELD_KEY = 'conversion';
- const EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY = 'likesToFollowers';
- public function actionTest()
- {
- /*$datePost = date('Y-m-d H:i:s', 1510258861);
- $mbNow = date('Y-m-d H:i:s', strtotime($datePost . ' + 17 hours'));
- Y::dump($datePost, true);*/
- #$userInfo = InstagramWeb::getUserInfoRequest('https://www.instagram.com/kimkardashian/', true);
- /** @var BackendAccount[] $backendAccountList */
- $backendAccountList = BackendAccount::find()->limit(5)->all();
- $allPostByLike = [];
- $allPostByConversion = [];
- $allPostByLikesToFollowers = [];
- foreach ($backendAccountList as $backendAccount) {
- $rawAccountInfo = InstagramWeb::getUserInfoRequest($backendAccount->account_link, true);
- $accountInfo = $this->prepareAccountInfo($rawAccountInfo);
- #Y::dump($accountInfo, true);
- $allPostByLike = array_merge($allPostByLike, $accountInfo['excludeBadPostByLike']);
- $allPostByConversion = array_merge($allPostByConversion, $accountInfo['excludeBadPostByConversion']);
- $allPostByLikesToFollowers = array_merge($allPostByLikesToFollowers, $accountInfo['excludeBadPostByLikesToFollowers']);
- }
- #Y::dump($allPostByLikesToFollowers, true);
- $excludeBadPostByLike = $this->excludedPostByAll($allPostByLike);
- $excludeBadPostByConversion = $this->excludedPostByAll($allPostByConversion, self::EXCLUDE_CONVERSION_FIELD_KEY);
- $excludeBadPostByLikesToFollowers = $this->excludedPostByAll($allPostByLikesToFollowers, self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY);
- #Y::dump(count([$excludeBadPostByLike, $excludeBadPostByConversion, $excludeBadPostByLikesToFollowers]), true);
- Y::dump($excludeBadPostByLike, false);
- Y::dump($excludeBadPostByConversion, false);
- Y::dump($excludeBadPostByLikesToFollowers, true);
- Y::dump($allPostByLike, false);
- Y::dump($allPostByConversion, true);
- echo 'test';
- }
- private function excludedPostByAll($data, $fieldKey = self::EXCLUDE_LIKE_FIELD_KEY)
- {
- ArrayHelper::multisort($data, 'unixTimestamp', SORT_DESC);
- #ArrayHelper::multisort($data, $fieldKey, SORT_DESC);
- $allPostData = ArrayHelper::getColumn($data, $fieldKey);
- return $this->excludeBadPost($data, $this->getMedian($allPostData), $fieldKey);
- }
- private function prepareAccountInfo($accountInfo)
- {
- $processedInfo = [];
- $processedInfo['followers'] = ArrayHelper::getValue($accountInfo, 'followed_by.count', 0);
- $processedInfo['accountName'] = ArrayHelper::getValue($accountInfo, 'username', null);
- return $this->preparePostInfo(ArrayHelper::getValue($accountInfo, 'media.nodes', []), $processedInfo);
- }
- private function preparePostInfo($rawPostList, $processedInfo)
- {
- #Y::dump($processedInfo, true);
- $allLikes = [];
- $allConversion = [];
- $processedPostInfoList = [];
- if ($rawPostList) {
- foreach ($rawPostList as $index => $post) {
- $processedPostInfoList[$index] = [
- 'postId' => ArrayHelper::getValue($post, 'id', 0),
- 'caption' => ArrayHelper::getValue($post, 'caption', 0),
- 'commentsCount' => ArrayHelper::getValue($post, 'comments.count', 0),
- self::EXCLUDE_LIKE_FIELD_KEY => ArrayHelper::getValue($post, 'likes.count', 0),
- 'postCode' => ArrayHelper::getValue($post, 'code', 0),
- 'unixTimestamp' => ArrayHelper::getValue($post, 'date', 0),
- 'imageLink' => ArrayHelper::getValue($post, 'thumbnail_src', 0),
- ];
- $processedPostInfoList[$index][self::EXCLUDE_CONVERSION_FIELD_KEY] = ($processedPostInfoList[$index][self::EXCLUDE_LIKE_FIELD_KEY] / $processedInfo['followers']) * 100;
- $allLikes[] = $processedPostInfoList[$index][self::EXCLUDE_LIKE_FIELD_KEY];
- $allConversion[] = $processedPostInfoList[$index][self::EXCLUDE_CONVERSION_FIELD_KEY];
- $processedPostInfoList[$index] = array_merge($processedPostInfoList[$index], $processedInfo);
- }
- }
- ArrayHelper::multisort($processedPostInfoList, 'unixTimestamp', SORT_DESC);
- $likeMedian = $this->getMedian($allLikes);
- $conversionMedian = $this->getMedian($allConversion);
- return [
- 'excludeBadPostByLike' => $this->excludeBadPost($processedPostInfoList, $likeMedian),
- 'excludeBadPostByConversion' => $this->excludeBadPost($processedPostInfoList, $conversionMedian, self::EXCLUDE_CONVERSION_FIELD_KEY),
- 'excludeBadPostByLikesToFollowers' => $this->prepareLikesToFollowers($processedPostInfoList, $likeMedian),
- ];
- }
- private function prepareLikesToFollowers($tempProcessedPostInfoList, $likeMedian)
- {
- $processedPostInfoList = [];
- foreach ($tempProcessedPostInfoList as $key => $processedPostInfo) {
- $processedPostInfoList[$key] = $processedPostInfo;
- $processedPostInfoList[$key][self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY] = $likeMedian / $processedPostInfo[self::EXCLUDE_LIKE_FIELD_KEY];
- };
- return $this->excludeBadPost(
- $processedPostInfoList,
- $this->getMedian(ArrayHelper::getColumn($processedPostInfoList, self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY)),
- self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY
- );
- }
- private function excludeBadPost($tempProcessedPostInfoList, $median, $excludeKey = self::EXCLUDE_LIKE_FIELD_KEY)
- {
- $processedPostInfoList = [];
- foreach ($tempProcessedPostInfoList as $key => $processedPostInfo) {
- if ($processedPostInfo[$excludeKey] > $median) {
- $processedPostInfoList[] = $processedPostInfo;
- }
- };
- return $processedPostInfoList;
- }
- /**
- * Возвращают медиану чисел в одномерном массиве
- * @param array $data
- * @return mixed
- */
- private function getMedian(array $data)
- {
- sort($data);
- $count = count($data);
- $middle = (int) floor($count/2);
- return ($count % 2 === 0) ? ($data[$middle - 1] + $data[$middle]) / 2 : $data[$middle];
- }
- /**
- * Возвращают среднее арефметическое чисел в одномерном массиве
- * @param array $data
- * @return mixed
- */
- private function getAverage(array $data)
- {
- return array_sum($data)/count($data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement