Skip to content

Commit

Permalink
Added image_path_fragment to UserProfileParser.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jan 31, 2025
1 parent 178e0a4 commit 3f9fe02
Show file tree
Hide file tree
Showing 4 changed files with 3,486 additions and 1,720 deletions.
19 changes: 17 additions & 2 deletions src/Scrape/UserProfileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,31 @@ public static function parse(Crawler $crawler): array
{
return [
'name' => $crawler->filter('.actual_persona_name')->text(),
'image_hash' => self::parseImageHash($crawler->filter('.playerAvatarAutoSizeInner > img')->attr('src')),
'image_hash' => self::parseImageHash(
$src = $crawler->filter('.playerAvatarAutoSizeInner > img')->attr('src')
),
'image_path_fragment' => self::parseImagePathFragment($src),
];
}

private static function parseImageHash(string $imageUrl): string
{
if (preg_match('[.*/([\da-f]+)]', $imageUrl, $matches)) {
if (preg_match('[/([\da-f]{40}(?=\b|_))]', $imageUrl, $matches)) {
return $matches[1];
}

throw new \InvalidArgumentException("Image URL does not contain hash: \"$imageUrl\".");
}

/**
* Parses the mutating part of the image path. When non-null, this should be used instead of just the hash.
*/
private static function parseImagePathFragment(string $imageUrl): ?string
{
if (preg_match('[/(\d+/[\da-f]{40}\..{3,10})]', $imageUrl, $matches)) {
return $matches[1];
}

return null;
}
}
Loading

0 comments on commit 3f9fe02

Please sign in to comment.