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
|
1 |
|
public function __construct() |
44
|
|
|
{ |
45
|
1 |
|
$this->http = new Http(); |
|
|
|
|
46
|
1 |
|
$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
|
|
|
final public function start(int $page): array |
99
|
|
|
{ |
100
|
|
|
if (!$this->urlBuilder) { |
101
|
|
|
$this->urlBuilder = $this->createUrl($page); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!$this->parseRulesBuilder) { |
105
|
|
|
$this->parseRulesBuilder = $this->createParseRules(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$response = $this->http->sendRequest($this->urlBuilder->build()); |
109
|
|
|
return $this->parseHtml($response); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $html |
114
|
|
|
* @return array |
115
|
|
|
* @throws ChildNotFoundException |
116
|
|
|
* @throws CircularException |
117
|
|
|
* @throws ContentLengthException |
118
|
|
|
* @throws LogicalException |
119
|
|
|
* @throws NotLoadedException |
120
|
|
|
* @throws StrictException |
121
|
|
|
*/ |
122
|
|
|
private function parseHtml(stdClass $html): array |
123
|
|
|
{ |
124
|
|
|
$this->dom->loadStr($html->results_html); |
125
|
|
|
|
126
|
|
|
$contents = $this->dom->find('.market_listing_row_link'); |
127
|
|
|
|
128
|
|
|
$data = []; |
129
|
|
|
|
130
|
|
|
foreach ($contents as $row) { |
131
|
|
|
$link = $row->getAttribute('href'); |
132
|
|
|
$name = $row->find('.market_listing_item_name_block > span'); |
133
|
|
|
$prices = $row->find('.market_listing_their_price .normal_price span'); |
134
|
|
|
|
135
|
|
|
$data[] = [ |
136
|
|
|
'link'=> $link, |
137
|
|
|
'name' => $name->innerHtml, |
138
|
|
|
'price' => $prices->innerHtml |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->parseSingleItem($data); |
143
|
|
|
|
144
|
|
|
return $data; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function parseSingleItem(array &$data): void |
148
|
|
|
{ |
149
|
|
|
foreach ($data as $name => $info) { |
150
|
|
|
$url = "https://steamcommunity.com/market/listings/730/" . $info['name'] . "/render?start=0&count=1¤cy=1&format=json"; |
151
|
|
|
$response = $this->http->sendRequest($url); |
152
|
|
|
$data['results_html'] = $response->results_html; |
153
|
|
|
$data['app_data'] = $response->app_data; |
154
|
|
|
$data['assets'] = $response->assets; |
155
|
|
|
// $response = $response->results_html; |
156
|
|
|
// |
157
|
|
|
// $this->dom->loadStr($response); |
158
|
|
|
// |
159
|
|
|
// $data[$name]['info'] = []; |
160
|
|
|
// $wrapper = $this->dom->find('.market_listing_iteminfo'); |
161
|
|
|
// |
162
|
|
|
// $data[$name]['info']['large_image'] = $wrapper->find('.market_listing_largeimage > img')->getAttribute('src'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|