-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebpage_parser.php
268 lines (254 loc) · 7.42 KB
/
Webpage_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
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
<?php
define("FAIL", -1);
define("READMORE", 1);
include_once dirname(__FILE__)."/simple_html_dom.php";
class WebpageParser{
public $infile;
public $outfile;
//private $html;
public $AppsName;
public $AppsLang;
public $Description;
public $ParserName;
public function __construct($infile, $outfile){
$this->infile = $infile;
$this->outfile = $outfile;
$ret = mb_internal_encoding("UTF-8");
if ($ret == false){
fprintf(STDERR, "internal encoding fail\n");
}
$ret = mb_regex_encoding("UTF-8");
if ($ret == false){
fprintf(STDERR, "regex encoding fail\n");
}
}
public function __destruct(){
//system("rm $indexfile");
}
public static function GenerateNutchSeedURL($UrlsFileName){
//virtual
return false;
}
public function Parse(){
return false;
}
public function SpecialWrite($fp){
//virtual
}
public function ConstructUniqueKeyValue(){
//virtual
}
public function ConstructField($name, $value){
$s = sprintf("<field name=\"%s\">%s</field>", $name, $value);
return $s;
}
public function WriteToFile(){
$fp = fopen($this->outfile,"w");
if ($fp == NULL){
fprintf(STDERR, "%s can't be open(for write)\n", $this->outfile);
return false;
}
fprintf($fp, "<add>\n<doc>\n");
$unikey = $this->ConstructUniqueKeyValue();
$tmp = $this->ConstructField("id", $unikey);
fprintf($fp, "%s\n" ,$tmp);
$tmp = $this->ConstructField("AppsName", $this->AppsName);
fprintf($fp, "%s\n" ,$tmp);
$tmp = $this->ConstructField("AppsLang", $this->AppsLang);
fprintf($fp, "%s\n", $tmp);
$tmp = $this->ConstructField("Description", $this->Description);
fprintf($fp, "%s\n", $tmp);
$this->SpecialWrite($fp);
fprintf($fp, "</doc>\n</add>\n");
fclose($fp);
}
public static function DomFind($html, $target){
$ret = $html->find($target);
if ($ret == NULL){
fprintf(STDERR, "html parse fail: ". $target." not found\n");
return NULL;
}else{
return $ret;
}
}
}
class MacUknowParser extends WebpageParser{
//public $Price;
public $SoftwareType;
public function __construct($infile, $outfile){
parent::__construct($infile,$outfile);
$this->ParserName = "MacUKnow";
}
public function Parse(){
try {
$html = file_get_html($this->infile);
$ret = $html->find("title");
$this->Description = $ret[0]->plaintext."\n";
$ret = $html->find("div[class=content]");
$ParseFlag = false;
foreach ($ret as $i => $block){
//echo "block ".$i."\n".$block->innertext."\n";
$content = str_get_html($block->innertext);
$flag = $content->find("fb:like");
if ($flag){
$des = preg_replace("/ /u", "", $block->plaintext);
$des = preg_replace("/&/u", "", $des);
$this->Description .= $des;
$spec = $content->find("p[class=rtecenter]");
if ($spec == NULL){
fprintf(STDERR, "<AppsName> not found, skip %s\n", $this->infile);
return false;
}
$spec_content = str_get_html($spec[1]->innertext);
$title = $spec_content->find("span[style]");
if ($title == NULL){
fprintf(STDERR, "<AppsName> not found, skip %s\n", $this->infile);
return false;
}
$this->AppsName = $title[0]->plaintext;
// because the format is not consistent
// I skip the AppsLang and Price
$ParseFlag = true;
}
}
return $ParseFlag;
}catch (Execption $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
public function SpecialWrite($fp){
//virtual
//fprintf($fp, "<Price>%s</Price>\n", $this->Price);
$tmp = $this->ConstructField("SoftwareType", $this->SoftwareType);
fprintf($fp, "%s\n",$tmp);
return ;
}
public static function GenerateNutchSeedURL($UrlsFileName){
try{
$homepage = file_get_contents("http://www.macuknow.com/iphoneapps");
$content = str_get_html($homepage);
$ret = parent::DomFind($content, "li[class=pager-last last]");
if ($ret ==NULL) {
return false;
}
$content = str_get_html($ret[0]->innertext);
$ret = parent::DomFind($content,"a[href]");
if ($ret ==NULL) {
return false;
}
$path = $ret[0]->href;
$ret = preg_match("/iphoneapps\?page=(.*)/u", $path, $matches);
if ($ret <= 0){
fprintf(STDERR, "%s doesn't match\n", $path);
return false;
}
$max = intval($matches[1]);
$fp = fopen($UrlsFileName, "w");
if ($fp == NULL){
fprintf(STDERR, "%s open fail\n", $UrlsFileName);
return false;
}
for ($i = 0;$i <= $max; $i++){
fprintf($fp, "http://www.macuknow.com/iphoneapps?page=%d\n",$i);
}
fclose($fp);
return true;
}catch (Execption $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
public function ConstructUniqueKeyValue(){
//virtual
return $this->ParserName."\t".$this->AppsName;
}
}
class iappParser extends WebpageParser{
public $Price;
public $SoftwareType;
public $Requirement;
public function __construct($infile, $outfile){
parent::__construct($infile,$outfile);
$this->ParserName = "iapp";
}
public function SpecialWrite($fp){
//virtual
$tmp = $this->ConstructField("Price", $this->Price);
fprintf($fp, "%s\n", $tmp);
$tmp = $this->ConstructField("SoftwareType", $this->SoftwareType);
fprintf($fp, "%s\n", $tmp);
$tmp = $this->ConstructField("Requirement", $this->Requirement);
fprintf($fp, "%s\n", $tmp);
return ;
}
public function Parse(){
try {
$html = file_get_html($this->infile);
$ret = parent::DomFind($html,"title");
$this->Description = $ret[0]->plaintext;
$ret = parent::DomFind($html, "div[id=app_show_data]");
if ($ret == NULL){
fprintf(STDERR, "skip %s\n", $this->infile);
return false;
}
$spec = str_get_html($ret[0]->innertext);
$title = parent::DomFind($spec, "h3");
$this->AppsName = $title[0]->plaintext;
$spec_ret = parent::DomFind($spec, "span[class=info]");
$this->SoftwareType = $spec_ret[0]->plaintext;
$this->Price = $spec_ret[1]->plaintext;
$this->Requirement = $spec_ret[4]->plaintext;
$this->AppsLang = $spec_ret[6]->plaintext;
$ret = parent::DomFind($html, "div[class=poster_papercontent]");
foreach ($ret as $i => $block){
//echo "block ".$i."\n".$block->innertext."\n";
$des = preg_replace("/ /u", "", $block->plaintext);
$des = preg_replace("/\r\n/u", "\n", $des);
$des = preg_replace("/&/u", " ", $des);
//$des = preg_replace("/\n/u", "", $des);
$des = preg_replace("/<\/embed>/u", "", $des);
$this->Description .= $des;
}
$ParseFlag = true;
return $ParseFlag;
}catch (Execption $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
public static function GenerateNutchSeedURL($UrlsFileName){
try{
$homepage = file_get_contents("http://iapp.com.tw/all_rating.php?d=2");
$content = str_get_html($homepage);
$ret = parent::DomFind($content, "div[class=paper getByCatPaper]");
if ($ret ==NULL) {
return false;
}
$content = str_get_html($ret[0]->innertext);
$ret = parent::DomFind($content, "a[href]");
if ($ret == NULL){
return false;
}
$max = intval($ret[9]->innertext);
$fp = fopen($UrlsFileName, "w");
if ($fp == NULL){
fprintf(STDERR, "%s open fail\n", $UrlsFileName);
return false;
}
for ($i = 0;$i <= $max; $i++){
fprintf($fp, "http://iapp.com.tw/all_rating.php?d=2&page=%d\n",$i);
}
fclose($fp);
return true;
}catch (Execption $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
public function ConstructUniqueKeyValue(){
//virtual
return $this->ParserName."\t".$this->AppsName;
}
}
?>