-
Notifications
You must be signed in to change notification settings - Fork 2
/
askSearch.php
143 lines (113 loc) · 3.94 KB
/
askSearch.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
<?php
class askResult{
private $title;
private $abstract;
private $link;
private $score;
private $pageNum;
public function getScore(){
return $this->score;
}
public function getTitle(){
return $this->title;
}
public function getAbstract(){
return $this->abstract;
}
public function getLink(){
return $this->link;
}
public function getPageNum(){
return $this->pageNum;
}
public function __construct($title,$link,$abstract,$pageNum,$rank){
$this->title=$title;
$this->link=$link;
$this->abstract=$abstract;
$this->pageNum=$pageNum;
$this->score=$rank;
}
}
class askParser{
public static function complexQueryAsk($q){
$q=str_replace(" NOT "," -",$q);
$q=urlencode("$q");
return $q;
}
public static function getAskSearchResultsByPage($query, $pageNum){
$objArray= array();
$url=askParser::formatAskQuery($query,$pageNum);
$urlContent=file_get_contents($url);
$formatContentStr=stristr($urlContent,"<div class=\"PartialSearchResults-item\" data-zen=\"true\">");
$formatContentStr=stristr($formatContentStr,"<script>",true);
$resultsInHtml=explode("class=\"PartialSearchResults-item\"",$formatContentStr);
$count=(($pageNum-1)*9);
foreach($resultsInHtml as $mkey=>$mvalue){
$res = new askResult(askParser::extract_title($mvalue),askParser::extract_link($mvalue)
,askParser::extract_abstract($mvalue),$pageNum,$count);
if((empty($res->getTitle())||empty($res->getLink())))
continue;
//Add res object to the array
$objArray[]=$res;
$count++;
}
return $objArray;
}
public static function getAggregatedSearchResults($query, $resultsNum){
$objArray= array();
$i=1;
while(count($objArray)<=$resultsNum){
$newArr=askParser::getAskSearchResultsByPage($query,$i);
//No more results available so break
if(count($newArr)<1)
break;
//Still more pages needs to be loaded to reach the required results number
if(count($newArr)+count($objArray)<=$resultsNum)
$objArray=array_merge($objArray,$newArr);
//Pick certain amount of results to reach the required number from the last page to be loaded
else{
$j=count($objArray);
foreach ($newArr as $key => $value) {
if($j++<$resultsNum){
$objArray[]=$value;
}
}
break;
}
$i++;
}
return $objArray;
}
public static function extract_title($htmlStr){
$title1=stristr(stristr($htmlStr,"class=\"PartialSearchResults-item-title-link result-link\""),"</",true);
$title1=stristr($title1,">");
$title1=str_replace(">", "", $title1);
return $title1;
}
public static function extract_abstract($htmlStr){
$abstract=stristr(stristr($htmlStr,"class=\"PartialSearchResults-item-abstract\""),"</",true);
$abstract=stristr($abstract,">");
$abstract=str_replace(">", "", $abstract);
return $abstract;
}
public static function extract_link($htmlStr){
$link="";
$link[0]=" ";
$link=stristr(stristr($htmlStr,"class=\"PartialSearchResults-item-url\""),"</",true);
$link=stristr($link,">");
$link=str_replace(">", "", $link);
$charArr=str_split($link);
if($charArr[0]=='.'){
$link=substr($link, 1);
}
return "http://".$link;
}
public static function formatAskQuery($query, $pageNum){
$q="http://www.ask.com/web?q=";
$q.=askParser::complexQueryAsk($query);
$q.="&qsrc=998&page=";
$q.=$pageNum;
return $q;
}
}
?>