Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 1256 runner to not use meetup api to display events #1335

Merged
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"smarty/smarty": "2.6.*",
"sabre/vobject": "^4.1",
"erusev/parsedown": "^1.6",
"dms/meetup-api-client": "^2.3",
"robmorgan/phinx": "^0.9.2",
"presta/sitemap-bundle": "^1.5",
"setasign/fpdf": "1.53",
Expand Down
60 changes: 0 additions & 60 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions sources/AppBundle/Command/IndexMeetupsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace AppBundle\Command;

use AlgoliaSearch\AlgoliaException;
use AlgoliaSearch\Client;
use AppBundle\Event\Model\Repository\MeetupRepository;
use AppBundle\Indexation\Meetups\Runner;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -20,18 +23,19 @@ protected function configure()
}

/**
* @throws AlgoliaException
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$ting = $this->getContainer()->get('ting');

$meetupClient = \DMS\Service\Meetup\MeetupOAuthClient::factory([
'consumer_key' => $container->getParameter('meetup_api_consumer_key'),
'consumer_secret' => $container->getParameter('meetup_api_consumer_secret'),
]);
/** @var Client $algoliaClient */
$algoliaClient = $container->get(Client::class);
$meetupRepository = $ting->get(MeetupRepository::class);

$runner = new Runner($container->get(\AlgoliaSearch\Client::class), $meetupClient);
$runner = new Runner($algoliaClient, $meetupRepository);
$runner->run();
}
}
95 changes: 61 additions & 34 deletions sources/AppBundle/Indexation/Meetups/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace AppBundle\Indexation\Meetups;

