~hacktivista/4get

20079079726760d1fb79be85b60ef8a7584b440b — lolcat 6 months ago 92d0102
more error handling for yep
1 files changed, 51 insertions(+), 44 deletions(-)

M scraper/yep.php
M scraper/yep.php => scraper/yep.php +51 -44
@@ 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);