From 20079079726760d1fb79be85b60ef8a7584b440b Mon Sep 17 00:00:00 2001 From: lolcat Date: Thu, 23 May 2024 09:05:04 -0400 Subject: [PATCH] more error handling for yep --- scraper/yep.php | 95 ++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/scraper/yep.php b/scraper/yep.php index eba4214..3bd0842 100644 --- a/scraper/yep.php +++ b/scraper/yep.php @@ -350,21 +350,7 @@ class yep{ throw new Exception("Failed to fetch JSON"); } - // detect cloudflare page - $this->fuckhtml->load($json); - - if( - count( - $this->fuckhtml - ->getElementsByClassName( - "cf-wrapper", - "div" - ) - ) !== 0 - ){ - - throw new Exception("Blocked by Cloudflare"); - } + $this->detect_cf($json); $json = json_decode($json, true); //$json = json_decode(file_get_contents("scraper/yep.json"), true); @@ -539,26 +525,27 @@ class yep{ try{ $json = - json_decode( - $this->get( - $this->backend->get_ip(), // no nextpage! - "https://api.yep.com/fs/2/search", - [ - "client" => "web", - "gl" => $country == "all" ? $country : strtoupper($country), - "no_correct" => "false", - "q" => $search, - "safeSearch" => $nsfw, - "type" => "images" - ] - ), - true + $this->get( + $this->backend->get_ip(), // no nextpage! + "https://api.yep.com/fs/2/search", + [ + "client" => "web", + "gl" => $country == "all" ? $country : strtoupper($country), + "no_correct" => "false", + "q" => $search, + "safeSearch" => $nsfw, + "type" => "images" + ] ); }catch(Exception $error){ throw new Exception("Failed to fetch JSON"); } + $this->detect_cf($json); + + $json = json_decode($json, true); + if($json === null){ throw new Exception("Failed to decode JSON"); @@ -636,27 +623,27 @@ class yep{ // https://api.yep.com/fs/2/search?client=web&gl=CA&no_correct=false&q=undefined+variable+javascript&safeSearch=off&type=web $json = - json_decode( - $this->get( - $this->backend->get_ip(), - "https://api.yep.com/fs/2/search", - [ - "client" => "web", - "gl" => $country == "all" ? $country : strtoupper($country), - "limit" => "99999", - "no_correct" => "false", - "q" => $search, - "safeSearch" => $nsfw, - "type" => "news" - ] - ), - true + $this->get( + $this->backend->get_ip(), + "https://api.yep.com/fs/2/search", + [ + "client" => "web", + "gl" => $country == "all" ? $country : strtoupper($country), + "limit" => "99999", + "no_correct" => "false", + "q" => $search, + "safeSearch" => $nsfw, + "type" => "news" + ] ); }catch(Exception $error){ 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); 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){ $substr = substr($title, -4);