more error handling for yep

This commit is contained in:
lolcat 2024-05-23 09:05:04 -04:00
parent 92d0102738
commit 2007907972
1 changed files with 51 additions and 44 deletions

View File

@ -350,21 +350,7 @@ class yep{
throw new Exception("Failed to fetch JSON"); throw new Exception("Failed to fetch JSON");
} }
// detect cloudflare page $this->detect_cf($json);
$this->fuckhtml->load($json);
if(
count(
$this->fuckhtml
->getElementsByClassName(
"cf-wrapper",
"div"
)
) !== 0
){
throw new Exception("Blocked by Cloudflare");
}
$json = json_decode($json, true); $json = json_decode($json, true);
//$json = json_decode(file_get_contents("scraper/yep.json"), true); //$json = json_decode(file_get_contents("scraper/yep.json"), true);
@ -539,7 +525,6 @@ class yep{
try{ try{
$json = $json =
json_decode(
$this->get( $this->get(
$this->backend->get_ip(), // no nextpage! $this->backend->get_ip(), // no nextpage!
"https://api.yep.com/fs/2/search", "https://api.yep.com/fs/2/search",
@ -551,14 +536,16 @@ class yep{
"safeSearch" => $nsfw, "safeSearch" => $nsfw,
"type" => "images" "type" => "images"
] ]
),
true
); );
}catch(Exception $error){ }catch(Exception $error){
throw new Exception("Failed to fetch JSON"); throw new Exception("Failed to fetch JSON");
} }
$this->detect_cf($json);
$json = json_decode($json, true);
if($json === null){ if($json === null){
throw new Exception("Failed to decode JSON"); throw new Exception("Failed to decode JSON");
@ -636,7 +623,6 @@ class yep{
// https://api.yep.com/fs/2/search?client=web&gl=CA&no_correct=false&q=undefined+variable+javascript&safeSearch=off&type=web // https://api.yep.com/fs/2/search?client=web&gl=CA&no_correct=false&q=undefined+variable+javascript&safeSearch=off&type=web
$json = $json =
json_decode(
$this->get( $this->get(
$this->backend->get_ip(), $this->backend->get_ip(),
"https://api.yep.com/fs/2/search", "https://api.yep.com/fs/2/search",
@ -649,14 +635,15 @@ class yep{
"safeSearch" => $nsfw, "safeSearch" => $nsfw,
"type" => "news" "type" => "news"
] ]
),
true
); );
}catch(Exception $error){ }catch(Exception $error){
throw new Exception("Failed to fetch JSON"); throw new Exception("Failed to fetch JSON");
} }
$this->detect_cf($json);
$json = json_decode($json, true);
//$json = json_decode(file_get_contents("scraper/yep.json"), true); //$json = json_decode(file_get_contents("scraper/yep.json"), true);
if($json === null){ if($json === null){
@ -698,6 +685,26 @@ class yep{
} }
private function detect_cf($payload){
// detect cloudflare page
$this->fuckhtml->load($payload);
if(
count(
$this->fuckhtml
->getElementsByClassName(
"cf-wrapper",
"div"
)
) !== 0
){
throw new Exception("Blocked by Cloudflare");
}
}
private function titledots($title){ private function titledots($title){
$substr = substr($title, -4); $substr = substr($title, -4);