sveden-parser/app/app.php

125 lines
3.9 KiB
PHP
Raw Normal View History

2024-08-08 12:32:27 +02:00
<?php
namespace App;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\RequestOptions;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use App\Library\ContingentParser;
2024-08-08 12:32:27 +02:00
use App\Library\Database;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
2024-08-08 12:32:27 +02:00
$dbconfig = [
"host" => "10.90.1.201",
"database1" => "opendata",
"database2" => "niimko",
2024-08-08 12:32:27 +02:00
"user" => "niimko_user",
"password" => "MOhA17FeboXE"
];
$dbOpendata = new Database(
"mysql:host={$dbconfig['host']};dbname={$dbconfig['database1']}",
2024-08-08 12:32:27 +02:00
$dbconfig['user'],
$dbconfig['password']
);
$dbNiimko = new Database(
"mysql:host={$dbconfig['host']};dbname={$dbconfig['database2']}",
$dbconfig['user'],
$dbconfig['password']
);
$builder = new GenericBuilder();
// select kod as org_id, site from niimko.s_vuzes
// where ootype = 'vuz' and deleted = 'n' and fake = 'n'
$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);
2024-08-08 12:32:27 +02:00
// select id, kod from niimko.s_specs where oopkodes = 'gos3p'
$params = ['gos3p'];
$query = $builder->select()
->setTable('s_specs')
->setColumns(['id', 'kod'])
->where()
->equals('oopkodes','gos3p')
->end();
$sql = $builder->write($query);
$specializations = $dbNiimko->executeQuery($sql, $params);
// print_r($sites);
// print_r($specializations);
// $sites = [ ['site' => "http://marsu.ru"], ['site' => "http://voenmeh.ru"], ['site' => "http://angtu.ru"] ];
$i = 0;
$succes = 0;
foreach ($sites as $site) {
try {
$client = new Client([
RequestOptions::ALLOW_REDIRECTS => [
'max' => 10, // allow at most 10 redirects.
'strict' => true, // use "strict" RFC compliant redirects.
'referer' => true, // add a Referer header
'track_redirects' => true,
],
]);
$route = "{$site['site']}/sveden/education/";
$route = str_replace("http","https", $route);
$route = str_replace("www.","", $route);
print(++$i.". Current url: $route\n");
$response = $client->get($route);
print("StatusCode: ".$response->getStatusCode() . "\n");
$html = $response->getBody()->getContents();
$parser = new ContingentParser($html, '//tr[@itemprop="eduChislen"]//');
$contingent = $parser->getDataTable();
// Добавляем поле spec_id по spec_code
foreach ($contingent as $key => $con) {
$needle = $con['spec_code'];
foreach ($specializations as $spec) {
if ($needle == $spec['kod']) {
$con['spec_id'] = $spec['id'];
}
}
$contingent[$key]['spec_id'] = $con['spec_id'];
}
// Добавляем поле org_id
foreach ($contingent as $key => $spec) {
$contingent[$key]['org_id'] = $site['org_id'];
}
print_r($contingent);
if ($response->getStatusCode() == 200 && !empty($contingent)){
print("Succes: ".++$succes."\n");
}
} catch (ClientException $e) {
$response = $e->getCode();
} catch (RequestException $e) {
$response = $e->getCode();
} catch (ConnectException $e) {
$response = $e->getCode();
} catch (ServerException $e) {
$response = $e->getCode();
}
}
// Чтобы не дублировались в базе
// $dbOpendata->insert('sveden_education_contingent', $data);
// $dbOpendata->update('sveden_education_contingent', $specializations);
// $data = $dbOpendata->select('sveden_education_contingent');