Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmy committed Nov 12, 2023
1 parent aaf5f19 commit 8167f19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin/moonboard-web-scraper
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $application->add(new ScrapeBenchmarksCommand(
authenticator: new Authenticator(
username: getenv('MOONBOARD_USERNAME'),
password: getenv('MOONBOARD_PASSWORD'),
logger: $logger,
),
holdSetups: HoldSetups::all(),
logger: $logger,
Expand Down
4 changes: 4 additions & 0 deletions src/Services/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@

namespace App\Services;

use Psr\Log\LoggerInterface;
use Symfony\Component\Panther\Client;

class Authenticator
{
public function __construct(
private readonly string $username,
private readonly string $password,
private readonly LoggerInterface $logger,
) {}

public function authenticate(Client $client): void
{
$this->logger->info('Authenticating...');

$client->request('GET', '/');
$client->executeScript("$('#loginDropdown').click()");

Expand Down
15 changes: 14 additions & 1 deletion src/UseCases/ScrapeBenchmarksAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function __invoke(): void
*/
private function getProblemUrls(HoldSetup $holdSetup): array
{
$this->logger->info('Scraping problem URLs...', [
'boardType' => $holdSetup->getBoardType()->getLabel(),
'boardAngle' => $holdSetup->getBoardAngle()->getLabel(),
]);

$this->client->request('GET', '/Dashboard/Index');
$this->client->executeScript("$('#Holdsetup').val('".$holdSetup->getBoardType()->value."').change()");
$this->client->executeScript("$('[data-id=\"".$holdSetup->getBoardAngle()->value."\"]').click()");
Expand Down Expand Up @@ -99,8 +104,11 @@ private function getProblemUrls(HoldSetup $holdSetup): array
private function getProblemData(array $problemUrls): array
{
$problemData = [];
$problemUrlCount = \count($problemUrls);

foreach ($problemUrls as $i => $problemUrl) {
$this->logger->info('Scraping problem data... ('.($i + 1).'/'.$problemUrlCount.')', ['problemUrl' => $problemUrl]);

foreach ($problemUrls as $problemUrl) {
$this->client->request('GET', $problemUrl);

$crawler = $this->client->getCrawler();
Expand Down Expand Up @@ -136,6 +144,11 @@ private function getProblemData(array $problemUrls): array
*/
private function saveProblemData(array $problemData, HoldSetup $holdSetup): void
{
$this->logger->info('Saving problem data...', [
'boardType' => $holdSetup->getBoardType()->getLabel(),
'boardAngle' => $holdSetup->getBoardAngle()->getLabel(),
]);

file_put_contents(
sprintf('problems %s %s.json', $holdSetup->getBoardType()->getLabel(), $holdSetup->getBoardAngle()->getLabel()),
json_encode($problemData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
Expand Down

0 comments on commit 8167f19

Please sign in to comment.