diff --git a/app/config/routing/meetups.yml b/app/config/routing/meetups.yml index c82de4ca3..63c1b69da 100644 --- a/app/config/routing/meetups.yml +++ b/app/config/routing/meetups.yml @@ -2,8 +2,4 @@ meetups_list: path: / defaults: {_controller: AppBundle:Meetups:list} options: - sitemap: true - -meetups_scrapper: - path: /scrapping/ - defaults: {_controller: AppBundle:Meetups:scrapMeetup} + sitemap: true \ No newline at end of file diff --git a/sources/AppBundle/Command/ScrappingMeetupEventsCommand.php b/sources/AppBundle/Command/ScrappingMeetupEventsCommand.php new file mode 100644 index 000000000..217d29ca7 --- /dev/null +++ b/sources/AppBundle/Command/ScrappingMeetupEventsCommand.php @@ -0,0 +1,99 @@ +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; + } +} diff --git a/sources/AppBundle/Controller/MeetupsController.php b/sources/AppBundle/Controller/MeetupsController.php index 0eba5ad02..37e1ff205 100644 --- a/sources/AppBundle/Controller/MeetupsController.php +++ b/sources/AppBundle/Controller/MeetupsController.php @@ -2,11 +2,7 @@ namespace AppBundle\Controller; -use AppBundle\Event\Model\Repository\MeetupRepository; -use AppBundle\Indexation\Meetups\MeetupScraper; -use Exception; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; class MeetupsController extends SiteBaseController { @@ -21,39 +17,4 @@ public function listAction(Request $request) ] ); } - - /** - * @throws Exception - * - * @return Response - */ - public function scrapMeetupAction() - { - try { - $meetups = $this->getScrapper()->getEvents(); - - $meetupRepository = $this->get('ting')->get(MeetupRepository::class); - - foreach ($meetups as $antenneMeetups) { - foreach ($antenneMeetups as $meetup) { - $existingMeetup = $meetupRepository->get($meetup->getId()); - if (!$existingMeetup) { - $meetupRepository->save($meetup); - } - } - } - //TODO: à laisser ? - return new Response('Meetups scraped and saved successfully!'); - } catch (\Exception $e) { - throw new \Exception('Problème lors du scraping ou de la sauvegarde des évènements Meetup', $e->getCode(), $e); - } - } - - /** - * @return MeetupScraper - */ - private function getScrapper() - { - return new MeetupScraper(); - } }