Création d'une commande faisant appel au service RPA Scrapper
Créer une commande qui appelera le service Scrapper RPA en prenant pour exemple le script suivant :
<?php
namespace App\Command;
use App\Entity\External\Commune;
use App\Entity\External\Structure;
use Doctrine\ORM\EntityManagerInterface;
use Quantum\RPA\Service\Scraper\Business\External\StructureScraper;
use Quantum\RPA\Service\Scraper\Scraper;
use Quantum\RPA\Service\Scraper\ScraperConstraint;
use Quantum\RPA\Service\Scraper\SeleniumChromeClient;
use Quantum\RPA\Service\SimpleHtml\SimpleHtml;
use Quantum\Geomatic\Repository\CityRepository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DomCrawler\Crawler as DomCrawler;
#[AsCommand(
name: 'app:import:structure-spip',
description: 'Import spécial dédicace à Delphine pour monsuivijustice.fr',
)]
class ImportStructureSPIPCommand extends Command
{
/** @var NatinfScraper */
private $_scraper;
/** @var CityRepository */
private $_cityRepository;
private $_em;
public function __construct(
StructureScraper $scraper,
CityRepository $cityRepository,
EntityManagerInterface $em
)
{
$this->_cityRepository = $cityRepository;
$this->_scraper = $scraper;
$this->_em = $em;
parent::__construct();
}
protected function configure(): void
{
$this
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
/**
* DESACTIVATION DES LOGS
*
*/
/** @var EntityManagerInterface $em */
$em = $this->_em;
/** @var Connection $conn */
$conn = $em->getConnection();
$conn->getConfiguration()->setSQLLogger(null);
/** @var CommuneRepository $cr */
$cr = $em->getRepository(Commune::class);
/** @var StructureRepository $sr */
$sr = $em->getRepository(Structure::class);
$io = new SymfonyStyle($input, $output);
$scraper = $this->_scraper;
$cityRepository = $this->_cityRepository;
$isInit = false;
$limit = 100;
$page = 1;
$nbTotal = 0;
$nbSuccess = 0;
$nbCreated = 0;
$nbFailed = 0;
do
{
$cities = $cityRepository->findAllByLocale("FR",$limit,$page);
foreach($cities as $city)
{
$nbTotal++;
foreach($city as $param => $value)
$$param=$value;
if(null === $geoname_admin4_code)
continue;
if(null === $gadm_id)
continue;
if(null === ($commune = $cr->findOneBy(['cityId' => $city_id])))
{
$commune = new Commune();
$commune->setCityId($city_id);
$commune->setGeonameId($geoname_id);
$commune->setGadmId($gadm_id);
$commune->setNames($name);
$commune->setAsciiName($geoname_ascii_name);
$commune->setPostalCode($postal_code);
$commune->setInseeCode($geoname_admin4_code);
$em->persist($commune);
$em->flush();
}
elseif(true === $commune->isAnalyzed())
{
continue;
}
if(false === $isInit) {
$scraper->start();
$isInit = true;
}
$fulltext = mb_strtoupper(str_replace(["-","'"], [" "," "],$geoname_ascii_name));
$targetDept = preg_replace("/^(\d{2})(.*)$/","$1",$postal_code);
$resultOfCities = $scraper
->searchByCity($fulltext)
;
$lId = null;
foreach($resultOfCities['items'] as $item)
{
$label = trim(preg_replace("/^(\d+)[ ]+(.+)[(](.*)$/","$2",$item['label']));
$dept = preg_replace("/^(\d{2})(.*)$/","$1",$item['label']);
if(
(
($fulltext === $label)
&&
($targetDept === $dept)
)
||
(count($resultOfCities['items']) === 1)
||
preg_match("/$postal_code/",$item['label'])
)
$lId = $item['id'];
}
$details = [];
if(null !== $lId)
{
$details = $scraper->getDetailsById($lId);
$nbSuccess++;
}
else
$nbFailed++;
if(count($details))
{
foreach($details as $detailStructure)
{
if(null === ($structure = $sr->findOneBy(['name' => $detailStructure['name']])))
{
$structure = new Structure();
$structure->setName($detailStructure['name']);
$structure->setAddress($detailStructure['address']);
$structure->setPhone($detailStructure['phone']);
$em->persist($structure);
}
$structure->addCommune($commune);
}
unset($structure);
}
$commune->setIsAnalyzed(true);
$em->flush();
unset($commune);
}
if(true === $isInit) {
$scraper->getClient()->quit();
$scraper->start();
}
dump("RESULTAT:$nbTotal/$nbSuccess/$nbCreated/$nbFailed");
$em->clear(Structure::class);
$em->clear(Commune::class);
$page++;
}
while(count($cities) === $limit);
return Command::SUCCESS;
}
}