-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch from controller endpoint to sf command
- Loading branch information
Vincent Amstoutz
committed
Sep 17, 2023
1 parent
7edac11
commit 11876bc
Showing
3 changed files
with
100 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
sources/AppBundle/Command/ScrappingMeetupEventsCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace AppBundle\Command; | ||
|
||
use AppBundle\Event\Model\Repository\MeetupRepository; | ||
use AppBundle\Indexation\Meetups\MeetupScraper; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Command\LockableTrait; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
class ScrappingMeetupEventsCommand extends ContainerAwareCommand | ||
{ | ||
use LockableTrait; | ||
|
||
/** | ||
* @see Command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('scrapping-meetup-event') | ||
->setAliases(['s-m-e']) | ||
->setDescription('Récupère les évènements meetup AFUP sur meetup.com pour les afficher sur le site de l\'afup.') | ||
; | ||
} | ||
|
||
/** | ||
* | ||
* @see Command | ||
* | ||
* @throws \Exception | ||
* @return int | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->title('Import des events meetups via scrapping de meetup.com'); | ||
|
||
if (!$this->lock()) { | ||
$io->warning('La commande est déjà en cours d\'exécution dans un autre processus.'); | ||
|
||
return 0; | ||
} | ||
|
||
try { | ||
$ting = $this->getContainer()->get('ting'); | ||
$meetupScraper = new MeetupScraper(); | ||
$meetups = $meetupScraper->getEvents(); | ||
|
||
$meetupRepository = $ting->get(MeetupRepository::class); | ||
|
||
$emlementsLength = $this->countAllNestedElements($meetups); | ||
$io->progressStart($emlementsLength); | ||
foreach ($meetups as $antenneMeetups) { | ||
foreach ($antenneMeetups as $meetup) { | ||
$io->progressAdvance(); | ||
|
||
$id =$meetup->getId(); | ||
$existingMeetup = $meetupRepository->get($id); | ||
if (!$existingMeetup) { | ||
$meetupRepository->save($meetup); | ||
} else { | ||
$io->note(sprintf('Meetup id %d déjà en base.', $id)); | ||
} | ||
} | ||
} | ||
$io->progressFinish(); | ||
$io->success('Terminé avec succès'); | ||
return 1; | ||
} catch (\Exception $e) { | ||
throw new \Exception('Problème lors du scraping ou de la sauvegarde des évènements Meetup', $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* Permet de faire un count sur un tableau multi-dimensionnel | ||
* | ||
* @param $array | ||
* | ||
* @return int | ||
*/ | ||
private function countAllNestedElements($array) | ||
{ | ||
$count = 0; | ||
|
||
foreach ($array as $element) { | ||
if (is_array($element)) { | ||
// Si l'élément est un tableau, on appelle récursivement la fonction | ||
$count += $this->countAllNestedElements($element); | ||
} else { | ||
$count++; | ||
} | ||
} | ||
|
||
return $count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters