-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers to scrape missing Bern events
- Loading branch information
Showing
12 changed files
with
1,165 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license http://opensource.org/licenses/mit-license.php MIT | ||
* @link https://github.com/nicoSWD | ||
* @author Nicolas Oelgart <[email protected]> | ||
*/ | ||
namespace nicoSWD\IfscCalendar\Domain\Calendar\Fixes; | ||
|
||
use Closure; | ||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use nicoSWD\IfscCalendar\Domain\Event\Helpers\Normalizer; | ||
use nicoSWD\IfscCalendar\Domain\Event\IFSCEvent; | ||
use nicoSWD\IfscCalendar\Domain\Event\IFSCEventFactory; | ||
use nicoSWD\IfscCalendar\Domain\Event\Month; | ||
use nicoSWD\IfscCalendar\Domain\HttpClient\HttpClientInterface; | ||
|
||
final readonly class SeasonFix2023 | ||
{ | ||
private const BERN_SCHEDULE_URL = 'https://www.ifsc-climbing.org/bern-2023/schedule'; | ||
|
||
public function __construct( | ||
private HttpClientInterface $httpClient, | ||
private IFSCEventFactory $eventFactory, | ||
private Normalizer $normalizer, | ||
) { | ||
} | ||
|
||
/** | ||
* @param IFSCEvent[] $events | ||
* @return IFSCEvent[] | ||
*/ | ||
public function fix(array $events): array | ||
{ | ||
// Add missing Bern events, which are listed on a separate page in an | ||
// entirely different format. Thanks, y'all. | ||
$bernEvents = array_filter($events, $this->isBernEvent()); | ||
|
||
if (!$bernEvents) { | ||
$events = array_merge($events, $this->fetchBernEvents()); | ||
} | ||
|
||
return $events; | ||
} | ||
|
||
private function fetchBernEvents(): array | ||
{ | ||
// Use DOM/XPath | ||
$regex = '~<div data-tag=[^>]+>\s*(?:<div[^>]+>\s*){2}\s*(?<date>\d{1,2}\sAUGUST\s\|\|\s\d{1,2}:\d{2})\s*</div>\s*<h3[^>]+>(?<name>[^<]+)~s'; | ||
$html = $this->httpClient->get(self::BERN_SCHEDULE_URL); | ||
$events = []; | ||
|
||
if (!preg_match_all($regex, $html, $matches)) { | ||
return []; | ||
} | ||
|
||
foreach ($matches['date'] as $key => $date) { | ||
$startDateTime = $this->createStartDate($date); | ||
$endDateTime = $this->getEndDateTime($startDateTime); | ||
|
||
$events[] = $this->eventFactory->create( | ||
name: $this->normalizer->cupName($matches['name'][$key]), | ||
id: 1301, | ||
description: 'IFSC - Climbing World Championships (B,L,S,B&L) - Bern (SUI) 2023', | ||
streamUrl: '', | ||
poster: '', | ||
startTime: $startDateTime, | ||
endTime: $endDateTime, | ||
); | ||
} | ||
|
||
return $events; | ||
} | ||
|
||
private function createStartDate(string $date): DateTimeImmutable | ||
{ | ||
[$day, $month, $hour, $minute] = sscanf(trim($date), '%d %s || %d:%d'); | ||
|
||
$date = new DateTime(); | ||
$date->setTimezone(new DateTimeZone('Europe/Zurich')); | ||
$date->setDate(2023, Month::fromName($month)->value, $day); | ||
$date->setTime($hour, $minute); | ||
|
||
return DateTimeImmutable::createFromMutable($date); | ||
} | ||
|
||
private function getEndDateTime(DateTimeImmutable $date): DateTimeImmutable | ||
{ | ||
$endDate = DateTime::createFromImmutable($date); | ||
$endDate->modify('+3 hours'); | ||
|
||
return DateTimeImmutable::createFromMutable($endDate); | ||
} | ||
|
||
private function isBernEvent(): Closure | ||
{ | ||
return static fn(IFSCEvent $event): bool => str_contains($event->name, 'Bern (SUI)'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license http://opensource.org/licenses/mit-license.php MIT | ||
* @link https://github.com/nicoSWD | ||
* @author Nicolas Oelgart <[email protected]> | ||
*/ | ||
namespace nicoSWD\IfscCalendar\Domain\Calendar; | ||
|
||
use nicoSWD\IfscCalendar\Domain\Calendar\Fixes\SeasonFix2023; | ||
use nicoSWD\IfscCalendar\Domain\Event\IFSCEvent; | ||
|
||
final readonly class IFSCCalendarPostFix | ||
{ | ||
public function __construct( | ||
private SeasonFix2023 $seasonFix2023, | ||
) { | ||
} | ||
|
||
/** | ||
* @param int $season | ||
* @param IFSCEvent[] $events | ||
* @return IFSCEvent[] | ||
*/ | ||
public function fix(int $season, array $events): array | ||
{ | ||
switch ($season) { | ||
case 2023: | ||
$events = $this->seasonFix2023->fix($events); | ||
break; | ||
} | ||
|
||
return $events; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.