Добавлен цикл проверки сайтов
This commit is contained in:
74
app/app.php
74
app/app.php
@ -1,9 +1,15 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
|
||||
use App\Library\ContingentParser;
|
||||
use App\Library\Database;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
$dbconfig = [
|
||||
"host" => "10.90.1.201",
|
||||
@ -24,25 +30,61 @@ $dbNiimko = new Database(
|
||||
$dbconfig['user'],
|
||||
$dbconfig['password']
|
||||
);
|
||||
$builder = new GenericBuilder();
|
||||
$params = ['vuz', 'n', 'n'];
|
||||
$query = $builder->select()
|
||||
->setTable('s_vuzes')
|
||||
->setColumns(['org_id' => 'kod', 'site'])
|
||||
->where('AND')
|
||||
->equals('ootype', 'vuz')
|
||||
->equals('deleted', 'n')
|
||||
->equals('fake', 'n')
|
||||
->end();
|
||||
$sql = $builder->write($query);
|
||||
$sites = $dbNiimko->executeQuery($sql, $params);
|
||||
|
||||
$client = new Client();
|
||||
$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php');
|
||||
$html = $response->getBody()->getContents();
|
||||
$parser = new ContingentParser($html, '//tr[@itemprop="eduChislen"]//');
|
||||
// print_r($sites);
|
||||
// $sites = [ ['site' => "http://marsu.ru"], ['site' => "http://voenmeh.ru"], ['site' => "http://angtu.ru"] ];
|
||||
$i = 0;
|
||||
foreach ($sites as $site) {
|
||||
try {
|
||||
$client = new Client();
|
||||
|
||||
$contingent = $parser->getDataTable();
|
||||
$specializations = $dbNiimko->selectWhere('s_specs', ['id', 'kod'], ['oopkodes' => 'gos3p']);
|
||||
$route = "{$site['site']}/sveden/education/";
|
||||
$route = str_replace("http","https", $route);
|
||||
$route = str_replace("www.","", $route);
|
||||
print(++$i.". Current url: $route\n");
|
||||
|
||||
foreach ($contingent as $key => $con) {
|
||||
$needle = $con['spec_code'];
|
||||
foreach ($specializations as $spec) {
|
||||
if ($needle == $spec['kod']) {
|
||||
$contingent[$key] += ['spec_id' => $spec['id']];
|
||||
}
|
||||
$response = $client->get($route);
|
||||
print("StatusCode: ".$response->getStatusCode() . "\n");
|
||||
|
||||
$html = $response->getBody()->getContents();
|
||||
$parser = new ContingentParser($html, '//tr[@itemprop="eduChislen"]//');
|
||||
$contingent = $parser->getDataTable();
|
||||
print_r($contingent);
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getCode();
|
||||
} catch (RequestException $e) {
|
||||
$response = $e->getCode();
|
||||
} catch (ConnectException $e) {
|
||||
$response = $e->getCode();
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getCode();
|
||||
}
|
||||
}
|
||||
|
||||
// $specializations = $dbNiimko->selectWhere('s_specs', ['id', 'kod'], ['oopkodes' => 'gos3p']);
|
||||
|
||||
// foreach ($contingent as $key => $con) {
|
||||
// $needle = $con['spec_code'];
|
||||
// foreach ($specializations as $spec) {
|
||||
// if ($needle == $spec['kod']) {
|
||||
// $contingent[$key] += ['spec_id' => $spec['id']];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// Чтобы не дублировались в базе
|
||||
// $dbOpendata->insert('sveden_education_contingent', $data);
|
||||
// $dbOpendata = $db->select('sveden_education_contingent');
|
||||
echo "<pre>";
|
||||
print_r($contingent);
|
||||
echo "</pre>";
|
||||
// $dbOpendata->update('sveden_education_contingent', $specializations);
|
||||
// $data = $dbOpendata->select('sveden_education_contingent');
|
@ -20,7 +20,7 @@ class ContingentParser
|
||||
"numberBMF" => "th",
|
||||
"numberP" => "th",
|
||||
"numberPF" => "th",
|
||||
"numberAll" => "th"
|
||||
"numberAll" => ["th", "td"]
|
||||
];
|
||||
|
||||
public function __construct(string $html, string $template)
|
||||
@ -36,7 +36,14 @@ class ContingentParser
|
||||
{
|
||||
$data = array();
|
||||
foreach (self::FIELDS as $field => $tag) {
|
||||
$data[$field] = $this->xpath->query($this->template . $tag . "[@itemprop=\"$field\"]");
|
||||
if (!is_array($tag)) {
|
||||
$data[$field] = $this->xpath->query($this->template . $tag . "[@itemprop=\"$field\"]");
|
||||
} else {
|
||||
$x = $this->xpath->query($this->template . $tag[0] . "[@itemprop=\"$field\"]");
|
||||
$y = $this->xpath->query($this->template . $tag[1] . "[@itemprop=\"$field\"]");
|
||||
$data[$field] = $x > $y ? $x : $y;
|
||||
}
|
||||
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
@ -46,7 +53,7 @@ class ContingentParser
|
||||
$data = $this->parse();
|
||||
$records = array();
|
||||
|
||||
for ($i = 0; $i < $data['numberAll']->length; $i++) {
|
||||
for ($i = 0; $i < $data['eduCode']->length; $i++) {
|
||||
$contingentRow = new ContingentRow(
|
||||
$data['eduCode']->item($i)->textContent,
|
||||
$data['eduName']->item($i)->textContent,
|
||||
|
@ -1,9 +1,8 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
use PDO;
|
||||
class Database extends PDO
|
||||
{
|
||||
public function __construct(string $dsn, string $username, string $password)
|
||||
@ -27,69 +26,17 @@ class Database extends PDO
|
||||
echo "Подключение прервано!\n";
|
||||
}
|
||||
|
||||
public function insert(string $table, array $data) : void
|
||||
public function executeQuery(string $sql, array $params) : array|bool
|
||||
{
|
||||
$keys = array_keys($data);
|
||||
$sql = "INSERT INTO $table (";
|
||||
foreach ($keys as $key) {
|
||||
$sql .= "$key,";
|
||||
}
|
||||
$sql = substr_replace($sql, ")", -1);
|
||||
$sql .= " VALUES (";
|
||||
foreach ($keys as $key) {
|
||||
$sql .= ":$key,";
|
||||
}
|
||||
$sql = substr_replace($sql,"", -1);
|
||||
$stmt = $this->prepare($sql);
|
||||
|
||||
foreach ($data as $key => $row) {
|
||||
try {
|
||||
$stmt->bindParam(":$key", $row[$key]);
|
||||
$stmt->execute();
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
try {
|
||||
$stmt = $this->prepare($sql);
|
||||
for ($i = 0; $i < count($params); $i++) {
|
||||
$stmt->bindParam(":v".$i+1, $params[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function select(string $table) : array|null
|
||||
{
|
||||
$stmt = $this->prepare("SELECT * FROM $table");
|
||||
try {
|
||||
$stmt->execute();
|
||||
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
} finally {
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
public function selectWhere(
|
||||
string $table,
|
||||
array $atributes,
|
||||
array $filters
|
||||
) : array|null {
|
||||
// Строим запрос
|
||||
$sql = "SELECT ";
|
||||
foreach ($atributes as $atribute) {
|
||||
$sql .= "$atribute,";
|
||||
}
|
||||
$sql = substr_replace($sql," ", -1);
|
||||
$sql .= "FROM $table WHERE ";
|
||||
foreach ($filters as $key => $filter) {
|
||||
$sql .= "$key = :$key ";
|
||||
}
|
||||
$stmt = $this->prepare($sql);
|
||||
foreach ($filters as $key => $filter) {
|
||||
$stmt->bindParam(":$key", $filter);
|
||||
}
|
||||
// Выполняем
|
||||
try {
|
||||
$stmt->execute();
|
||||
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
} finally {
|
||||
return $array;
|
||||
}
|
||||
|
Reference in New Issue
Block a user