/** * Import deputies' info */ public function actionInfo() { // Authority dates echo "Authority dates...\n"; $strToTime = function ($str) { list($date, $month, $year) = explode(' ', $str); $date = (int) $date; $month = mb_strtolower(mb_substr($month, 0, 3, 'UTF-8'), 'UTF-8'); switch ($month) { case 'січ': $month = 1; break; case 'лют': $month = 2; break; case 'бер': $month = 3; break; case 'кві': $month = 4; break; case 'тра': $month = 5; break; case 'чер': $month = 6; break; case 'лип': $month = 7; break; case 'сер': $month = 8; break; case 'вер': $month = 9; break; case 'жов': $month = 10; break; case 'лис': $month = 11; break; case 'гру': $month = 12; break; } $year = (int) $year; $date = date_create("{$year}-{$month}-{$date}"); return $date->getTimestamp(); }; foreach (Deputy::find()->all() as $deputy) { echo "{$deputy->id}\n"; $url = "http://itd.rada.gov.ua/mps/info/page/{$deputy->id}"; $content = file_get_contents($url); $html = $this->parser->str_get_html($content); $deputy->dateAuthorityStart = null; $deputy->dateAuthorityStop = null; foreach ($html->find('.mp-general-info dl', 0)->children() as $i => $row) { if (mb_strpos($row->plaintext, 'Дата набуття депутатських повноважень') !== false) { $dateStr = $html->find('.mp-general-info dl', 0)->children($i + 1)->plaintext; $deputy->dateAuthorityStart = date('Y-m-d', $strToTime($dateStr)); } if (mb_strpos($row->plaintext, 'Дата припинення депутатських повноважень') !== false) { $dateStr = $html->find('.mp-general-info dl', 0)->children($i + 1)->plaintext; $deputy->dateAuthorityStop = date('Y-m-d', $strToTime($dateStr)); } } $deputy->save(); } // Get districts echo "Districts...\n"; $content = file_get_contents('http://w1.c1.rada.gov.ua/pls/site2/fetch_mps?skl_id=9&district=y'); $content = iconv('windows-1251', 'utf-8', $content); $html = $this->parser->str_get_html($content); $htmlDeputies = $html->find('.search-filter-results li'); // Save districts foreach ($htmlDeputies as $htmlDeputy) { $name = $htmlDeputy->find('.title a')[0]->plaintext; $deputy = Deputy::findOne(['name' => $name]); if ($deputy !== null) { $districts = $htmlDeputy->find('dl dd'); $districtId = str_replace('Виборчому округу №', '', $districts[0]->plaintext); $districtRegion = $districts[1]->plaintext; $deputy->districtId = $districtId; $deputy->save(); if (!District::findOne(['id' => $districtId])) { $district = new District(); $district->setAttributes(['id' => $districtId, 'region' => $districtRegion], false); $district->save(); } } } }
/** * Finds the District model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return District the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = District::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }