forked from upfor/tianya-post
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtianya.php
510 lines (430 loc) · 14.7 KB
/
tianya.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
require_once __DIR__ . '/vendor/autoload.php';
// 运行程序
(new TianYaV2())->start();
use Goutte\Client as GoutteClient;
use GuzzleHttp\Client as GuzzleClient;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\Show;
use Symfony\Component\DomCrawler\Crawler;
/**
* V2版本
*/
class TianYaV2
{
/**
* @var int 程序开始执行时间
*/
private $startTime;
/**
* @var Input
*/
private $input;
/**
* @var Output
*/
private $output;
/**
* @var string 传入的帖子URL
*/
private $url;
/**
* @var bool 纯净版(--type=1)
*/
private $fetchPure;
/**
* @var bool 完整版(--type=2)
*/
private $fetchFull;
/**
* @var bool Email(--type=3)
*/
private $fetchEmail;
/**
* @var string 解析后的帖子URL
*/
private $postUrl;
/**
* @var string 帖子类型
*/
private $postType;
/**
* @var string 帖子分类
*/
private $postCat;
/**
* @var int 帖子ID
*/
private $postId;
/**
* @var string 帖子标题
*/
private $title;
/**
* @var int 帖子分页数
*/
private $totalPage = 1;
/**
* @var GoutteClient
*/
private $client;
/**
* @var array 配置信息
*/
private $config;
/**
* @var string 数据存放目录
*/
private $dataDir = __DIR__ . '/data';
/**
* @var resource 含回复、评论的完整内容
*/
private $fileHandleGt;
/**
* @var resource 仅含楼主帖子的脱水版
*/
private $fileHandleLz;
/**
* @var resource 帖子&评论内容中解析出的邮箱地址
*/
private $fileHandleEmail;
/**
* @var Generator 进度条
*/
private $progressBar;
public function __construct()
{
$this->startTime = microtime(true);
$this->input = new Input();
$this->output = new Output();
$this->config = require __DIR__ . '/config.php';
$this->checkArgs();
$this->initClient();
$this->parsePostUrl();
}
/**
* 检查传参
*/
private function checkArgs()
{
// 检查URL
$this->url = $this->input->getCommand();
if (!$this->url) {
$this->output->error('帖子 URL 缺失');
exit;
}
// 输出内容, 传参:
// --type=1,2,3
// -t=1,2,3
$type = $this->input->getSameStringOpt(['t', 'type'], '1,2,3');
$this->fetchPure = strpos($type, '1') !== false;
$this->fetchFull = strpos($type, '2') !== false;
$this->fetchEmail = strpos($type, '3') !== false;
}
/**
* 解析帖子URL信息
*/
private function parsePostUrl()
{
[$this->postType, $this->postCat, $this->postId]
= explode('-', pathinfo(parse_url($this->url, PHP_URL_PATH), PATHINFO_FILENAME));
$this->postType = str_ireplace('_author', '', $this->postType);
$this->postUrl = "https://bbs.tianya.cn/m/{$this->postType}-{$this->postCat}-{$this->postId}-1.shtml";
}
/**
* 初始化帖子文件句柄
*/
private function initFileHandle()
{
if (!file_exists($this->dataDir)) {
mkdir($this->dataDir);
}
$types = [];
// 仅含楼主帖
if ($this->fetchPure) {
$fileLz = $this->dataDir . "/{$this->postCat}-{$this->postId}.楼主版.txt";
if (file_exists($fileLz)) {
unlink($fileLz);
}
$this->fileHandleLz = fopen($fileLz, 'w+');
$types[] = '<info>纯净版</info>';
}
// 含跟帖、评论
if ($this->fetchFull) {
$fileGt = $this->dataDir . "/{$this->postCat}-{$this->postId}.完整版.txt";
if (file_exists($fileGt)) {
unlink($fileGt);
}
$this->fileHandleGt = fopen($fileGt, 'w+');
$types[] = '<info>完整版</info>';
}
// 帖子中的邮件地址
if ($this->fetchEmail) {
$fileEmail = $this->dataDir . "/{$this->postCat}-{$this->postId}.email.txt";
if (file_exists($fileEmail)) {
unlink($fileEmail);
}
$this->fileHandleEmail = fopen($fileEmail, 'w+');
$types[] = '<info>Email</info>';
}
if (!$types) {
$this->output->error('没有输出类型');
exit;
}
$this->output->write('输出类型: ' . implode(', ', $types));
}
/**
* 关闭文件句柄
*/
private function closeFileHandle()
{
is_resource($this->fileHandleGt) && fclose($this->fileHandleGt);
is_resource($this->fileHandleLz) && fclose($this->fileHandleLz);
is_resource($this->fileHandleEmail) && fclose($this->fileHandleEmail);
}
/**
* 初始化HTTP请求客户端
*/
private function initClient()
{
$this->client = new GoutteClient();
$guzzleClient = new GuzzleClient($this->config['guzzle_client_config']);
$this->client->setClient($guzzleClient);
foreach ($this->config['headers'] as $name => $val) {
$this->client->setHeader($name, $val);
}
}
/**
* 进度条
*/
private function initProgressBar()
{
$this->progressBar = Show::dynamicText('执行完成...!', '');
}
/**
* 开始分析帖子
*
* @throws Exception
*/
public function start()
{
$this->fetchPostInfo();
$this->initFileHandle();
$this->writePostInfo();
$this->initProgressBar();
$this->fetchPostContent();
}
/**
* 获取帖子基本信息
*/
public function fetchPostInfo()
{
try {
$this->client->setHeader('Referer', 'bbs.tianya.cn');
$crawler = $this->client->request('GET', $this->postUrl);
$response = $this->client->getResponse();
$statusCode = $response->getStatus();
if ($statusCode != 200) {
$this->output->error('页面不存在');
exit;
}
$this->title = $crawler->filter('#j-post-content .title h1')->first()->text();
$this->output->write("帖子: <info>{$this->title}</info>");
$totalPageCrawler = $crawler->filter('#j-post-content .u-pager .page-txt input.txt');
if ($totalPageCrawler->count()) {
[, $this->totalPage] = explode('/', $totalPageCrawler->first()->attr('placeholder'));
}
$this->output->write("总共<info>{$this->totalPage}</info>页");
} catch (Exception $e) {
$this->output->error($e->getMessage());
exit;
}
}
private function writePostInfo()
{
$postTitle = <<<TITLE
{$this->title}
{$this->postUrl}
{$this->config['statement']}
\n\n
TITLE;
$this->fetchFull && fwrite($this->fileHandleGt, $postTitle);
$this->fetchPure && fwrite($this->fileHandleLz, $postTitle);
}
public function fetchPostContent()
{
for ($page = 1; $page <= $this->totalPage; $page++) {
$this->progressBar->send(date('[Y-m-d H:i:s] ') . "{$page}/{$this->totalPage}页");
$pageUrl = str_ireplace('-1.shtml', '-' . $page . '.shtml', $this->postUrl);
$tries = 0;
try {
START:
$tries++;
$crawler = $this->client->request('GET', $pageUrl);
} catch (Exception $e) {
if ($tries > 10) {
$this->output->error(sprintf('帖子API请求失败超过10次, 分页数(%d), URL: %s', $page, $pageUrl));
throw new $e;
}
goto START;
}
$crawler->filter('#j-post-content .content .item')->each(/**
* @throws Exception
*/ function (Crawler $node) use ($page) {
// 是否为楼主发的帖子
$isLz = $node->filter('.hd .info .author .u-badge')->count() ? ' [楼主]' : '';
$user = $node->attr('data-user'); // 发帖人名称
$lid = $node->attr('data-id'); // 第N楼
$replyId = $node->attr('data-replyid'); // 评论ID
$replyIdText = $replyId ? " [{$replyId}]" : '';
$commentText = '';
$this->progressBar->send(date('[Y-m-d H:i:s] ') . "{$page}/{$this->totalPage}页, 帖子{$lid}楼");
if ($lid > 0) {
$datetime = $node->attr('data-time');
$content = $node->filter('.bd .reply-div');
// 非纯净版,无需获取评论内容
if ($this->fetchFull || $this->fetchEmail) {
$commentText = $this->fetchPostComment($node, $page, $lid);
}
} else {
$datetime = $node->filter('.info .time')->text();
$content = $node->filter('.bd');
}
// 帖子内容,按段落获取后格式化
$contentList = [];
$content->filter('p')->each(function (Crawler $node) use (&$contentList) {
$text = trim($node->text());
if ($text) {
$contentList[] = $text;
} // 图片内容, 用图片地址替代
elseif ($node->filter('img')->count()) {
$img = $node->filter('img')->attr('src');
if (strpos($img, '//') === 0) {
$img = 'http:' . $img;
}
$contentList[] = $img;
}
});
$contentText = implode("\n\n", $contentList);
// 解析email地址
if ($this->fetchEmail) {
foreach ($this->parseEmail($contentText) as $email) {
// 邮箱,类型,帖子楼层,时间
fputcsv($this->fileHandleEmail, [$email, '帖子', $lid, $datetime]);
}
}
// 完整版
if ($this->fetchFull) {
$postGt = <<<POST
----------------------------------------
[{$lid}楼] [{$user}]{$isLz} [$datetime]
----------------------------------------
{$contentText}
{$commentText}
\n\n\n
POST;
fwrite($this->fileHandleGt, $postGt);
}
// 楼主脱水版
if ($isLz && $this->fetchPure) {
$postLz = <<<POST
----------------------------------------
[{$lid}楼] [{$user}]{$isLz} [$datetime]{$replyIdText}
----------------------------------------
{$contentText}
\n\n\n
POST;
fwrite($this->fileHandleLz, $postLz);
}
});
}
// 发送false表示结束
$this->progressBar->send(false);
}
/**
* 获取本楼帖子评论
*
* @param Crawler $node 帖子维度的Node
* @return string
* @throws Exception
*/
private function fetchPostComment(Crawler $node, $page, $lid)
{
$commentText = '';
$replyId = $node->attr('data-replyid');
$item = is_numeric($this->postCat) ? 'develop' : $this->postCat;
if ($replyId && $node->filter('.bd .comments')->count()) {
$commentCount = $node->filter('.bd .comments')->attr('data-total');
$commentList = [];
// 只能每次10条
$commentPageTotal = ceil($commentCount / 10);
for ($num = 1; $num <= $commentPageTotal; $num++) {
$this->progressBar->send(date('[Y-m-d H:i:s] ') . "{$page}/{$this->totalPage}页, 帖子{$lid}楼, 评论{$num}/{$commentPageTotal}页");
$commentUrl = "https://bbs.tianya.cn/api?method=bbs.api.getCommentList¶ms.item={$item}¶ms.articleId={$this->postId}¶ms.replyId={$replyId}¶ms.pageNum={$num}";
// 尝试N次(避免因网络或接口响应慢导致中断)
$tries = 0;
try {
START:
$tries++;
$this->client->request('GET', $commentUrl);
} catch (Exception $e) {
if ($tries > 5) {
$this->output->error(sprintf('评论API请求失败超过10次, URL: %s', $commentUrl));
throw new $e;
}
goto START;
}
$response = $this->client->getResponse()->getContent();
$response = json_decode($response, true);
// 解析评论内容
foreach ((array)$response['data'] as $row) {
$ctime = date('Y-m-d H:i:s', strtotime($row['comment_time']));
$row['content'] = trim(strip_tags($row['content']));
$commentList[] = <<<COMMENT
[{$row['author_name']}] [{$ctime}]
>>>
{$row['content']}
COMMENT;
// 解析email地址
if ($this->fetchEmail) {
foreach ($this->parseEmail($row['content']) as $email) {
// 邮箱,类型,评论ID,时间
fputcsv($this->fileHandleEmail, [$email, '评论', $row['id'], $ctime]);
}
}
}
// 延迟10ms
usleep(10000);
}
if (count($commentList)) {
$commentText .= "\n\n↓↓↓↓↓↓↓↓【本楼评论】↓↓↓↓↓↓↓↓\n";
$commentText .= implode("\n\n ************************\n", $commentList);
}
}
return $commentText;
}
public function __destruct()
{
$this->closeFileHandle();
$this->output->write("结束时间: <info>" . date('Y-m-d H:i:s') . "</info>");
$useTime = $second = round(microtime(true) - $this->startTime, 3); // 任务用时
$minute = '';
if ($useTime > 60) {
$minute = "<info>" . floor($useTime / 60) . "</info>分";
$second = $useTime % 60;
}
$this->output->write("用时{$minute}<info>{$second}</info>秒");
}
/**
* 解析文本中的邮箱地址
*
* @param string $str
* @return array
*/
function parseEmail($str)
{
preg_match_all('/[a-zA-Z0-9_.-]{2,}@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}/i', $str, $matches);
return array_unique($matches[0]);
}
}