Skip to content

Commit

Permalink
feat: switch from controller endpoint to sf command
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Amstoutz committed Sep 17, 2023
1 parent 7edac11 commit 11876bc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 44 deletions.
6 changes: 1 addition & 5 deletions app/config/routing/meetups.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
99 changes: 99 additions & 0 deletions sources/AppBundle/Command/ScrappingMeetupEventsCommand.php
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;
}
}
39 changes: 0 additions & 39 deletions sources/AppBundle/Controller/MeetupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
}

0 comments on commit 11876bc

Please sign in to comment.