qwant captcha detect
This commit is contained in:
		
							parent
							
								
									458bd8c1b5
								
							
						
					
					
						commit
						6eabc3edf4
					
				| @ -353,26 +353,17 @@ class qwant{ | |||||||
| 			"related" => [] | 			"related" => [] | ||||||
| 		]; | 		]; | ||||||
| 		 | 		 | ||||||
| 		if($json["status"] != "success"){ | 		if( | ||||||
|  | 			$json["status"] != "success" && | ||||||
|  | 			$json["data"]["error_code"] === 5 | ||||||
|  | 		){ | ||||||
| 			 | 			 | ||||||
| 			if($json["data"]["error_code"] === 5){ | 			// no results
 | ||||||
| 				 | 			return $out; | ||||||
| 				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"])){ | 		if(!isset($json["data"]["result"]["items"]["mainline"])){ | ||||||
| 			 | 			 | ||||||
| 			throw new Exception("Server did not return a result object"); | 			throw new Exception("Server did not return a result object"); | ||||||
| @ -654,10 +645,7 @@ class qwant{ | |||||||
| 			throw new Exception("Failed to decode JSON"); | 			throw new Exception("Failed to decode JSON"); | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		if($json["status"] != "success"){ | 		$this->detect_errors($json); | ||||||
| 			 |  | ||||||
| 			throw new Exception("Qwant returned an API error"); |  | ||||||
| 		} |  | ||||||
| 		 | 		 | ||||||
| 		if(isset($json["data"]["result"]["items"]["mainline"])){ | 		if(isset($json["data"]["result"]["items"]["mainline"])){ | ||||||
| 			 | 			 | ||||||
| @ -754,10 +742,7 @@ class qwant{ | |||||||
| 			throw new Exception("Could not parse JSON"); | 			throw new Exception("Could not parse JSON"); | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		if($json["status"] != "success"){ | 		$this->detect_errors($json); | ||||||
| 			 |  | ||||||
| 			throw new Exception("Qwant returned an API error"); |  | ||||||
| 		} |  | ||||||
| 		 | 		 | ||||||
| 		if(isset($json["data"]["result"]["items"]["mainline"])){ | 		if(isset($json["data"]["result"]["items"]["mainline"])){ | ||||||
| 			 | 			 | ||||||
| @ -861,10 +846,7 @@ class qwant{ | |||||||
| 			throw new Exception("Could not parse JSON"); | 			throw new Exception("Could not parse JSON"); | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		if($json["status"] != "success"){ | 		$this->detect_errors($json); | ||||||
| 			 |  | ||||||
| 			throw new Exception("Qwant returned an API error"); |  | ||||||
| 		} |  | ||||||
| 		 | 		 | ||||||
| 		if(isset($json["data"]["result"]["items"]["mainline"])){ | 		if(isset($json["data"]["result"]["items"]["mainline"])){ | ||||||
| 			 | 			 | ||||||
| @ -906,6 +888,28 @@ class qwant{ | |||||||
| 		return $out; | 		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){ | 	private function limitstrlen($text){ | ||||||
| 		 | 		 | ||||||
| 		return explode("\n", wordwrap($text, 300, "\n"))[0]; | 		return explode("\n", wordwrap($text, 300, "\n"))[0]; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 lolcat
						lolcat