|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SteamMarketProviders\ParserManager\Parser; |
|
6
|
|
|
|
|
7
|
|
|
use PHPHtmlParser\Dom; |
|
8
|
|
|
use PHPHtmlParser\Exceptions\ChildNotFoundException; |
|
9
|
|
|
use PHPHtmlParser\Exceptions\CircularException; |
|
10
|
|
|
use PHPHtmlParser\Exceptions\ContentLengthException; |
|
11
|
|
|
use PHPHtmlParser\Exceptions\LogicalException; |
|
12
|
|
|
use PHPHtmlParser\Exceptions\NotLoadedException; |
|
13
|
|
|
use PHPHtmlParser\Exceptions\StrictException; |
|
14
|
|
|
use stdClass; |
|
15
|
|
|
use SteamMarketProviders\ParserManager\Builder\ParseRulesBuilder; |
|
16
|
|
|
use SteamMarketProviders\ParserManager\Contract\StrategyInterface; |
|
17
|
|
|
use SteamMarketProviders\ParserManager\Contract\UrlBuilderInterface; |
|
18
|
|
|
use SteamMarketProviders\ParserManager\Exception\HttpException; |
|
19
|
|
|
use SteamMarketProviders\ParserManager\Http\Http; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractProvider |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ParseRulesBuilder|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private null|ParseRulesBuilder $parseRulesBuilder = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var UrlBuilderInterface|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private null|UrlBuilderInterface $urlBuilder = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Dom |
|
35
|
|
|
*/ |
|
36
|
|
|
private readonly Dom $dom; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Http |
|
40
|
|
|
*/ |
|
41
|
|
|
private readonly Http $http; |
|
42
|
|
|
|
|
43
|
3 |
|
public function __construct() |
|
44
|
|
|
{ |
|
45
|
3 |
|
$this->http = new Http(); |
|
|
|
|
|
|
46
|
3 |
|
$this->dom = new Dom(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param int $page |
|
51
|
|
|
* @return UrlBuilderInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract protected function createUrl(int $page): UrlBuilderInterface; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return ParseRulesBuilder |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract protected function createParseRules(): ParseRulesBuilder; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param UrlBuilderInterface $urlBuilder |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setUrl(UrlBuilderInterface $urlBuilder): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->urlBuilder = $urlBuilder; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ParseRulesBuilder $parseRulesBuilder |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setParseRules(ParseRulesBuilder $parseRulesBuilder): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->parseRulesBuilder = $parseRulesBuilder; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param StrategyInterface $strategy |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setHttpStrategy(StrategyInterface $strategy): AbstractProvider |
|
83
|
|
|
{ |
|
84
|
|
|
$this->http->setStrategy($strategy); |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws HttpException |
|
91
|
|
|
* @throws ChildNotFoundException |
|
92
|
|
|
* @throws CircularException |
|
93
|
|
|
* @throws StrictException |
|
94
|
|
|
* @throws NotLoadedException |
|
95
|
|
|
* @throws ContentLengthException |
|
96
|
|
|
* @throws LogicalException |
|
97
|
|
|
*/ |
|
98
|
2 |
|
final public function start(int $page): array |
|
99
|
|
|
{ |
|
100
|
2 |
|
if (!$this->urlBuilder) { |
|
101
|
2 |
|
$this->urlBuilder = $this->createUrl($page); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
if (!$this->parseRulesBuilder) { |
|
105
|
1 |
|
$this->parseRulesBuilder = $this->createParseRules(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$response = $this->http |
|
109
|
|
|
->setUrl($this->urlBuilder->build()) |
|
110
|
|
|
->sendRequest(); |
|
111
|
|
|
|
|
112
|
|
|
return $this->parseHtml($response); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $html |
|
117
|
|
|
* @return array |
|
118
|
|
|
* @throws ChildNotFoundException |
|
119
|
|
|
* @throws CircularException |
|
120
|
|
|
* @throws ContentLengthException |
|
121
|
|
|
* @throws LogicalException |
|
122
|
|
|
* @throws NotLoadedException |
|
123
|
|
|
* @throws StrictException |
|
124
|
|
|
*/ |
|
125
|
|
|
private function parseHtml(stdClass $html): array |
|
126
|
|
|
{ |
|
127
|
|
|
$this->dom->loadStr($html->results_html); |
|
128
|
|
|
|
|
129
|
|
|
$contents = $this->dom->find('.market_listing_row_link'); |
|
130
|
|
|
|
|
131
|
|
|
$data = []; |
|
132
|
|
|
|
|
133
|
|
|
foreach ($contents as $row) { |
|
134
|
|
|
$link = $row->getAttribute('href'); |
|
135
|
|
|
$name = $row->find('.market_listing_item_name_block > span'); |
|
136
|
|
|
$prices = $row->find('.market_listing_their_price .normal_price span'); |
|
137
|
|
|
|
|
138
|
|
|
$data[] = [ |
|
139
|
|
|
'link'=> $link, |
|
140
|
|
|
'name' => $name->innerHtml, |
|
141
|
|
|
'price' => $prices->innerHtml |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->parseSingleItem($data); |
|
146
|
|
|
|
|
147
|
|
|
return $data; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
private function parseSingleItem(array &$data): void |
|
151
|
|
|
{ |
|
152
|
|
|
foreach ($data as $name => $info) { |
|
153
|
|
|
$url = "https://steamcommunity.com/market/listings/730/" . urlencode($info['name']) . "/render?start=0&count=1¤cy=1&format=json"; |
|
154
|
|
|
$response = $this->http->setUrl($url)->sendRequest(); |
|
155
|
|
|
$data['results_html'] = $response->results_html; |
|
156
|
|
|
$data['app_data'] = $response->app_data; |
|
157
|
|
|
$data['assets'] = $response->assets; |
|
158
|
|
|
// $response = $response->results_html; |
|
159
|
|
|
// |
|
160
|
|
|
// $this->dom->loadStr($response); |
|
161
|
|
|
// |
|
162
|
|
|
// $data[$name]['info'] = []; |
|
163
|
|
|
// $wrapper = $this->dom->find('.market_listing_iteminfo'); |
|
164
|
|
|
// |
|
165
|
|
|
// $data[$name]['info']['large_image'] = $wrapper->find('.market_listing_largeimage > img')->getAttribute('src'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|