/
m_parser.php
166 lines (130 loc) · 4.9 KB
/
m_parser.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
<?php
include_once("simple_html_dom.php");
include_once("s_book.php");
interface PtParser {
public static function parseContentFromThread(&$html);
public static function parseBookFromThread(&$html);
public static function parseBooksFromForum(&$html);
public static function parseForumPages(&$html);
}
class PtParserUtil {
public static function page_url_to_info($page_url)
{
$url = explode("-",$page_url);
return Array("novel_id"=>$url[1], "novel_page"=>$url[2]);
}
public static function stringToInt($s) {
$s = str_replace("k", "00", $s);
$s = str_replace("m", "00000", $s);
$s = str_replace(".", "", $s);
return +$s;
}
}
class PtParserCk101 implements PtParser{
public static function parseContentFromThread(&$html) {
$text = "";
$posts = $html->find("*[id^=postmessage_]");
foreach($posts as $post)
{
$text .= str_replace(" ", "", $post->plaintext);
}
return $text;
}
public static function parseBookFromThread(&$html) {
if ($html == null) {
return null;
}
$postlist = $html->find("div[id='postlist']", 0);
if ($postlist == null) {
return null;
}
$class = $postlist->find("h2",0)->plaintext;
$name = $postlist->find("h1",0)->plaintext;
$url = trim($html->find("link", 0)->href);
list($id, $current_pages) = sscanf($url, "http://ck101.com/thread-%d-%d-1.html");
$looks = $posts = $likes = 0;
$bookInfo = $html->find("div.authMsg", 0);
$looks = 0; $posts = 0; $likes = 0;
if ($bookInfo != null) {
$looks = $bookInfo->find(".viewNum", 0)->plaintext;
$posts = $bookInfo->find(".replayNum", 0)->plaintext;
$likes = $bookInfo->find(".thankNum", 0)->plaintext;
}
$pages = floor($posts/10) + 1;
$info = strstr($name, "已完") ? 1 : 0;
$source = "ck101";
$update_time = time();
return new Book($id, $name, $class, $posts, $pages,
$current_pages, $looks, $likes, $info, $source, $update_time);
}
public static function parseBooksFromForum(&$html) {
$books = array();
$threads = $html->find("*[id^=normalthread]");
foreach($threads as $key => $thread) {
$bookTitle = $thread->find("div.blockTitle", 0);
$numbers = $thread->find(".num", 0)->plaintext;
$id = $thread->tid;
$class = $bookTitle->find("em", 0)->plaintext;
$name = $bookTitle->find("a", 1)->plaintext;
if($class == "[版務公告]") {
continue;
}
list($likes, $posts, $looks) = array_values(array_filter(explode(" ", $numbers), function(&$v) {
$v = str_replace("/", "", $v);
$v = str_replace("感謝", "", $v);
$v = trim($v);
return $v != "" && $v != "\n";
}));
$likes = PtParserUtil::stringToInt($likes);
$posts = PtParserUtil::stringToInt($posts);
$looks = PtParserUtil::stringToInt($looks);
$pages = floor($posts/10) + 1;
$current_pages = 0;
$info = strstr($name, "已完") ? 1 : 0;
$source = "ck101";
$update_time = 0;
$books[] = new Book($id, $name, $class, $posts, $pages,
$current_pages, $looks, $likes, $info, $source, $update_time);
}
return $books;
}
public static function parseForumPages(&$html) {
$pages = @$html->find("a.last", 0)->plaintext;
if ($pages == "") {
$nxt = $html->find("a.nxt", 0);
$pages = $nxt == NULL ? 0 : $nxt->prev_sibling()->plaintext;
}
$pages = trim(str_replace(".", "", $pages));
return $pages;
}
}
class PtParserCk101Test {
var $parser;
public function PtParserCk101Test() {
$this->testParserBooksFromForum();
$this->testParseBookFromThread();
$this->testParseForumPages();
}
public function testParserBooksFromForum() {
//$html = file_get_html("test/forum.html");
// new version 2016/01/20 update
$search = "</span>\r\n </div>";
$replace = "</span>\r\n";
$htmlText = str_replace($search, $replace, file_get_contents("test/forum.html"));
$html = str_get_html($htmlText);
$r = PtParserCk101::parseBooksFromForum($html);
//print_r($r);
}
public function testParseBookFromThread() {
$html = file_get_html("test/thread.html");
$r = PtParserCk101::parseBookFromThread($html);
//print_r($r);
}
public function testParseForumPages() {
$html = file_get_html("test/forum.html");
$pages = PtParserCk101::parseForumPages($html);
//echo $pages;
}
}
//$test = new PtParserCk101Test();
?>