-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tieba.php
274 lines (260 loc) · 7.89 KB
/
Tieba.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
class Tieba
{
private $content = [];
/**
* @param string[] $tieba
* @param string $cookie
*/
public function __construct(array $tieba, string $cookie = '')
{
$this->content = $this->load($tieba, $cookie);
}
/**
* 获取百度贴吧热门帖子
*
* @return array 二维数组
*/
public function getTiebaHot(): array
{
$results = [];
foreach ($this->content as $k => $text) {
$results[] = empty($text) ? [] : $this->getHot($text);
}
return $results;
}
/**
* 获取用户
*
* @return array
*/
public function getUsers(): array
{
return array_values(array_unique(array_merge($this->getAuthors(), $this->getLastPostings()), SORT_LOCALE_STRING));
}
/**
* 获取作者
*
* @return array
*/
public function getAuthors(): array
{
$results = [];
foreach ($this->content as $k => $text) {
$results = array_merge($results, empty($text) ? [] : $this->getAuthor($text));
}
return array_values(array_unique($results, SORT_LOCALE_STRING));
}
/**
* 获取作者
*
* @return array
*/
public function getLastPostings(): array
{
$results = [];
foreach ($this->content as $k => $text) {
$results = array_merge($results, empty($text) ? [] : $this->getLastPosting($text));
}
return array_values(array_unique($results, SORT_LOCALE_STRING));
}
/**
* 获取所有帖子
*
* @return string[]
*/
public function getAllText(): array
{
$results = [];
foreach ($this->content as $k => $text) {
$results[] = $this->getText($text);
}
return $results;
}
/**
* 获取所有帖子
*
* @return string[]
*/
public function getText(string $text): array
{
$startString = '<a';
$endString = '</a>';
$startStringLen = mb_strlen($startString);
$endStringLen = mb_strlen($endString);
$start = 0;
$results = [];
$find = ' title="';
$findLen = mb_strlen($find);
$findEnd = "\"";
while (true) {
$head = mb_stripos($text, $startString, $start);
if (false === $head) {
break;
}
$tail = mb_stripos($text, $endString, $start + $startStringLen);
if (false === $tail) {
break;
}
$centerText = mb_substr($text, $head + $startStringLen, $tail - $head - $startStringLen);
if (
false !== mb_stripos($centerText, ' rel="noreferrer"') &&
false !== mb_stripos($centerText, "/p/") &&
false !== ($titleStart = mb_stripos($centerText, $find)) &&
false !== mb_stripos($centerText, ' target="_blank"')
) {
$titleEnd = mb_stripos($centerText, $findEnd, $titleStart + $findLen);
if (false === $titleEnd) {
break;
}
$results[] = mb_substr($centerText, $titleStart + $findLen, $titleEnd - $titleStart - $findLen);
}
$start = $tail + $endStringLen;
}
return $results;
}
/**
* 获取百度贴吧首页信息
*
* @param string[] $tieba
* @param string $cookie
* @return array
*/
private function load(array $tieba, string $cookie = ''): array
{
foreach ($tieba as $v) {
if (!is_string($v)) {
return [];
}
// 这个用来作为reffer
$address = 'http://tieba.baidu.com/f?' . http_build_query([
'ie' => 'utf-8',
'kw' => $v,
'fr' => 'search',
]);
break;
}
if (!isset($address)) {
return [];
}
$tools = new Tools\MultipleUrlContextLoader();
$tools->setTimeout(60 * count($tieba));
$tools->setUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$tools->setReffer($address);
if (!empty($cookie)) {
$tools->setCookie($cookie);
}
$tools->setHeaders([
'Host: tieba.baidu.com',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection: keep-alive',
]);
$urls = [];
foreach ($tieba as $v) {
$urls[] = 'http://tieba.baidu.com/f?' . http_build_query([
'ie' => 'utf-8',
'kw' => $v,
'fr' => 'search',
]);
}
$tools->setUrls($urls);
$tools->loadContent();
return $tools->getContents();
}
/**
* 请求百度贴吧的PC页面,这个函数返回它的本吧热贴数据
*
* 这个函数安全的,总会返回字符串
*
* @param string $content
* @return string
* @author az13js
*/
private function getHot(string $content): array
{
$len = mb_strlen('<meta name="description" content="');
if (false === $len) {
return [];
}
$start = mb_stripos($content, '<meta name="description" content="');
if (false === $start) {
return [];
}
$end = mb_stripos($content, '">', $start);
if (false === $end) {
return [];
}
$metaData = mb_substr($content, $start + $len, $end - $start - $len);
$path = [];
$results = [];
$id = 0;
do {
$id++;
$sign = " $id-";
$match = mb_stripos($metaData, $sign);
if (false !== $match) {
$path[] = $match;
} else {
break;
}
} while (true);
$total = count($path);
for ($i = 0; $i < $total - 1; $i++) {
$results[] = mb_substr($metaData, $path[$i] + mb_strlen(' ' . ($i + 1) . '-'), $path[$i + 1] - $path[$i] - mb_strlen(' ' . ($i + 1) . '-'));
}
if ($total > 0) {
$i = $total - 1;
$results[] = mb_substr($metaData, $path[$i] + mb_strlen(' ' . ($i + 1) . '-'), mb_strlen($metaData) - $path[$i] - mb_strlen(' ' . ($i + 1) . '-'));
}
return $results;
}
/**
* 获取作者
*
* @return array
*/
private function getAuthor(string $text): array
{
$head = '"主题作者: ';
$tail = '"';
$results = [];
$offset = 0;
$hLen = mb_strlen($head);
$eLen = mb_strlen($tail);
while (false !== ($start = mb_stripos($text, $head, $offset))) {
$end = mb_stripos($text, $tail, $start + $hLen);
if ($end !== false) {
$results[] = mb_substr($text, $start + $hLen, $end - $start - $hLen);
$offset = $end + $eLen;
} else {
$offset = $start + $hLen;
}
}
return array_values(array_unique($results, SORT_LOCALE_STRING));
}
/**
* 获取最后回复人
*
* @return array
*/
private function getLastPosting(string $text): array
{
$head = '最后回复人: ';
$tail = '"';
$results = [];
$offset = 0;
$hLen = mb_strlen($head);
$eLen = mb_strlen($tail);
while (false !== ($start = mb_stripos($text, $head, $offset))) {
$end = mb_stripos($text, $tail, $start + $hLen);
if ($end !== false) {
$results[] = mb_substr($text, $start + $hLen, $end - $start - $hLen);
$offset = $end + $eLen;
} else {
$offset = $start + $hLen;
}
}
return array_values(array_unique($results, SORT_LOCALE_STRING));
}
}