forked from lolcat/4get
1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
lolcat 12d5b4ade8 Merge branch 'master' into master 2024-10-21 14:16:54 +00:00
lolcat 6eabc3edf4 qwant captcha detect 2024-10-09 18:34:36 -04:00
1 changed files with 33 additions and 29 deletions

View File

@ -353,25 +353,16 @@ class qwant{
"related" => []
];
if($json["status"] != "success"){
if($json["data"]["error_code"] === 5){
if(
$json["status"] != "success" &&
$json["data"]["error_code"] === 5
){
// no results
return $out;
}
if(isset($json["data"]["error_code"])){
switch($json["data"]["error_code"]){
case 27:
throw new Exception("Qwant returned a captcha");
break;
}
}
throw new Exception("Qwant returned an error code: " . $json["data"]["error_code"]);
}
$this->detect_errors($json);
if(!isset($json["data"]["result"]["items"]["mainline"])){
@ -654,10 +645,7 @@ class qwant{
throw new Exception("Failed to decode JSON");
}
if($json["status"] != "success"){
throw new Exception("Qwant returned an API error");
}
$this->detect_errors($json);
if(isset($json["data"]["result"]["items"]["mainline"])){
@ -754,10 +742,7 @@ class qwant{
throw new Exception("Could not parse JSON");
}
if($json["status"] != "success"){
throw new Exception("Qwant returned an API error");
}
$this->detect_errors($json);
if(isset($json["data"]["result"]["items"]["mainline"])){
@ -861,10 +846,7 @@ class qwant{
throw new Exception("Could not parse JSON");
}
if($json["status"] != "success"){
throw new Exception("Qwant returned an API error");
}
$this->detect_errors($json);
if(isset($json["data"]["result"]["items"]["mainline"])){
@ -906,6 +888,28 @@ class qwant{
return $out;
}
private function detect_errors($json){
if(
isset($json["status"]) &&
$json["status"] == "error"
){
if(isset($json["data"]["error_data"]["captchaUrl"])){
throw new Exception("Qwant returned a captcha");
}elseif(isset($json["data"]["error_data"]["error_code"])){
throw new Exception(
"Qwant returned an API error: " .
$json["data"]["error_data"]["error_code"]
);
}
throw new Exception("Qwant returned an API error");
}
}
private function limitstrlen($text){
return explode("\n", wordwrap($text, 300, "\n"))[0];