Commit 02d02997 by san58

переработал парс csv

1 parent d8a8ed2c
...@@ -39,34 +39,77 @@ class ScanController extends Controller ...@@ -39,34 +39,77 @@ class ScanController extends Controller
*/ */
public function actionIndex() public function actionIndex()
{ {
$hosts = Collection::getHostsByCsv(Yii::$app->params['csv_url']); $rows = 0;
$csv_date = date("Y-m-d H:i:s",strtotime($hosts['csv_date'])); $csv_file = Collection::download_file(Yii::$app->params['csv_url']);
foreach ($hosts['data'] as $host) if ($csv_file!=='')
{ {
$host_utf8 = utf8_encode($host); $csv_handle = fopen($csv_file ,'r');
$res = Yii::$app->db->createCommand(); if ($csv_handle !== false)
if ( Host::find()->where(['domain' =>$host_utf8])->count() ) {
$res->update( $csv_row = 0;
$csv_datatime = '';
while (($csv_data = fgetcsv($csv_handle, 1024, ';')) !== false)
{
$csv_row++;
if ($csv_row===1)
$csv_datatime = date(
"Y-m-d H:i:s",
strtotime(
str_replace('Updated: ', '', $csv_data[0])
)
);
elseif (isset($csv_data[1])===true)
{
$host = str_replace(
'*.',
'',
utf8_encode($csv_data[1])
);
$host_level = count( explode('.', $host) );
if ($host_level === 2)
{
$db_request = Yii::$app->db->createCommand();
if ($db_request)
{
if ( Host::find()->where(['domain' =>$host])->count()>0 )
$db_request->update(
'{{%host}}', '{{%host}}',
[ [
'csv_date' => $csv_date], 'csv_date' => $csv_datatime],
[ [
'domain' => $host_utf8 'domain' => $host
] ]
); );
else else
$res->insert( $db_request->insert(
'{{%host}}', '{{%host}}',
[ [
'domain' =>$host_utf8, 'domain' =>$host,
'created_at' => new Expression('NOW()'), 'created_at' => new Expression('NOW()'),
'csv_date' => $csv_date, 'csv_date' => $csv_datatime,
] ]
); );
$res->execute(); $db_request->execute();
$rows++;
}
unset($db_request);
} }
echo "\n".$this->ansiFormat('Количество полученных хостов', Console::FG_CYAN) . ' => ' . $this->ansiFormat(count($hosts['data']), Console::BOLD, Console::FG_GREEN) . "\n"; unset($host_level);
unset($host);
}
}
unset($csv_data);
unset($csv_datatime);
unset($csv_row);
fclose($csv_handle);
unlink($csv_file);
}
unset($csv_handle);
}
unset($csv_file);
echo "\n".$this->ansiFormat('Count of hosts processed: ', Console::FG_CYAN) . ' => ' . $this->ansiFormat($rows, Console::BOLD, Console::FG_GREEN) . "\n";
unset($rows);
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -7,32 +7,25 @@ use yii\base\Component; ...@@ -7,32 +7,25 @@ use yii\base\Component;
class Collection extends Component class Collection extends Component
{ {
public static function getHostsByCsv($csvFile, $separator = ";", $seporatorHosts = '|', $pos = 1, $unique = true) //Скачиваине URL файла и передача пути к нему
public static function download_file($csvFile)
{ {
$date = ''; $out = '';
$hosts = []; $temp_csv_name = tempnam(sys_get_temp_dir(), 'rkn_');
$row = 1; if ($temp_csv_name !== false)
if (($handle = fopen($csvFile, "r")) !== FALSE) { {
while (($data = fgetcsv($handle, 1000, $separator)) !== FALSE) { $file_handle = fopen((string)$csvFile, 'r');
if ($row > 1) { if ($file_handle!==false)
$hostsString = $data[$pos] ?? ''; {
if ($hostsString) { $file_write = file_put_contents($temp_csv_name, $file_handle);
//$arr = array_map('trim', explode($seporatorHosts, $hostsString)); if ($file_write!==false)
//$hosts = array_merge($hosts, $arr); $out = $temp_csv_name;
$hosts[] = str_replace('*.', '', $hostsString); unset($file_write);
}
} else {
$date = $data[0];
}
$row++;
}
fclose($handle);
} }
if ($unique && $hosts) { unset($file_handle);
$hosts = array_unique($hosts);
} }
unset($temp_csv_name);
return ['csv_date' => str_replace('Updated: ', '', $date), 'data' => $hosts]; return $out;
} }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!