Пример #1
0
 /**
  * Creates a new ParseOtchet model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new ParseOtchet();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Пример #2
0
 /**
  * парсим "кскфорум"
  * обновляем отчеты
  */
 public function importReport($offset, $limit)
 {
     $saved = 0;
     $errors = [];
     $models = ParseKsk::find()->where(['parse_region_id' => $this->id])->andWhere(['IS NOT', 'url_otchet', null])->offset($offset)->limit($limit)->all();
     foreach ($models as $model) {
         /* @var $model ParseKsk */
         $saw = new NokogiriHelper(file_get_contents($model->url_otchet));
         foreach ($saw->get('.col_f_content') as $forum_name) {
             $h4s = $forum_name['h4'];
             if (isset($forum_name['span'][0]['span'])) {
                 $posted_at = $forum_name['span'][0]['span'][0]['#text'][0];
             } else {
                 $posted_at = $forum_name['span'][1]['span'][0]['#text'][0];
             }
             foreach ($h4s as $h4) {
                 $a = $h4['a'][0];
                 $name = trim($a['span'][0]['#text'][0]);
                 $url = $a['href'];
                 $model_otchet = ParseOtchet::find()->where(['name' => $name])->one();
                 if (is_null($model_otchet)) {
                     $model_otchet = new ParseOtchet();
                     $model_otchet->parse_ksk_id = $model->id;
                     $model_otchet->name = $name;
                     $model_otchet->url_otchet = $url;
                     $model_otchet->posted_at = date('Y-m-d H:i:s', strtotime($posted_at));
                     $model_otchet->updated_at = new \yii\db\Expression('utc_timestamp()');
                     $model_otchet->save() ? $saved++ : ($errors[] = $model_otchet->errors);
                 }
             }
         }
     }
     if ($saved > 0) {
         //            Yii::$app->session->setFlash('info', "Создано записей - {$saved}<br>");
         $this->updated_at = new \yii\db\Expression('utc_timestamp()');
         $this->save();
     }
     if (sizeof($errors) > 0) {
         //            Yii::$app->session->setFlash('warning', Json::encode($errors));
     }
     return sizeof($models) > 0;
 }
Пример #3
0
 public function actionUpdateReport()
 {
     foreach (ParseRegion::find()->all() as $region) {
         /* @var $region ParseRegion*/
         echo $region->url . "\n";
         $saved = 0;
         $errors = [];
         $models = ParseKsk::find()->where(['parse_region_id' => $region->id])->andWhere(['IS NOT', 'url_otchet', null])->all();
         foreach ($models as $model) {
             /* @var $model ParseKsk */
             echo '  ' . $model->url_otchet . "\n";
             $saw = new NokogiriHelper(file_get_contents($model->url_otchet));
             $ok = $saved;
             foreach ($saw->get('.col_f_content') as $forum_name) {
                 $h4s = $forum_name['h4'];
                 if (isset($forum_name['span'][0]['span'])) {
                     $posted_at = $forum_name['span'][0]['span'][0]['#text'][0];
                 } else {
                     $posted_at = $forum_name['span'][1]['span'][0]['#text'][0];
                 }
                 foreach ($h4s as $h4) {
                     $a = $h4['a'][0];
                     $name = trim($a['span'][0]['#text'][0]);
                     $url = $a['href'];
                     $model_otchet = ParseOtchet::find()->where(['name' => $name])->one();
                     if (is_null($model_otchet)) {
                         $model_otchet = new ParseOtchet();
                         $model_otchet->parse_ksk_id = $model->id;
                         $model_otchet->name = $name;
                         $model_otchet->url_otchet = $url;
                         $model_otchet->posted_at = date('Y-m-d H:i:s', strtotime($posted_at));
                         $model_otchet->updated_at = new \yii\db\Expression('utc_timestamp()');
                         $model_otchet->save() ? $saved++ : ($errors[] = $model_otchet->errors);
                     }
                 }
             }
             if ($ok < $saved) {
                 $model = ParseKsk::findOne($model->id);
                 $model->updated_at = new \yii\db\Expression('utc_timestamp()');
                 $model->save();
             }
         }
         if ($saved > 0) {
             $region = ParseRegion::findOne($region->id);
             $region->updated_at = new \yii\db\Expression('utc_timestamp()');
             $region->save();
         }
         echo "saved - {$saved}\n";
     }
 }