-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path14.php
74 lines (59 loc) · 1.93 KB
/
14.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
<?php
use Symfony\Component\DomCrawler\Crawler;
require __DIR__ . '/vendor/autoload.php';
function first($array) {
return reset($array);
}
function normalizeUrl($url) {
return 'http://yiiframework.ru/forum/'. ltrim($url, './');
}
function getHtml($url) {
$file = __DIR__ . '/cache/' . md5($url);
if (file_exists($file)) {
return file_get_contents($file);
} else {
$html = file_get_contents($url);
file_put_contents($file, $html);
return $html;
}
}
function crawler($url) {
return new Crawler(getHtml(normalizeUrl($url)));
}
function clearUrl($url) {
return preg_replace('#\&sid=.{32}#s', '', $url);
}
function getForumMaxPageNumber($forumUrl) {
return max(
first(crawler($forumUrl)
->filter('div.action-bar.bar-top .pagination li:nth-last-of-type(2)')
->each(function (Crawler $link) {
return intval($link->text());
})),
1
);
}
function getForumPages($forumUrl) {
echo 'Forum pages for ' . clearUrl($forumUrl) . PHP_EOL;
return array_map(function ($number) use ($forumUrl) {
return $forumUrl . ($number > 1 ? '&start=' . (25 * ($number - 1)) : '');
}, range(1, getForumMaxPageNumber($forumUrl)));
}
function getForumPageTopics($forumPageUrl) {
echo 'Forum page topics for ' . clearUrl($forumPageUrl) . PHP_EOL;
return crawler($forumPageUrl)
->filter('ul.topiclist.topics li dl')
->each(function (Crawler $topic) {
$link = $topic->filter('div.list-inner a.topictitle');
return [
'title' => $link->html(),
'url' => $link->attr('href'),
'count' => intval($topic->filter('dd.posts')->text()) + 1,
];
});
}
$forumUrl = './viewforum.php?f=28';
$forumPages = getForumPages($forumUrl);
$topics = array_map('getForumPageTopics', $forumPages);
echo clearUrl(print_r($topics, true));
echo PHP_EOL;