Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.80 KB | None | 0 0
  1.     const EXCLUDE_LIKE_FIELD_KEY = 'likesCount';
  2.     const EXCLUDE_CONVERSION_FIELD_KEY = 'conversion';
  3.     const EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY = 'likesToFollowers';
  4.  
  5.     public function actionTest()
  6.     {
  7.         /*$datePost = date('Y-m-d H:i:s', 1510258861);
  8.         $mbNow = date('Y-m-d H:i:s', strtotime($datePost . ' + 17 hours'));
  9.         Y::dump($datePost, true);*/
  10.  
  11.         #$userInfo = InstagramWeb::getUserInfoRequest('https://www.instagram.com/kimkardashian/', true);
  12.  
  13.         /** @var BackendAccount[] $backendAccountList */
  14.         $backendAccountList = BackendAccount::find()->limit(5)->all();
  15.  
  16.         $allPostByLike = [];
  17.         $allPostByConversion = [];
  18.         $allPostByLikesToFollowers = [];
  19.         foreach ($backendAccountList as $backendAccount) {
  20.  
  21.             $rawAccountInfo = InstagramWeb::getUserInfoRequest($backendAccount->account_link, true);
  22.  
  23.             $accountInfo = $this->prepareAccountInfo($rawAccountInfo);
  24.  
  25.             #Y::dump($accountInfo, true);
  26.  
  27.             $allPostByLike = array_merge($allPostByLike, $accountInfo['excludeBadPostByLike']);
  28.             $allPostByConversion = array_merge($allPostByConversion, $accountInfo['excludeBadPostByConversion']);
  29.             $allPostByLikesToFollowers = array_merge($allPostByLikesToFollowers, $accountInfo['excludeBadPostByLikesToFollowers']);
  30.         }
  31.  
  32.         #Y::dump($allPostByLikesToFollowers, true);
  33.  
  34.         $excludeBadPostByLike = $this->excludedPostByAll($allPostByLike);
  35.         $excludeBadPostByConversion = $this->excludedPostByAll($allPostByConversion, self::EXCLUDE_CONVERSION_FIELD_KEY);
  36.         $excludeBadPostByLikesToFollowers = $this->excludedPostByAll($allPostByLikesToFollowers, self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY);
  37.  
  38.         #Y::dump(count([$excludeBadPostByLike, $excludeBadPostByConversion, $excludeBadPostByLikesToFollowers]), true);
  39.  
  40.         Y::dump($excludeBadPostByLike, false);
  41.         Y::dump($excludeBadPostByConversion, false);
  42.         Y::dump($excludeBadPostByLikesToFollowers, true);
  43.  
  44.         Y::dump($allPostByLike, false);
  45.         Y::dump($allPostByConversion, true);
  46.  
  47.         echo 'test';
  48.     }
  49.  
  50.     private function excludedPostByAll($data, $fieldKey = self::EXCLUDE_LIKE_FIELD_KEY)
  51.     {
  52.         ArrayHelper::multisort($data, 'unixTimestamp', SORT_DESC);
  53.  
  54.         #ArrayHelper::multisort($data, $fieldKey, SORT_DESC);
  55.  
  56.         $allPostData = ArrayHelper::getColumn($data, $fieldKey);
  57.  
  58.         return $this->excludeBadPost($data, $this->getMedian($allPostData), $fieldKey);
  59.     }
  60.  
  61.     private function prepareAccountInfo($accountInfo)
  62.     {
  63.         $processedInfo = [];
  64.         $processedInfo['followers'] = ArrayHelper::getValue($accountInfo, 'followed_by.count', 0);
  65.         $processedInfo['accountName'] = ArrayHelper::getValue($accountInfo, 'username', null);
  66.  
  67.         return $this->preparePostInfo(ArrayHelper::getValue($accountInfo, 'media.nodes', []), $processedInfo);
  68.     }
  69.  
  70.     private function preparePostInfo($rawPostList, $processedInfo)
  71.     {
  72.         #Y::dump($processedInfo, true);
  73.  
  74.         $allLikes = [];
  75.         $allConversion = [];
  76.         $processedPostInfoList = [];
  77.         if ($rawPostList) {
  78.             foreach ($rawPostList as $index => $post) {
  79.                 $processedPostInfoList[$index] = [
  80.                     'postId' => ArrayHelper::getValue($post, 'id', 0),
  81.                     'caption' => ArrayHelper::getValue($post, 'caption', 0),
  82.                     'commentsCount' => ArrayHelper::getValue($post, 'comments.count', 0),
  83.                     self::EXCLUDE_LIKE_FIELD_KEY => ArrayHelper::getValue($post, 'likes.count', 0),
  84.                     'postCode' => ArrayHelper::getValue($post, 'code', 0),
  85.                     'unixTimestamp' => ArrayHelper::getValue($post, 'date', 0),
  86.                     'imageLink' => ArrayHelper::getValue($post, 'thumbnail_src', 0),
  87.                 ];
  88.  
  89.                 $processedPostInfoList[$index][self::EXCLUDE_CONVERSION_FIELD_KEY] = ($processedPostInfoList[$index][self::EXCLUDE_LIKE_FIELD_KEY] / $processedInfo['followers']) * 100;
  90.  
  91.                 $allLikes[] = $processedPostInfoList[$index][self::EXCLUDE_LIKE_FIELD_KEY];
  92.                 $allConversion[] = $processedPostInfoList[$index][self::EXCLUDE_CONVERSION_FIELD_KEY];
  93.  
  94.                 $processedPostInfoList[$index] = array_merge($processedPostInfoList[$index], $processedInfo);
  95.             }
  96.         }
  97.  
  98.         ArrayHelper::multisort($processedPostInfoList, 'unixTimestamp', SORT_DESC);
  99.  
  100.         $likeMedian = $this->getMedian($allLikes);
  101.         $conversionMedian = $this->getMedian($allConversion);
  102.  
  103.         return [
  104.             'excludeBadPostByLike' => $this->excludeBadPost($processedPostInfoList, $likeMedian),
  105.             'excludeBadPostByConversion' => $this->excludeBadPost($processedPostInfoList, $conversionMedian, self::EXCLUDE_CONVERSION_FIELD_KEY),
  106.             'excludeBadPostByLikesToFollowers' => $this->prepareLikesToFollowers($processedPostInfoList, $likeMedian),
  107.         ];
  108.     }
  109.  
  110.     private function prepareLikesToFollowers($tempProcessedPostInfoList, $likeMedian)
  111.     {
  112.         $processedPostInfoList = [];
  113.         foreach ($tempProcessedPostInfoList as $key => $processedPostInfo) {
  114.             $processedPostInfoList[$key] = $processedPostInfo;
  115.             $processedPostInfoList[$key][self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY] = $likeMedian / $processedPostInfo[self::EXCLUDE_LIKE_FIELD_KEY];
  116.  
  117.         };
  118.  
  119.         return $this->excludeBadPost(
  120.             $processedPostInfoList,
  121.             $this->getMedian(ArrayHelper::getColumn($processedPostInfoList, self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY)),
  122.             self::EXCLUDE_LIKES_TO_FOLLOWERS_FIELD_KEY
  123.         );
  124.     }
  125.  
  126.     private function excludeBadPost($tempProcessedPostInfoList, $median, $excludeKey = self::EXCLUDE_LIKE_FIELD_KEY)
  127.     {
  128.         $processedPostInfoList = [];
  129.         foreach ($tempProcessedPostInfoList as $key => $processedPostInfo) {
  130.             if ($processedPostInfo[$excludeKey] > $median) {
  131.                 $processedPostInfoList[] = $processedPostInfo;
  132.             }
  133.         };
  134.  
  135.         return $processedPostInfoList;
  136.     }
  137.  
  138.     /**
  139.      * Возвращают медиану чисел в одномерном массиве
  140.      * @param array $data
  141.      * @return mixed
  142.      */
  143.     private function getMedian(array $data)
  144.     {
  145.         sort($data);
  146.         $count = count($data);
  147.         $middle = (int) floor($count/2);
  148.  
  149.         return ($count % 2 === 0) ? ($data[$middle - 1] + $data[$middle]) / 2 : $data[$middle];
  150.     }
  151.  
  152.     /**
  153.      * Возвращают среднее арефметическое чисел в одномерном массиве
  154.      * @param array $data
  155.      * @return mixed
  156.      */
  157.     private function getAverage(array $data)
  158.     {
  159.         return array_sum($data)/count($data);
  160.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement