This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auction.php
146 lines (111 loc) · 4.16 KB
/
auction.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
require __DIR__.'/boot.php';
function retrieve_id($string, $needle = '=')
{
return intval(substr($string, strpos($string, $needle) + strlen($needle)));
}
function parameter_info($content)
{
$info = explode(' by ', substr($content, 2));
return [
'type' => str_replace(' ', '_', $info[0]),
'value' => floatval($info[1]),
'percentage' => str_contains($info[1], '%'),
];
}
function history_info($html)
{
$rows = Sunra\PhpSimple\HtmlDomParser::str_get_html($html)->find('table', 1)->find('tr');
array_shift($rows);
$info = [];
foreach ($rows as $row) {
$info[] = [
'bidder_id' => retrieve_id($row->children(0)->find('a', 0)->attr['href']),
'price' => floatval($row->children(1)->find('b', 0)->text()),
'time' => \Carbon\Carbon::parse(trim($row->children(2)->text()))->setTimezone('GMT')->toDateTimeString(),
'timezone' => 'GMT',
];
}
usort($info, function ($a, $b) {
return strcmp($a['time'], $b['time']);
});
return $info;
}
function auction_info($html)
{
$dom = Sunra\PhpSimple\HtmlDomParser::str_get_html($html)->find('table tr', 1);
$item = strtolower($dom->children(2)->text());
if (str_contains($item, 'company')) {
$temp = explode(' ', trim(strstr($item, '<', true)));
$item = [
'type' => 'company',
'quality' => $temp[0],
'product' => $temp[1],
];
} elseif (str_contains($item, '#')) {
$temp = explode(PHP_EOL, trim($item));
$temp2 = explode(' ', trim($temp[1]));
$item = [
'type' => 'equipment',
'equipment_id' => retrieve_id($temp[0], '#'),
'quality' => $temp2[0],
'slot' => $temp2[1],
'parameters' => [parameter_info($temp[2]), parameter_info($temp[3])],
];
} else {
$temp = explode('_-_', str_replace(' ', '_', trim($item)));
$item = [
'type' => 'special_item',
'name' => $temp[0],
'period' => isset($temp[1]) ? $temp[1] : null,
];
}
$noBid = trim($dom->children(1)->text()) === 'None';
$info = [
'seller_id' => retrieve_id($dom->children(0)->find('a', 0)->attr['href']),
'bidder_id' => $noBid ? null : retrieve_id($dom->children(1)->find('a', 0)->attr['href']),
'item' => $item,
'price' => floatval($dom->children(3)->find('b', 0)->text()),
'bidders' => intval($dom->children(4)->find('b', 0)->text()),
'history' => $noBid ? [] : history_info($html),
];
return json_encode($info);
}
$client = new GuzzleHttp\Client();
do {
$climate->out("Fetching {$server} {$index}...");
$tries = 0;
download:
try {
$promises = [];
for ($i = $index; $i < $index + 5; ++$i) {
$promises[] = $client
->getAsync("https://{$server}.e-sim.org/auction.html?id={$i}")
->then(function (GuzzleHttp\Psr7\Response $response) use ($server, $i) {
$outputPath = __DIR__."/{$server}/auctions/".floor($i / 10000)."/";
if (! is_dir($outputPath)) {
mkdir($outputPath, 0777, true);
}
file_put_contents($outputPath.$i.'.json', auction_info($response->getBody()->getContents()), LOCK_EX);
}, function (GuzzleHttp\Exception\ServerException $e) {
if (! $e->hasResponse() ||
false === stripos($e->getResponse()->getBody()->getContents(), 'No such auction')
) {
throw new Exception($e->getMessage());
}
});
}
GuzzleHttp\Promise\settle($promises)->wait();
} catch (\Exception $e) {
$climate->to('error')->red($e->getMessage());
if (++$tries > 10) {
$climate->to('error')->red("Fail to fetch {$server} {$index} after 10 times, give up.");
exit(1);
}
sleep(5 + $tries ^ 2);
$climate->yellow("Refetching {$server} {$index}...");
goto download;
}
sleep(3);
$index += 5;
} while ($index < $endAt);