Skip to content

Commit

Permalink
Fixed purchase area parsing for games sold as "editions".
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Feb 3, 2025
1 parent 3f9fe02 commit 2005610
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Scrape/AppDetailsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function tryParseStorePage(string $html): array

private static function parseStorePage(string $html): array
{
$html = self::sanitize($html);
$crawler = new NativeCrawler($html);

self::validate($crawler, $html);
Expand Down Expand Up @@ -426,6 +427,8 @@ private static function findPrimaryPurchaseArea(Crawler $crawler, string $title)
private static function filterPurchaseAreas(Crawler $crawler, bool $firstOnly = false): Crawler
{
// Pick purchase areas with platform icons.
// TODO: Use :has when supported, since there may be multiple platform_img elements, duplicating work.
// https://github.com/symfony/symfony/pull/49388
$purchaseAreas = $crawler->filter(
'#game_area_purchase .game_area_purchase_game:not(.demo_above_purchase)
> .game_area_purchase_platform > .platform_img'
Expand Down Expand Up @@ -492,4 +495,16 @@ private static function parseCapsuleUrl(Crawler $crawler): string
{
return $crawler->filter('meta[itemprop=image]')->attr('content');
}

/**
* Strips the script containing the ShowComparisonDialog function from the specified HTML source. This JS function
* includes quoted markup, which breaks the parser on pages where games are sold in "editions". This should not
* be necessary once a proper native parser is implemented.
*
* @see https://github.com/symfony/symfony/pull/54383
*/
private static function sanitize(string $html): string
{
return preg_replace('[<script>(.(?!</script>))*function ShowComparisonDialog.*?</script>]s', '', $html);
}
}
24 changes: 24 additions & 0 deletions test/Functional/ScrapeAppDetailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,30 @@ public function provideFreeApps(): array
];
}

/**
* Tests that when a game is sold in "editions", the price is parsed correctly.
*
* @dataProvider provideEditions
*/
public function testEditions(int $appId): void
{
$app = $this->porter->importOne(new Import(new ScrapeAppDetails($appId)));

self::assertNotNull($app['price']);
}

/**
* @see https://store.steampowered.com/app/2495100/Hello_Kitty_Island_Adventure
* @see https://store.steampowered.com/app/690830/Foundation
*/
public function provideEditions(): iterable
{
return [
'Single edition' => [2495100],
'Multiple editions' => [690830],
];
}

/**
* Tests that a game with multiple videos has its video IDs parsed correctly.
*
Expand Down

0 comments on commit 2005610

Please sign in to comment.