use AlgoliaSearch\AlgoliaException;
use AlgoliaSearch\Client;
use AlgoliaSearch\Index;
use AppBundle\Event\Model\Meetup;
use AppBundle\Event\Model\Repository\MeetupRepository;
use AppBundle\Offices\OfficesCollection;
use DMS\Service\Meetup\MeetupOAuthClient;
use CCMBenchmark\Ting\Repository\CollectionInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class Runner
{
Expand All @@ -14,9 +20,9 @@ class Runner
protected $algoliaClient;

/**
* @var MeetupOAuthClient
* @var MeetupRepository
*/
protected $meetupClient;
protected $meetupRepository;

/**
* @var OfficesCollection
Expand All @@ -28,61 +34,54 @@ class Runner
*/
protected $transformer;

public function __construct(Client $algoliaClient, MeetupOAuthClient $meetupClient)
public function __construct(Client $algoliaClient, MeetupRepository $meetupRepository)
{
$this->algoliaClient = $algoliaClient;
$this->meetupClient = $meetupClient;
$this->meetupRepository = $meetupRepository;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pour moi il faut conserver l'appel au transformer. Le format de ce qu'on envoie à Algolia doit toujours être celui-ci :

public function transform(array $meetup)
(mais la source doit être la base de données et plus l'api meetup)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok je modifie ça à l'occasion

Copy link
Contributor Author

@vinceAmstoutz vinceAmstoutz Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est fait j'ai restoré le transformer par contre j'ai des doutes sur le faite qu'il fasse lui passer OfficesCollections à l'instanciation. Le contenu de la méthode transform me parait également peut adapté puis qu'on a un tableau d'objet Meetups désormais à traiter. Comment puis-je tester tout ca @agallou ? En modifiant tests/units/AppBundle/Indexation/Meetups/Transformer.php ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le plus simple que je vois est de se créer un compte de test sur Algolia.
De modifier les paramètres lié à algolia dans le parameters.yml :

algolia_app_id: DVB92YWTPE

Et de relancer indexing:meetups.
Et ensuite aller voir cette page en local : https://afup.org/meetups/
Pour moi le officesCollection peux rester, il permet d'en récupérer les libellés, mais effectivement il faut adapter le transformer pour non plus traiter un tableau qui viens de l'API meetup, mais de la base de données.
(au besoin si tu veux on peux s'appeler si ça peux être plus clair)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je re-regarde ca dès que possible et je te redis

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voilà c'est fait @agallou 🙂
image
image

$this->officiesCollection = new OfficesCollection();
$this->transformer = new Transformer($this->officiesCollection);
}

/**
*
* @throws AlgoliaException
*/
public function run()
{
$index = $this->initIndex();
$command = ['./bin/console', 'scrapping-meetup-event'];
$process = new Process($command);

$command = $this->meetupClient->getCommand(
'GetEvents',
[
'group_id' => implode(',', $this->getGroupIds()),
'status' => 'upcoming,past',
'order' => 'time',
'desc' => 'true',
]
);
try {
$process->start();

$command->prepare();

$meetups = [];
while ($process->isRunning()) {
echo "En cours de scrapping des meetups...\n";
usleep(500000);
}

foreach ($command->execute() as $meetup) {
if (null === ($transformedMeetup = $this->transformer->transform($meetup))) {
continue;
echo $process->getOutput();
} catch (ProcessFailedException $exception) {
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$meetups[] = $transformedMeetup;
}

echo "Indexation des meetups en cours ...\n\n";


$meetups = $this->getTransformedMeetupsFromDatabase();


$index->clearIndex();
$index->addObjects($meetups, 'meetup_id');
}

protected function getGroupIds()
{
$groupIds = [];
foreach ($this->officiesCollection->getAll() as $office) {
if (!isset($office['meetup_id'])) {
continue;
}
$groupIds[] = $office['meetup_id'];
}

return $groupIds;
echo "Indexation des meetups terminée avec succès !\n";
}

/**
* @return \AlgoliaSearch\Index
* @return Index
* @throws AlgoliaException
*/
protected function initIndex()
{
Expand All @@ -106,4 +105,32 @@ protected function initIndex()

return $index;
}

/**
* @return array
*/
private function getTransformedMeetupsFromDatabase()
{
$meetupsCollection = $this->meetupRepository->getAll();

return $this->transformMeetupsForIndexation($meetupsCollection);
}

/**
* @param CollectionInterface $meetupsCollection
* @return array<Meetup>
*/
public function transformMeetupsForIndexation($meetupsCollection)
{
$meetupsArray = [];
/** @var Meetup $meetup */
foreach ($meetupsCollection as $meetup) {
if (null === ($transformedMeetup = $this->transformer->transform($meetup))) {
continue;
}
$meetupsArray[] = $transformedMeetup;
}

return $meetupsArray;
}
}
46 changes: 27 additions & 19 deletions sources/AppBundle/Indexation/Meetups/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace AppBundle\Indexation\Meetups;

use AppBundle\Event\Model\Meetup;
use AppBundle\Offices\OfficesCollection;
use Exception;

class Transformer
{
const MEETUP_URL = 'https://www.meetup.com/fr-FR/';

/**
* @var OfficesCollection
*/
Expand All @@ -20,17 +24,18 @@ public function __construct(OfficesCollection $officesCollection)
}

/**
* @param array $meetup
* @param Meetup $meetup
*
* @return array
* @throws Exception
*/
public function transform(array $meetup)
public function transform(Meetup $meetup)
{
$office = $this->officesCollection->findByMeetupId($meetup['group']['id']);

$datetime = new \DateTime('@' . ($meetup['time'] / 1000));
$codeOffice = $meetup->getAntenneName();
$office = $this->officesCollection->findByCode($codeOffice);
$datetime = $meetup->getDate();

$isUpcoming = $meetup['status'] == 'upcoming';
$isUpcoming = new \DateTime() < $datetime;

if (isset($office['meetup_filter'])) {
$matches = [];
Expand All @@ -41,10 +46,11 @@ public function transform(array $meetup)
$meetup['name'] = $matches[1];
}

$eventUrl = $this->getEventUrl($office, $meetup);
$item = [
'meetup_id' => $meetup['id'],
'label' => $meetup['name'],
'event_url' => $meetup['event_url'],
'meetup_id' => $meetup->getId(),
'label' => $meetup->getTitle(),
'event_url' => $eventUrl,
'timestamp' => $datetime->format('U'),
'year' => $datetime->format('Y'),
'datetime' => $datetime->format('Y-m-d H:i:s'),
Expand All @@ -53,23 +59,25 @@ public function transform(array $meetup)
'label' => $office['label'],
'logo_url' => $office['logo_url'],
],
'description' => $meetup['description'],
'description' => $meetup->getDescription(),
'is_upcoming' => $isUpcoming,
'custom_sort' => $isUpcoming ? PHP_INT_MAX - $meetup['time'] : $meetup['time'],
'custom_sort' => $isUpcoming ? PHP_INT_MAX - $meetup->getDate()->getTimestamp() : $meetup->getDate()->getTimestamp(),
];

if (isset($office['twitter'])) {
$item['twitter'] = $office['twitter'];
}

if (isset($meetup['venue'])) {
$item['venue'] = [
'name' => $meetup['venue']['name'],
'address_1' => $meetup['venue']['address_1'],
'city' => $meetup['venue']['city'],
];
}

return $item;
}

/**
* @param array $office
* @param Meetup $meetup
* @return string
*/
public function getEventUrl($office, Meetup $meetup)
{
return self::MEETUP_URL . $office['meetup_urlname'] . '/events/' . $meetup->getId();
}
}
Loading
Loading