Skip to content

Commit

Permalink
add new extractor and fix baseurl dataset and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Jul 17, 2023
1 parent 1350185 commit 46570cf
Show file tree
Hide file tree
Showing 156 changed files with 103 additions and 139 deletions.
75 changes: 75 additions & 0 deletions src/Extractors/Banlist/BlueExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Astrotomic\SourceBansSdk\Extractors\Banlist;

use Astrotomic\SourceBansSdk\Data\Ban;
use Astrotomic\SourceBansSdk\Extractors\Extractor;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
use SteamID;
use Symfony\Component\DomCrawler\Crawler;

class BlueExtractor extends Extractor
{
public function canHandle(Crawler $crawler): bool
{
$css = $crawler->filter('head link[rel=stylesheet]')->each(fn (Crawler $link) => $link->attr('href'));

return collect($css)->contains(
fn (string $href) => str_starts_with(trim($href, '/'), 'themes/sourcebans_blue/css')
);
}

public function handle(Crawler $crawler): ?LengthAwarePaginator
{
$tables = $crawler->filter('#banlist table.listtable tr td div.opener table.listtable');

if ($tables->count() === 0) {
return null;
}

$bans = $tables->each(function (Crawler $table): ?Ban {
$data = new Fluent();

$table->filter('tr')->each(function (Crawler $row) use ($data): void {
$cells = $row->filter('td.listtable_1');

if ($cells->count() !== 2) {
return;
}

$key = Str::slug($cells->eq(0)->innerText(), '_');
$value = rescue(fn () => $cells->eq(1)->innerText(), report: false);

$data->{$key} = $value;
});

return rescue(
callback: fn () => new Ban(
steam_id: new SteamID($data->steam_id),
invoked_on: $this->toCarbonImmutable($data->invoked_on),
ban_length: $this->toCarbonInterval($data->ban_length ?? $data->banlength),
expires_on: $this->toCarbonImmutable($data->expires_on),
ban_reason: $data->reason ?: null,
unban_reason: $data->unban_reason ?: null,
total_bans: (int) $data->total_bans,
),
report: false
);
});

$pagination = $this->paginationFromString(
$crawler->filter('#banlist-nav')->innerText()
);

return new LengthAwarePaginator(
items: collect($bans)
->filter()
->values(),
total: $pagination['total'],
perPage: $pagination['end'] - $pagination['start'],
currentPage: $this->currentPageFromSelect($crawler->filter('#banlist-nav select')),
);
}
}
2 changes: 1 addition & 1 deletion src/Extractors/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function toCarbonImmutable(?string $value): ?CarbonImmutable
'd.m.Y H:i',
'd.m.y H:i:s',
'l dS \o\f F Y h:i:s A',
'd-m-Y | H:i:s',
'd-m-Y \| H:i:s',
])
->map(fn (string $format) => rescue(fn () => CarbonImmutable::createFromFormat($format, $value), report: false))
->filter()
Expand Down
3 changes: 3 additions & 0 deletions src/SourceBansSdkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Astrotomic\SourceBansSdk;

use Astrotomic\SourceBansSdk\Extractors\Banlist\BlueExtractor;
use Astrotomic\SourceBansSdk\Extractors\Banlist\DefaultExtractor;
use Astrotomic\SourceBansSdk\Extractors\Banlist\FluentExtractor;
use Illuminate\Support\ServiceProvider;
Expand All @@ -12,10 +13,12 @@ public function register(): void
{
$this->app->bind(DefaultExtractor::class);
$this->app->bind(FluentExtractor::class);
$this->app->bind(BlueExtractor::class);

$this->app->tag([
DefaultExtractor::class,
FluentExtractor::class,
BlueExtractor::class,
], 'extractors.banlist');
}
}
5 changes: 1 addition & 4 deletions tests/Datasets/baseurls.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dataset('baseurls', [
// default
'https://firepoweredgaming.com/sourcebanspp/index.php',
'https://bans.neonheightsservers.ca/index.php',
'https://neonheights.xyz/bans/index.php',
'https://bans.harpoongaming.com/index.php',
'https://bans.panda-community.com/index.php',
'https://sourcebans.gamerzhost.de/sb419/index.php',
Expand All @@ -20,18 +20,15 @@
'http://162.55.184.155/index.php',
'https://sourcebans.ghostcap.com/index.php',
'https://pong.setti.info/sourcebans/index.php',
'http://freiheit-servers.ru/bans/index.php',
// fluent
'https://www.cswroclaw.pl/sbcsw/index.php',
'https://bans.zzk-community.de/index.php',
'https://kitiltasok.vrain.hu/index.php',
'https://sourcebans.zkservidores.com/index.php',
'https://www.sirplease.gg/index.php',
'https://sourcebans.onetap.pl/index.php',
'https://www.weallplay.eu/sourcebans/index.php',
'https://sb.feelthegame.eu/index.php',
'https://goodteemo.serverscstrike.com/index.php',
'https://bans.gaming-biatch.eu/index.php',
'https://bans.pineriver.dk/index.php',
'https://newlandscsgo.com.br/sourcebans/index.php',
]);
6 changes: 3 additions & 3 deletions tests/Feature/Requests/QueryBansRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
it('can load first page of bans', function (string $baseUrl): void {
$bans = $this->sourcebans($baseUrl)->queryBans(page: 1);

Assert::assertGreaterThanOrEqual(0, $bans->count());
Assert::assertGreaterThan(0, $bans->count());
Assert::assertLessThanOrEqual($bans->perPage(), $bans->count());
Assert::assertContainsOnlyInstancesOf(Ban::class, $bans);
})->with('baseurls');
Expand All @@ -34,10 +34,10 @@
it('can load all bans', function (string $baseUrl): void {
$bans = $this->sourcebans($baseUrl)->queryBans();

Assert::assertGreaterThanOrEqual(0, $bans->count());
Assert::assertGreaterThan(0, $bans->count());
Assert::assertContainsOnlyInstancesOf(Ban::class, $bans);
})->with([
'http://freiheit-servers.ru/bans/index.php', // default
'https://swedisholdtimers.net/sourcebans/index.php', // default
'https://www.weallplay.eu/sourcebans/index.php', // fluent
]);

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 46570cf

Please sign in to comment.