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
|
<?php
new autocomplete();
class autocomplete{
public function __construct(){
header("Content-Type: application/json");
$this->scrapers = [
"brave" => "https://search.brave.com/api/suggest?q={searchTerms}",
"ddg" => "https://duckduckgo.com/ac/?q={searchTerms}&type=list",
"yandex" => "https://suggest.yandex.com/suggest-ff.cgi?part={searchTerms}&uil=en&v=3&sn=5&lr=21276&yu=4861394161661655015",
"google" => "https://www.google.com/complete/search?client=mobile-gws-lite&q={searchTerms}",
"qwant" => "https://api.qwant.com/v3/suggest/?q={searchTerms}&client=opensearch",
"yep" => "https://api.yep.com/ac/?query={searchTerms}",
"marginalia" => "https://search.marginalia.nu/suggest/?partial={searchTerms}",
"yt" => "https://suggestqueries-clients6.youtube.com/complete/search?client=youtube&q={searchTerms}",
"sc" => "https://api-v2.soundcloud.com/search/queries?q={searchTerms}&client_id=ArYppSEotE3YiXCO4Nsgid2LLqJutiww&limit=10&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en"
];
/*
Sanitize input
*/
if(!isset($_GET["s"])){
$this->do404("Missing search(s) parameter");
}
if(is_string($_GET["s"]) === false){
$this->do404("Invalid search(s) parameter");
}
if(strlen($_GET["s"]) > 500){
$this->do404("Search(s) exceeds the 500 char length");
}
if(
isset($_GET["scraper"]) &&
is_string($_GET["scraper"]) === false
){
$_GET["scraper"] = "brave"; // default option
}
/*
Get $scraper
*/
if(!isset($_GET["scraper"])){
if(isset($_COOKIE["scraper_ac"])){
$scraper = $_COOKIE["scraper_ac"];
}else{
$scraper = "brave"; // default option
}
}else{
$scraper = $_GET["scraper"];
}
if($scraper == "disabled"){
// this shouldnt happen, but let's handle it anyways
$this->doempty();
}
// make sure it exists
if(!isset($this->scrapers[$scraper])){
$scraper = "brave"; // default option
}
// return results
switch($scraper){
case "google":
case "yt":
// handle google cause they want to be a special snowflake :(
$js = $this->get($this->scrapers[$scraper], $_GET["s"]);
preg_match(
'/\((\[.*\])\)/',
$js,
$js
);
if(!isset($js[1])){
$this->doempty();
}
$js = json_decode($js[1]);
$json = [];
foreach($js[1] as $item){
$json[] = htmlspecialchars_decode(strip_tags($item[0]));
}
echo json_encode(
[
$_GET["s"],
$json
]
);
break;
case "sc":
// soundcloud
$js = $this->get($this->scrapers[$scraper], $_GET["s"]);
$js = json_decode($js, true);
if(!isset($js["collection"])){
$this->doempty();
}
$json = [];
foreach($js["collection"] as $item){
$json[] = $item["query"];
}
echo json_encode(
[
$_GET["s"],
$json
]
);
break;
case "marginalia":
$json = $this->get($this->scrapers[$scraper], $_GET["s"]);
$json = json_decode($json, true);
if($json === null){
$this->doempty();
}
echo json_encode(
[
$_GET["s"],
$json
]
);
break;
default:
// if it respects the openSearch protocol
$json = json_decode($this->get($this->scrapers[$scraper], $_GET["s"]), true);
echo json_encode(
[
$_GET["s"],
$json[1] // ensure it contains valid key 0
]
);
break;
}
}
private function get($url, $query){
$curlproc = curl_init();
$url = str_replace("{searchTerms}", urlencode($query), $url);
curl_setopt($curlproc, CURLOPT_URL, $url);
curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
curl_setopt($curlproc, CURLOPT_HTTPHEADER,
["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip",
"DNT: 1",
"Connection: keep-alive",
"Sec-Fetch-Dest: empty",
"Sec-Fetch-Mode: cors",
"Sec-Fetch-Site: same-site"]
);
curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
$data = curl_exec($curlproc);
if(curl_errno($curlproc)){
throw new Exception(curl_error($curlproc));
}
curl_close($curlproc);
return $data;
}
private function do404($error){
echo json_encode(["error" => $error]);
die();
}
private function doempty(){
echo json_encode(
[
$_GET["s"],
[]
]
);
die();
}
}
|