コード例 #1
0
 public function actionGrab()
 {
     //        Yii::$app->response->format = Response::FORMAT_JSON;
     $success = true;
     $source = Source::find()->joinWith('logs')->where(['enable' => true])->orderBy(['updated' => SORT_ASC])->one();
     if ($source) {
         $grabber = new Grabber($source);
         $newPosts = $grabber->execute();
         if (!empty($newPosts)) {
             foreach ($newPosts as $post) {
                 $hashPost = md5($post);
                 // есть ли в базе Post или Moderation
                 $doubling = Post::findOne(['hash' => $hashPost]) || Moderation::findOne(['hash' => $hashPost]);
                 if ($doubling) {
                     continue;
                 }
                 // добавляем новый пост на модерацию
                 $model = new Moderation();
                 $model->text = $post;
                 $model->hash = $hashPost;
                 $model->ip = "127.0.0.1";
                 //Yii::$app->request->getUserIP();
                 $model->user_agent = "Auto Grabber";
                 //Yii::$app->request->getUserAgent();
                 $success = $model->save() && $success;
             }
         }
         $source->updateLog();
     }
     return $success;
 }