Skip to content

Commit

Permalink
Merge pull request #1335 from vinceAmstoutz/feat-1256-runner-to-not-u…
Browse files Browse the repository at this point in the history
…se-meetup-api-to-display-events

Feat 1256 runner to not use meetup api to display events
  • Loading branch information
agallou authored Nov 26, 2023
2 parents 1c9e269 + bd48142 commit 871b2b2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 183 deletions.
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;
$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

0 comments on commit 871b2b2

Please sign in to comment.