예제 #1
1
 public function actionParserStart()
 {
     $file = Yii::getAlias('@json') . DIRECTORY_SEPARATOR . 'import.json';
     if (file_exists($file)) {
         unlink($file);
     }
     //SET SESSION wait_timeout=600000;
     ini_set('max_execution_time', 60000);
     ini_set('wait_timeout', 60000);
     ini_set('memory_limit', '128M');
     ini_set('default_charset', 'UTF-8');
     //header('Content-Type: text/html; charset=UTF-8');
     $arrResult = [];
     $arrResult2 = [];
     $html = SHD::file_get_html('http://politrussia.com/news/');
     // Find all links
     foreach ($html->find('a.overlink') as $element) {
         //echo $element->href. '<br>';
         $arrResult[] = $element->href;
     }
     $i = 0;
     foreach ($arrResult as $key => $link) {
         $html = SHD::file_get_html('http://politrussia.com' . $link);
         $i++;
         $content = $html->find('div[class="news_text"]', 0)->plaintext;
         $title = $html->find('h1[itemprop="name"]', 0)->plaintext;
         foreach ($html->find('img[itemprop="contentUrl"]') as $element) {
             $img2 = 'http://politrussia.com' . $element->src;
         }
         //$content2 = mb_convert_encoding($content, "UTF-8");
         //$title2 = mb_convert_encoding($title, "UTF-8");
         $arrResult2[$key]['title'] = $title;
         $arrResult2[$key]['content'] = $content;
         $arrResult2[$key]['image'] = $img2;
     }
     //vd($arrResult2);
     if (!empty($arrResult2)) {
         //$model = ImportNews::deleteAll();
         // данные в json
         $data = json_encode($arrResult2);
         //echo $data;
         //vd(1);
         $file2 = Yii::getAlias('@json') . DIRECTORY_SEPARATOR . 'import.json';
         file_put_contents($file2, $data);
     }
     //        ImportNews::deleteAll();
     //        foreach($arrResult2 as $key => $row){
     //
     //            $modelBlog = new ImportNews();
     //            $modelBlog->title = $row['title'];
     //            $modelBlog->content = $row['content'];
     //            $modelBlog->created_at = time();
     //            $modelBlog->updated_at = time();
     //            $modelBlog->author = Yii::$app->user->id;
     //            $modelBlog->image = $row['img'];
     //
     //            //$dublicate = ImportNews::getDublicateByTitle($row['title']);
     //            //if(!$dublicate){
     //                $modelBlog->save();
     //            //}
     //
     //
     //        }
     return $this->redirect('/admin/index');
 }
예제 #2
0
 /**
  * Displays a single Apartment model.
  * @param integer $id
  * @return mixed
  */
 public function actionDetails($id)
 {
     $model = $this->findModel($id);
     if ($model->html) {
         return $this->renderPartial('details', ['model' => $model]);
     } else {
         $html = SHD::file_get_html($model->url);
         $model->html = $html->find('.offercontentinner', 0);
         //            $model->description = $html->find('.offercontentinner',0);
         //            $model->phones = $html->find('.offercontentinner',0);
         //            $model->sows = $html->find('.offercontentinner',0);
         //            $model->rooms = $html->find('.offercontentinner',0);
         //            $model->rent_types = $html->find('.offercontentinner',0);
         //            $model->images = $html->find('.offercontentinner',0);
         //            $model->sows = $html->find('.offercontentinner',0);
         //            $model->sows = $html->find('.offercontentinner',0);
         $model->save();
         return $this->renderPartial('details', ['model' => $model]);
     }
 }