Skip to content

Commit 7adc52c

Browse files
committed
Add CrawlCommand
1 parent 0fa189e commit 7adc52c

14 files changed

Lines changed: 1524 additions & 1 deletion

File tree

‎.travis.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ install:
1414
script:
1515
- vendor/bin/phpunit
1616

17+
- bin/console crawl
18+
1719
# code analysis
1820
- composer check-cs
1921
- composer phpstan

‎composer.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"rinvex/country": "^3.1",
1111
"symfony/console": "^4.1",
1212
"symfony/dependency-injection": "^4.1",
13-
"symfony/http-kernel": "^4.1"
13+
"symfony/http-kernel": "^4.1",
14+
"symfony/dom-crawler": "^4.1"
1415
},
1516
"require-dev": {
1617
"phpstan/phpstan": "^0.10.3",
@@ -27,6 +28,7 @@
2728
},
2829
"autoload-dev": {
2930
"psr-4": {
31+
"Fop\\MeetupCom\\Tests\\": "packages/MeetupCom/tests",
3032
"Fop\\Tests\\": "tests"
3133
}
3234
},

‎ecs.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ parameters:
1919
- 'PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer'
2020
- 'PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer'
2121
- 'Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff'
22+
23+
skip:
24+
Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenStaticFunctionSniff:
25+
# helper class
26+
- 'src/Utils/Arrays.php'

‎monorepo-builder.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ parameters:
1313
symfony/console: '%symfony_version%'
1414
symfony/dependency-injection: '%symfony_version%'
1515
symfony/http-kernel: '%symfony_version%'
16+
symfony/dom-crawler: '%symfony_version%'
1617
autoload:
1718
psr-4:
1819
Fop\: "src"

‎packages/MeetupCom/composer.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@
99
"psr-4": {
1010
"Fop\\MeetupCom\\": "src"
1111
}
12+
},
13+
"autoload-dev": {
14+
"psr-4": {
15+
"Fop\\MeetupCom\\Tests\\": "tests"
16+
}
1217
}
1318
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fop\MeetupCom\Filter;
4+
5+
use Fop\Entity\Group;
6+
use Nette\Utils\Strings;
7+
8+
final class PhpRelatedFilter
9+
{
10+
/**
11+
* @var string[]
12+
*/
13+
private $yesKeywords = [
14+
'php',
15+
'PrestaShop',
16+
'Wordpress',
17+
'Drupal',
18+
'Laravel',
19+
'Symfony',
20+
'Zend',
21+
'CakePHP',
22+
'Magento',
23+
'Mautic',
24+
'SilverStripe',
25+
'craft cms',
26+
'eZ',
27+
// misc
28+
'StripeGirls',
29+
'Elastic',
30+
];
31+
32+
/**
33+
* @param mixed[] $groups
34+
* @return mixed[]
35+
*/
36+
public function filterGroups(array $groups): array
37+
{
38+
$matchedGroups = [];
39+
foreach ($groups as $group) {
40+
if ($this->isNameMatch((string) $group[Group::NAME])) {
41+
$matchedGroups[] = $group;
42+
}
43+
}
44+
45+
return $matchedGroups;
46+
}
47+
48+
private function isNameMatch(string $name): bool
49+
{
50+
if (Strings::match($name, '#\b(' . implode('|', $this->yesKeywords) . ')\b#i')) {
51+
return true;
52+
}
53+
54+
return Strings::endsWith($name, 'PHP');
55+
}
56+
}

‎packages/MeetupCom/src/config/config.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,14 @@ imports:
44
parameters:
55
meetup_com_api_key: ''
66
found_meetups_storage_file: '%kernel.project_dir%/source/_data/generated/found_meetups.yml'
7+
8+
topics_to_crawl:
9+
- 'php'
10+
- 'laravel'
11+
- 'drupal'
12+
- 'wordpress'
13+
- 'symfony'
14+
- 'cakephp'
15+
- 'magento'
16+
- 'prestashop'
17+
- 'php-framework'

0 commit comments

Comments
 (0)