|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Fop\MeetupCom\Command; |
| 4 | + |
| 5 | +use Fop\Entity\Group; |
| 6 | +use Fop\MeetupCom\Filter\PhpRelatedFilter; |
| 7 | +use Fop\Repository\GroupRepository; |
| 8 | +use Fop\Utils\Arrays; |
| 9 | +use Nette\Utils\FileSystem; |
| 10 | +use Nette\Utils\Strings; |
| 11 | +use Rinvex\Country\Country; |
| 12 | +use Rinvex\Country\CountryLoader; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 17 | +use Symfony\Component\DomCrawler\Crawler; |
| 18 | +use Symplify\PackageBuilder\Console\Command\CommandNaming; |
| 19 | + |
| 20 | +final class CrawlCommand extends Command |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var SymfonyStyle |
| 24 | + */ |
| 25 | + private $symfonyStyle; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var mixed[][] |
| 29 | + */ |
| 30 | + private $groupsByCountry = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string[] |
| 34 | + */ |
| 35 | + private $topicsToCrawl = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var GroupRepository |
| 39 | + */ |
| 40 | + private $groupRepository; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var PhpRelatedFilter |
| 44 | + */ |
| 45 | + private $phpRelatedFilter; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string[] $topicsToCrawl |
| 49 | + */ |
| 50 | + public function __construct( |
| 51 | + SymfonyStyle $symfonyStyle, |
| 52 | + GroupRepository $groupRepository, |
| 53 | + PhpRelatedFilter $phpRelatedFilter, |
| 54 | + array $topicsToCrawl |
| 55 | + ) { |
| 56 | + parent::__construct(); |
| 57 | + $this->symfonyStyle = $symfonyStyle; |
| 58 | + $this->groupRepository = $groupRepository; |
| 59 | + $this->phpRelatedFilter = $phpRelatedFilter; |
| 60 | + $this->topicsToCrawl = $topicsToCrawl; |
| 61 | + } |
| 62 | + |
| 63 | + protected function configure(): void |
| 64 | + { |
| 65 | + $this->setName(CommandNaming::classToName(self::class)); |
| 66 | + $this->setDescription('Crawl "meetup.com" api topic lists by every country.'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Api is broken, thus useles here @see https://github.com/meetup/api/issues/249s |
| 71 | + */ |
| 72 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 73 | + { |
| 74 | + $emptyCountriesOnPhpFileContent = FileSystem::read(__DIR__ . '/../data/empty_countries_on_php.txt'); |
| 75 | + $emptyCountriesOnPhp = explode(PHP_EOL, $emptyCountriesOnPhpFileContent); |
| 76 | + |
| 77 | + foreach (CountryLoader::countries() as $country) { |
| 78 | + /** @var Country $country */ |
| 79 | + $country = CountryLoader::country($country['iso_3166_1_alpha2']); |
| 80 | + if ($country->getIsoAlpha2() === null) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + /* slovenia testing data: 'https://www.meetup.com/topics/php/si/' */ |
| 85 | + // $country = CountryLoader::country('si'); |
| 86 | + $countryCode = strtolower($country->getIsoAlpha2()); |
| 87 | + if (in_array($countryCode, $emptyCountriesOnPhp, true)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($this->topicsToCrawl as $keyword) { |
| 92 | + $this->processKeywordAndCountry($keyword, $countryCode, $country); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // detect country codes that were empty on "PHP" search |
| 97 | + // by excluding them, the followup search with other keywords can be faster |
| 98 | + $this->reportEmptyCountries(); |
| 99 | + $this->reportFoundGroups(); |
| 100 | + |
| 101 | + $this->symfonyStyle->success('OK'); |
| 102 | + |
| 103 | + return 0; |
| 104 | + } |
| 105 | + |
| 106 | + private function processKeywordAndCountry(string $keyword, string $countryCode, Country $country): void |
| 107 | + { |
| 108 | + $crawlUrl = sprintf('https://www.meetup.com/topics/%s/%s/', $keyword, $countryCode); |
| 109 | + $this->symfonyStyle->note(sprintf( |
| 110 | + 'Looking for meetups with "%s" topic in "%s" country code (%s):%s%s', |
| 111 | + $keyword, |
| 112 | + $countryCode, |
| 113 | + $country->getName(), |
| 114 | + PHP_EOL, |
| 115 | + $crawlUrl |
| 116 | + )); |
| 117 | + |
| 118 | + // init |
| 119 | + if (! isset($this->groupsByCountry[$countryCode])) { |
| 120 | + $this->groupsByCountry[$countryCode] = []; |
| 121 | + } |
| 122 | + |
| 123 | + $crawler = $this->createCrawlerFromUrl($crawlUrl); |
| 124 | + |
| 125 | + // top 10 overall → nothing found and fallback to main page → skip |
| 126 | + if (Strings::contains($crawler->text(), 'SQL NYC, The NoSQL & NewSQL Database Meetup')) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + $this->collectGroups($crawler, $countryCode); |
| 131 | + } |
| 132 | + |
| 133 | + private function reportFoundGroups(): void |
| 134 | + { |
| 135 | + $this->symfonyStyle->section('Found groups'); |
| 136 | + |
| 137 | + foreach ($this->groupsByCountry as $groups) { |
| 138 | + $groups = Arrays::unique($groups); |
| 139 | + $groups = $this->phpRelatedFilter->filterGroups($groups); |
| 140 | + |
| 141 | + foreach ($groups as $group) { |
| 142 | + $this->symfonyStyle->writeln(' - name: "' . str_replace('"', "'", $group[Group::NAME]) . '"'); |
| 143 | + $this->symfonyStyle->writeln(' meetup_com_url: ' . $group[Group::URL]); |
| 144 | + $this->symfonyStyle->newLine(); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private function createCrawlerFromUrl(string $url): Crawler |
| 150 | + { |
| 151 | + return new Crawler(FileSystem::read($url)); |
| 152 | + } |
| 153 | + |
| 154 | + private function collectGroups(Crawler $crawler, string $countryCode): void |
| 155 | + { |
| 156 | + $crawler->filterXPath('//span[@class="spreadable-item attachment"]')->each( |
| 157 | + function (Crawler $node) use ($countryCode): void { |
| 158 | + $groupUrl = $node->filterXPath('//a/@href')->text(); |
| 159 | + |
| 160 | + // is already among groups? |
| 161 | + if ($this->groupRepository->findByUrl($groupUrl)) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // headlines + urls of found groups |
| 166 | + $this->groupsByCountry[$countryCode][] = [ |
| 167 | + Group::NAME => $node->filterXPath('//span[@class="text--bold display--block"]')->text(), |
| 168 | + Group::URL => $groupUrl, |
| 169 | + ]; |
| 170 | + } |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + private function reportEmptyCountries(): void |
| 175 | + { |
| 176 | + $this->symfonyStyle->section('Empty country codes'); |
| 177 | + |
| 178 | + foreach ($this->groupsByCountry as $country => $groups) { |
| 179 | + if (count($groups)) { |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + $this->symfonyStyle->writeln($country); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments