Skip to content

Commit

Permalink
Fix OQS live streams and schedule (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoSWD authored May 15, 2024
1 parent d7e2e3a commit 6d417a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/Domain/Calendar/PostProcess/Season2024PostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
use nicoSWD\IfscCalendar\Domain\Round\IFSCRoundProviderInterface;
use nicoSWD\IfscCalendar\Domain\Round\IFSCRoundStatus;
use nicoSWD\IfscCalendar\Domain\Schedule\IFSCSchedule;
use nicoSWD\IfscCalendar\Domain\Stream\LiveStream;

final readonly class Season2024PostProcessor
{
private const int WUJIANG_IFSC_EVENT_ID = 1354;
private const string WUJIAN_POSTER_URL = 'https://res.dasheng.top/227/pro/ctms_tool/20240323/jpg/d7f6d496a71d4dc785c8d0e51169d17b.jpg';
private const int OLYMPIC_QUALIFIERS_SHANGHAI_ID = 1384;
private const string OLYMPIC_QUALIFIERS_SHANGHAI_INFO_SHEET = 'https://images.ifsc-climbing.org/ifsc/image/private/t_q_good/prd/yg3cqznmay12orsv9hpa.pdf';
private const string OLYMPIC_QUALIFIERS_SHANGHAI_LIVE_STREAM = 'https://olympics.com/en/sport-events/olympic-qualifier-series-2024-shanghai/broadcasting-schedule';

public function __construct(
private IFSCRoundProviderInterface $roundProvider,
Expand Down Expand Up @@ -72,6 +74,7 @@ private function fetchOlympicQualifiersShanghaiRounds(IFSCEvent $event): array
startTime: $schedule->startsAt,
endTime: $schedule->endsAt,
status: IFSCRoundStatus::PROVISIONAL,
liveStream: new LiveStream(self::OLYMPIC_QUALIFIERS_SHANGHAI_LIVE_STREAM),
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Domain/Round/IFSCRoundFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public function create(
DateTimeImmutable $startTime,
?DateTimeImmutable $endTime,
IFSCRoundStatus $status,
?LiveStream $liveStream = null,
): IFSCRound {
$tags = $this->getTags($roundName);
$liveStream = $this->findLiveStream($event, $roundName);

if (!$liveStream) {
$liveStream = $this->findLiveStream($event, $roundName);
}

if ($liveStream->scheduledStartTime) {
$youTubeStartTime = $this->buildStartTime($liveStream, $event);
Expand Down
29 changes: 16 additions & 13 deletions src/Infrastructure/Schedule/HTMLNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public function normalize(string $html): string
{
$html = str_replace(["<br/>", "<b>&#160;</b>"], ["\n", ''], $html);
$html = preg_replace(['~<([ai])\s?[^>]*>.*?</\\1>~', '~<(?:img|hr)[^>]+>~'], '', $html);
$html = preg_replace(['~<(a)\s?[^>]*>.*?</a>~', '~<(?:img|hr)[^>]+>~'], '', $html);
$html = str_replace(["", ""], "-", $html);
$html = str_replace("&#160;", " ", $html);
$html = preg_replace("~,\s+20\d\d~", '', $html);
Expand All @@ -24,7 +24,7 @@ public function normalize(string $html): string
$html,
);
$html = html_entity_decode($html);
$offset = $this->cutOffOffset2($html);
$offset = $this->cutOffOffset($html);
$html = substr($html, $offset);
$lines = preg_split('~\n~', $html, -1, PREG_SPLIT_NO_EMPTY);
$lines = array_map('trim', $lines);
Expand All @@ -42,17 +42,6 @@ public function normalizeTime(string $schedule): string
return preg_replace('~(\d\d:\d\d)\s*\n(\d\d:\d\d)\s*~', "\$1 - \$2\n", $schedule);
}

private function cutOffOffset2(string $html): int
{
$pos = strpos($html, 'PROGRAMME');

if ($pos === false) {
$pos = strpos($html, 'Schedule');
}

return $pos ?: 0;
}

private function fixOqsShanghai(string $html): string
{
// Remove line-break
Expand All @@ -62,6 +51,20 @@ private function fixOqsShanghai(string $html): string
$html
);

// Fix round names
$html = preg_replace_callback(
'~(?<category>Women|Men)\'s Boulder & Lead Final - <i>(?<discipline>Boulder|Lead) stage</i>~',
static fn (array $matches): string => sprintf("%s's %s Final", $matches['category'], $matches['discipline']),
$html,
);

// Fix round name
$html = str_replace(
'followed by <b>Men’s Speed Qualification </b>',
"<b>Women's & Men's Speed Qualification",
$html
);

// Re-order lines
return preg_replace_callback(
'~(([^\n]+\n){2})(<b>(?:Thursday|Friday) 1\d May\s*[^\n]+\n)~',
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/Infrastructure/Schedule/PDFScheduleProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ final class PDFScheduleProviderTest extends TestCase
$this->assertSameDate("2024-05-17T10:00:00+08:00", $schedule[1]->startsAt);
$this->assertNull($schedule[1]->endsAt);

$this->assertSame("Men's Speed Qualification", $schedule[2]->name);
$this->assertSame("Men's & Women's Speed Qualification", $schedule[2]->name);
$this->assertSameDate("2024-05-17T16:50:00+08:00", $schedule[2]->startsAt);
$this->assertNull($schedule[2]->endsAt);

Expand All @@ -290,17 +290,21 @@ final class PDFScheduleProviderTest extends TestCase
$this->assertSameDate("2024-05-18T17:00:00+08:00", $schedule[5]->startsAt);
$this->assertNull($schedule[5]->endsAt);

$this->assertSame("Men's Boulder & Lead Final", $schedule[6]->name);
$this->assertSame("Men's Boulder Final", $schedule[6]->name);
$this->assertSameDate("2024-05-18T10:00:00+08:00", $schedule[6]->startsAt);
$this->assertNull($schedule[6]->endsAt);

$this->assertSame("Men's Boulder & Lead Final", $schedule[7]->name);
$this->assertSame("Men's Lead Final", $schedule[7]->name);
$this->assertSameDate("2024-05-18T12:05:00+08:00", $schedule[7]->startsAt);
$this->assertNull($schedule[7]->endsAt);

$this->assertSame("Women's Boulder & Lead Final", $schedule[8]->name);
$this->assertSame("Women's Boulder Final", $schedule[8]->name);
$this->assertSameDate("2024-05-19T15:25:00+08:00", $schedule[8]->startsAt);
$this->assertNull($schedule[8]->endsAt);

$this->assertSame("Women's Lead Final", $schedule[9]->name);
$this->assertSameDate("2024-05-19T17:30:00+08:00", $schedule[9]->startsAt);
$this->assertNull($schedule[9]->endsAt);
}

private function assertSameDate(string $expected, DateTimeImmutable $actual): void
Expand Down

0 comments on commit 6d417a5

Please sign in to comment.