|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mosiyash\HidemeParser; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use function str_replace; |
|
7
|
|
|
use Symfony\Component\Cache\Simple\FilesystemCache; |
|
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
9
|
|
|
|
|
10
|
|
|
final class HidemeParser |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var FilesystemCache |
|
14
|
|
|
*/ |
|
15
|
|
|
private $cache; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cachePath; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Client |
|
24
|
|
|
*/ |
|
25
|
|
|
private $client; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $cachePath |
|
29
|
|
|
* @param null|Client $client |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(string $cachePath, Client $client = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->cachePath = $cachePath; |
|
34
|
|
|
$this->cache = new FilesystemCache('', 0, $this->cachePath); |
|
35
|
|
|
$this->client = $client; |
|
36
|
|
|
|
|
37
|
|
|
if (null === $this->client) { |
|
38
|
|
|
$this->client = new Client(); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $html |
|
44
|
|
|
* |
|
45
|
|
|
* @return string[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getPaginationLinks(string $html): array |
|
48
|
|
|
{ |
|
49
|
|
|
$crawler = new Crawler($html); |
|
50
|
|
|
$perPage = $crawler->filter('table.proxy__t tbody tr')->count(); |
|
51
|
|
|
$lastPageNum = (int) $crawler->filter('.proxy__pagination ul li')->last()->text(); |
|
52
|
|
|
|
|
53
|
|
|
$urlPattern = preg_replace( |
|
54
|
|
|
'/start=\d+/', |
|
55
|
|
|
'start={n}', |
|
56
|
|
|
$crawler->filter('.proxy__pagination ul li a')->attr('href') |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$data = []; |
|
60
|
|
|
|
|
61
|
|
|
for ($i = 1; $i <= $lastPageNum; ++$i) { |
|
62
|
|
|
$url = str_replace( |
|
63
|
|
|
'{n}', |
|
64
|
|
|
$i * $perPage - $perPage, |
|
65
|
|
|
$urlPattern |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$data[] = $url; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $data; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $html |
|
76
|
|
|
* |
|
77
|
|
|
* @return Ip[] |
|
78
|
|
|
*/ |
|
79
|
|
|
public function loadTableData(string $html): array |
|
80
|
|
|
{ |
|
81
|
|
|
$data = []; |
|
82
|
|
|
|
|
83
|
|
|
$crawler = new Crawler($html); |
|
84
|
|
|
$crawler->filter('table.proxy__t tbody tr')->each(function (Crawler $crawler) use (&$data) { |
|
85
|
|
|
$address = trim($crawler->filter('td')->eq(0)->text()); |
|
86
|
|
|
$port = (int) trim($crawler->filter('td')->eq(1)->text()); |
|
87
|
|
|
|
|
88
|
|
|
$country = trim( |
|
89
|
|
|
str_replace( |
|
90
|
|
|
["\n", ' '], |
|
91
|
|
|
['', ''], |
|
92
|
|
|
strip_tags( |
|
93
|
|
|
preg_replace( |
|
94
|
|
|
'/<span.*?>.*?<\/span>/', |
|
95
|
|
|
'', |
|
96
|
|
|
$crawler->filter('td')->eq(2)->html() |
|
97
|
|
|
) |
|
98
|
|
|
) |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$city = trim($crawler->filter('td')->eq(2)->filter('span')->eq(1)->text()); |
|
103
|
|
|
$city = ('' === $city) ? null : $city; |
|
104
|
|
|
|
|
105
|
|
|
$speed = (int) trim($crawler->filter('td')->eq(3)->filter('p')->eq(0)->text()); |
|
106
|
|
|
$type = trim($crawler->filter('td')->eq(4)->text()); |
|
107
|
|
|
$anonimity = Ip::$anonimityValues[trim($crawler->filter('td')->eq(5)->text())]; |
|
108
|
|
|
|
|
109
|
|
|
$data[] = new Ip( |
|
110
|
|
|
$address, |
|
111
|
|
|
$port, |
|
112
|
|
|
$country, |
|
113
|
|
|
$city, |
|
114
|
|
|
$speed, |
|
115
|
|
|
$type, |
|
116
|
|
|
$anonimity |
|
117
|
|
|
); |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
return $data; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function updateCache(array $ipList): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->cache->set('hideme_parser.ip_list', $ipList); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function count(): int |
|
129
|
|
|
{ |
|
130
|
|
|
return count($this->cache->get('hideme_parser.ip_list', [])); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string[]|null $type |
|
135
|
|
|
* @param string[]|null $anonimity |
|
136
|
|
|
* |
|
137
|
|
|
* @return Ip[] |
|
138
|
|
|
*/ |
|
139
|
|
|
public function all(array $type = null, array $anonimity = null): array |
|
140
|
|
|
{ |
|
141
|
|
|
/** @var Ip[] $data */ |
|
142
|
|
|
$data = $this->cache->get('hideme_parser.ip_list', []); |
|
143
|
|
|
|
|
144
|
|
|
if (null === $type && null === $anonimity) { |
|
145
|
|
|
return $data; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
foreach ($data as $key => $ip) { |
|
149
|
|
|
if ($type && !in_array($ip->getType(), $type)) { |
|
150
|
|
|
unset($data[$key]); |
|
151
|
|
|
} |
|
152
|
|
|
if ($anonimity && !in_array($ip->getAnonimity(), $anonimity)) { |
|
153
|
|
|
unset($data[$key]); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$data = array_values($data); |
|
158
|
|
|
|
|
159
|
|
|
return $data; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|