-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgrand_tour.php
74 lines (56 loc) · 2.21 KB
/
grand_tour.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types = 1);
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
define('RACE_ID', 171);
require_once('vendor/autoload.php');
require_once('ProCyclingStatsFetcher.php');
require_once('ScoritoFormatter.php');
$scorito = new ScoritoGrandTourGame(
RACE_ID
);
$scoritoData = $scorito->fetch();
$out = fopen('grand-tour.csv', 'w');
fputcsv($out, array_keys($scoritoData[0]));
foreach ($scoritoData as $row) {
fputcsv($out, array_map(function ($col) {
if (is_array($col)) {
return print_r($col, true);
}
return $col;
}, $row));
}
fclose($out);
class ScoritoGrandTourGame {
private HttpClientInterface $client;
private int $raceId;
private ProCyclingStatsFetcher $fetcher;
public function __construct(int $raceId)
{
$this->raceId = $raceId;
$this->client = HttpClient::create();
$this->fetcher = new ProCyclingStatsFetcher($this->client);
}
public function fetchTeams(): array
{
$response = $this->client->request('GET', 'https://cycling.scorito.com/cycling/v2.0/team');
$scoritoData = $response->toArray();
return $scoritoData['Content'];
}
public function fetch(): array
{
$response = $this->client->request('GET', 'https://cycling.scorito.com/cyclingmanager/v1.0/eventriderenriched/' . $this->raceId);
$scoritoData = $response->toArray();
$teams = $this->fetchTeams();
$filtered = $this->filterRidersOnStatus($scoritoData['Content']);
$filtered = array_map(['ScoritoFormatter', 'formatQualities'], $filtered);
$filtered = array_map(['ScoritoFormatter', 'formatType'], $filtered);
$filtered = array_map(fn (array $rider) => ScoritoFormatter::formatTeam($rider, $teams), $filtered);
$filtered = array_map(['ScoritoFormatter', 'filterColumns'], $filtered);
return $this->fetcher->fetchRiders($filtered, false, true, true);
}
private function filterRidersOnStatus(array $riders): array
{
return array_values(array_filter($riders, fn($rider): bool => $rider['Status'] === 1));
}
}