Добавлено логирование и исправлены некоторые ошибки в классах
This commit is contained in:
96
app/app.php
96
app/app.php
@ -11,20 +11,60 @@ use GuzzleHttp\RequestOptions;
|
||||
use App\Library\ContingentParser;
|
||||
use App\Library\Database;
|
||||
use GuzzleHttp\Client;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
$dbOpendata = new Database(new DatabaseConfig('opendata'));
|
||||
$dbNiimko = new Database(new DatabaseConfig('niimko'));
|
||||
|
||||
$sites = ContingentManager::getInstance()->getSites($dbNiimko);
|
||||
$specializations = ContingentManager::getInstance()->getSpecializations($dbNiimko);
|
||||
$sql = 'SELECT DISTINCT org_id FROM sveden_education_contingent';
|
||||
$org = $dbOpendata->selectQuery($sql);
|
||||
// print_r($sites);
|
||||
// print_r($specializations);
|
||||
// print_r($org);
|
||||
$orgs = [];
|
||||
foreach ($org as $o) {
|
||||
$orgs[] = $o['org_id'];
|
||||
}
|
||||
unset($org);
|
||||
|
||||
$errorSites = [];
|
||||
$filename = 'error-html.log';
|
||||
$array = file($filename);
|
||||
for ($i = 0; $i < count($array); $i++) {
|
||||
$arr = explode(' ', $array[$i]);
|
||||
if (!in_array($arr[2], $orgs)) {
|
||||
$errorSites[] = $arr[2];
|
||||
}
|
||||
}
|
||||
$filename = 'error-http.log';
|
||||
$array = file($filename);
|
||||
for ($i = 0; $i < count($array); $i++) {
|
||||
$arr = explode(' ', $array[$i]);
|
||||
if (!in_array($arr[2], $orgs)) {
|
||||
$errorSites[] = $arr[2];
|
||||
}
|
||||
}
|
||||
|
||||
$status = null;
|
||||
$succes = 0;
|
||||
$failed = array();
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
|
||||
$start = 600;
|
||||
|
||||
for ($i = $start; $i < count($sites); $i++) {
|
||||
// Нет URL сайта вуза
|
||||
if (empty($sites[$i]['site'])) {
|
||||
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
|
||||
file_put_contents(__DIR__ . '/../error-http.log', $log . PHP_EOL, FILE_APPEND);
|
||||
continue;
|
||||
}
|
||||
// Уже в базе
|
||||
if (in_array($sites[$i]['org_id'], $orgs)) {
|
||||
continue;
|
||||
}
|
||||
// С ошибками разметки игнорируем
|
||||
if (in_array($sites[$i]['org_id'], $errorSites)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$client = new Client([
|
||||
RequestOptions::ALLOW_REDIRECTS => [
|
||||
@ -32,10 +72,15 @@ for ($i = 0; $i < 100; $i++) {
|
||||
'strict' => true,
|
||||
'referer' => true,
|
||||
'track_redirects' => true,
|
||||
'headers' => [
|
||||
'content-type' => 'text/html;',
|
||||
'charset' => 'utf-8'
|
||||
]
|
||||
]
|
||||
]);
|
||||
$baseURL = $sites[$i]['site'];
|
||||
$url = ContingentManager::getInstance()->buildURL($baseURL);
|
||||
|
||||
$url = $sites[$i]['site'];
|
||||
$url = ContingentManager::getInstance()->buildURL($url);
|
||||
print(($i+1).". Current url: $url\n");
|
||||
|
||||
$response = $client->get($url, ['timeout' => 300]);
|
||||
@ -50,31 +95,32 @@ for ($i = 0; $i < 100; $i++) {
|
||||
|
||||
// Добавляем поле org_id
|
||||
ContingentManager::getInstance()->addOrgId($contingent, $sites[$i]['org_id']);
|
||||
|
||||
print_r($contingent);
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getCode();
|
||||
$status = 0;
|
||||
} catch (RequestException $e) {
|
||||
$response = $e->getCode();
|
||||
$status = 0;
|
||||
} catch (ConnectException $e) {
|
||||
$response = $e->getCode();
|
||||
$status = 0;
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getCode();
|
||||
$status = 0;
|
||||
} finally {
|
||||
if ($status == 200 && !empty($contingent)) {
|
||||
$status = 0;
|
||||
print("Succes: ".++$succes."\n");
|
||||
// TODO - здесь заносим в базу
|
||||
if ($status != 200) {
|
||||
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
|
||||
file_put_contents(__DIR__ . '/../error-http.log', $log . PHP_EOL, FILE_APPEND);
|
||||
} else if (empty($contingent)) {
|
||||
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
|
||||
file_put_contents(__DIR__ . '/../error-html.log', $log . PHP_EOL, FILE_APPEND);
|
||||
} else {
|
||||
// Сайты, которые распарсить не удолось
|
||||
$failed[] = $sites[$i];
|
||||
$set = ContingentManager::getInstance()->checkContingent($contingent);
|
||||
if ($set) {
|
||||
// Заносим в базу
|
||||
ContingentManager::getInstance()->insertContingent($dbOpendata, $contingent);
|
||||
} else {
|
||||
$log = date('Y-m-d H:i:s') . ' ' . $sites[$i]['org_id'] . ' ' . $sites[$i]['site'];
|
||||
file_put_contents(__DIR__ . '/../error-html.log', $log . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
unset($contingent);
|
||||
}
|
||||
}
|
||||
}
|
||||
$yaml = Yaml::dump($failed);
|
||||
file_put_contents(__DIR__ . '/../failed.yaml', $yaml);
|
||||
|
||||
// Чтобы не дублировались в базе
|
||||
// $dbOpendata->insert('sveden_education_contingent', $data);
|
||||
// $dbOpendata->update('sveden_education_contingent', $specializations);
|
||||
// $data = $dbOpendata->select('sveden_education_contingent');
|
||||
}
|
65
app/database.log
Normal file
65
app/database.log
Normal file
@ -0,0 +1,65 @@
|
||||
2024-08-16 10:13:53 Подлючение к успешно!
|
||||
2024-08-16 10:13:53 Подлючение к успешно!
|
||||
2024-08-16 10:13:53 Подлючение прервано!
|
||||
2024-08-16 10:13:53 Подлючение прервано!
|
||||
2024-08-16 10:18:33 Подлючение к успешно!
|
||||
2024-08-16 10:18:33 Подлючение к успешно!
|
||||
2024-08-16 10:21:06 Подлючение к успешно!
|
||||
2024-08-16 10:21:06 Подлючение к успешно!
|
||||
2024-08-16 10:21:15 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:23 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:24 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:38 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:49 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:50 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:52 Запрос выполнен успешно!
|
||||
2024-08-16 10:21:53 Запрос выполнен успешно!
|
||||
2024-08-16 10:22:28 Запрос выполнен успешно!
|
||||
2024-08-16 10:23:54 Подлючение к успешно!
|
||||
2024-08-16 10:23:54 Подлючение к успешно!
|
||||
2024-08-16 10:23:59 Запрос выполнен успешно!
|
||||
2024-08-16 10:24:06 Запрос выполнен успешно!
|
||||
2024-08-16 10:24:07 Запрос выполнен успешно!
|
||||
2024-08-16 10:25:47 Запрос выполнен успешно!
|
||||
2024-08-16 10:27:19 Запрос выполнен успешно!
|
||||
2024-08-16 10:27:22 Запрос выполнен успешно!
|
||||
2024-08-16 10:29:41 Запрос выполнен успешно!
|
||||
2024-08-16 10:29:44 Запрос выполнен успешно!
|
||||
2024-08-16 10:29:48 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:09 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:11 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:20 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:24 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:25 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:29 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:33 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:41 Запрос выполнен успешно!
|
||||
2024-08-16 10:32:58 Запрос выполнен успешно!
|
||||
2024-08-16 10:33:08 Запрос выполнен успешно!
|
||||
2024-08-16 10:33:10 Запрос выполнен успешно!
|
||||
2024-08-16 10:33:11 Запрос выполнен успешно!
|
||||
2024-08-16 10:33:43 Запрос выполнен успешно!
|
||||
2024-08-16 10:33:46 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:04 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:12 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:14 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:16 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:19 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:44 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:51 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:53 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:55 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:57 Запрос выполнен успешно!
|
||||
2024-08-16 10:36:59 Запрос выполнен успешно!
|
||||
2024-08-16 10:37:01 Запрос выполнен успешно!
|
||||
2024-08-16 10:37:06 Запрос выполнен успешно!
|
||||
2024-08-16 10:37:07 Запрос выполнен успешно!
|
||||
2024-08-16 10:37:08 Запрос выполнен успешно!
|
||||
2024-08-16 10:37:11 Запрос выполнен успешно!
|
||||
2024-08-16 10:37:15 Запрос выполнен успешно!
|
||||
2024-08-16 10:38:04 Запрос выполнен успешно!
|
||||
2024-08-16 10:38:07 Запрос выполнен успешно!
|
||||
2024-08-16 10:38:09 Запрос выполнен успешно!
|
||||
2024-08-16 10:38:13 Запрос выполнен успешно!
|
||||
2024-08-16 10:38:13 Подлючение прервано!
|
||||
2024-08-16 10:38:13 Подлючение прервано!
|
@ -31,11 +31,28 @@ final class ContingentManager
|
||||
->equals('fake', 'n')
|
||||
->end();
|
||||
$sql = $this->builder->write($query);
|
||||
$sites = $db->executeQuery($sql, $params);
|
||||
$sites = $db->selectQuery($sql, $params);
|
||||
|
||||
return $sites;
|
||||
}
|
||||
|
||||
public function insertContingent(Database $db, array $contingent) : void
|
||||
{
|
||||
$params = ['spec_code', 'spec_name', 'edu_level', 'edu_forms', 'contingent', 'spec_id', 'org_id'];
|
||||
$sql = "insert into sveden_education_contingent"
|
||||
."(". implode(',', $params) .") values";
|
||||
for ($i = 0; $i < count($contingent); $i++) {
|
||||
$sql .= "(";
|
||||
foreach ($contingent[$i] as $key => $value) {
|
||||
$sql .= ":$key". ($i+1).",";
|
||||
}
|
||||
$sql = substr_replace($sql,"),", -1);
|
||||
}
|
||||
$sql = substr_replace($sql,"", -1);
|
||||
|
||||
$db->insertQuery($sql, $contingent);
|
||||
}
|
||||
|
||||
public function getSpecializations(Database $db) : array
|
||||
{
|
||||
// select id, kod from niimko.s_specs where oopkodes = 'gos3p'
|
||||
@ -47,14 +64,19 @@ final class ContingentManager
|
||||
->equals('oopkodes','gos3p')
|
||||
->end();
|
||||
$sql = $this->builder->write($query);
|
||||
$specializations = $db->executeQuery($sql, $params);
|
||||
$specializations = $db->selectQuery($sql, $params);
|
||||
|
||||
return $specializations;
|
||||
}
|
||||
|
||||
public function buildURL(string $url): string
|
||||
{
|
||||
// TODO - сделать base_url
|
||||
// Строит -> https://<base_uri>/sveden/education/
|
||||
$offset = strpos($url, '/', strlen('http://'));
|
||||
if ($offset) {
|
||||
$url = substr_replace($url, '', $offset);
|
||||
}
|
||||
|
||||
$url = "$url/sveden/education/";
|
||||
if (str_contains($url, "http://")) {
|
||||
$url = str_replace("http://","https://", $url);
|
||||
@ -69,13 +91,15 @@ final class ContingentManager
|
||||
public function addSpecId(array &$contingent, array $specializations) : void
|
||||
{
|
||||
foreach ($contingent as $key => $con) {
|
||||
$buf = null;
|
||||
$needle = $con['spec_code'];
|
||||
foreach ($specializations as $spec) {
|
||||
if ($needle == $spec['kod']) {
|
||||
$cont['spec_id'] = $spec['id'];
|
||||
$buf = $spec['id'];
|
||||
}
|
||||
}
|
||||
$contingent[$key]['spec_id'] = $cont['spec_id'];
|
||||
$contingent[$key]['spec_id'] = $buf;
|
||||
unset($buf);
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,4 +109,13 @@ final class ContingentManager
|
||||
$contingent[$i]['org_id'] = $orgId;
|
||||
}
|
||||
}
|
||||
|
||||
public function checkContingent(array $contingent) : bool
|
||||
{
|
||||
$count = 0;
|
||||
foreach ($contingent as $value) {
|
||||
$count += $value['contingent'];
|
||||
}
|
||||
return $count ? true : false;
|
||||
}
|
||||
}
|
@ -12,14 +12,14 @@ class ContingentParser
|
||||
"eduName" => "td",
|
||||
"eduLevel" => "td",
|
||||
"eduForm" => "td",
|
||||
"numberBF" => "th",
|
||||
"numberBFF" => "th",
|
||||
"numberBR" => "th",
|
||||
"numberBRF" => "th",
|
||||
"numberBM" => "th",
|
||||
"numberBMF" => "th",
|
||||
"numberP" => "th",
|
||||
"numberPF" => "th",
|
||||
// "numberBF" => "th",
|
||||
// "numberBFF" => "th",
|
||||
// "numberBR" => "th",
|
||||
// "numberBRF" => "th",
|
||||
// "numberBM" => "th",
|
||||
// "numberBMF" => "th",
|
||||
// "numberP" => "th",
|
||||
// "numberPF" => "th",
|
||||
"numberAll" => ["th", "td"]
|
||||
];
|
||||
|
||||
@ -41,7 +41,7 @@ class ContingentParser
|
||||
} else {
|
||||
$th = $this->xpath->query($this->template . $tag[0] . "[@itemprop=\"$field\"]");
|
||||
$td = $this->xpath->query($this->template . $tag[1] . "[@itemprop=\"$field\"]");
|
||||
$data[$field] = $th > $td ? $th : $td;
|
||||
$data[$field] = $th->length > $td->length ? $th : $td;
|
||||
}
|
||||
|
||||
}
|
||||
@ -53,6 +53,19 @@ class ContingentParser
|
||||
$data = $this->parse();
|
||||
$records = array();
|
||||
|
||||
// var_dump($data['eduName']->item(0));
|
||||
// exit(0);
|
||||
|
||||
$equel = $data['eduName']->length;
|
||||
foreach ($data as $field) {
|
||||
if ($field->length == 0) {
|
||||
return [];
|
||||
}
|
||||
if ($field->length != $equel) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $data['eduCode']->length; $i++) {
|
||||
$contingentRow = new ContingentRow(
|
||||
$data['eduCode']->item($i)->textContent,
|
||||
|
@ -19,18 +19,21 @@ class Database
|
||||
$password,
|
||||
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
|
||||
);
|
||||
print("Подлючено успешно!\n");
|
||||
$log = date('Y-m-d H:i:s') . ' Подлючение к успешно!';
|
||||
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка подключения:". $e->getMessage() . "\n";
|
||||
$log = date('Y-m-d H:i:s') . " Ошибка подключения:" . $e->getMessage();
|
||||
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
echo "Подключение прервано!\n";
|
||||
$log = date('Y-m-d H:i:s') . ' Подлючение прервано!';
|
||||
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
|
||||
public function executeQuery(string $sql, array $params) : array|null
|
||||
public function selectQuery(string $sql, array $params = []) : array
|
||||
{
|
||||
try {
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
@ -41,9 +44,32 @@ class Database
|
||||
$stmt->execute();
|
||||
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
$log = date('Y-m-d H:i:s') . " Ошибка запроса:" . $e->getMessage();
|
||||
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
|
||||
} finally {
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
public function insertQuery(string $sql, array $params)
|
||||
{
|
||||
try {
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
for ($i = 0; $i < count($params); $i++) {
|
||||
$stmt->bindParam(":spec_code".$i+1, $params[$i]['spec_code']);
|
||||
$stmt->bindParam(":spec_name".$i+1, $params[$i]['spec_name']);
|
||||
$stmt->bindParam(":edu_forms".$i+1, $params[$i]['edu_forms']);
|
||||
$stmt->bindParam(":edu_level".$i+1, $params[$i]['edu_level']);
|
||||
$stmt->bindParam(":contingent".$i+1, $params[$i]['contingent']);
|
||||
$stmt->bindParam(":org_id".$i+1, $params[$i]['org_id']);
|
||||
$stmt->bindParam(":spec_id".$i+1, $params[$i]['spec_id']);
|
||||
}
|
||||
$stmt->execute();
|
||||
$log = date('Y-m-d H:i:s') . " Запрос выполнен успешно!";
|
||||
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
|
||||
} catch (PDOException $e) {
|
||||
$log = date('Y-m-d H:i:s') . " Ошибка запроса:" . $e->getMessage();
|
||||
file_put_contents(__DIR__ . '/../database.log', $log . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user