diff --git a/api.php b/api.php index 9bee3c6..5226273 100644 --- a/api.php +++ b/api.php @@ -4,7 +4,15 @@ if (!isset($_REQUEST["q"])) { - echo "API usage: https://github.com/hnhx/librex/"; + echo "

Example API request: ./api.php?q=gentoo&p=2&type=0

+
+

\"q\" is the keyword

+

\"p\" is the result page (the first page is 0)

+

\"type\" is the search type (0=text, 1=image, 2=video, 3=torrent)

+
+

The results are going to be in JSON format.

+

The API supports both POST and GET requests.

"; + die(); } @@ -44,6 +52,6 @@ break; } - header('Content-Type: application/json'); + header("Content-Type: application/json"); echo json_encode($results, JSON_PRETTY_PRINT); ?> \ No newline at end of file diff --git a/config.php.example b/config.php.example index f274ed2..b0d616a 100644 --- a/config.php.example +++ b/config.php.example @@ -39,7 +39,7 @@ // CURLOPT_PROXYTYPE => CURLPROXY_HTTP, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", - CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36", + CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36", CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP, diff --git a/engines/google/image.php b/engines/google/image.php index 3cf7361..5604de7 100644 --- a/engines/google/image.php +++ b/engines/google/image.php @@ -8,21 +8,33 @@ $xpath = get_xpath($response); $mh = curl_multi_init(); - $chs = $alts = $results = array(); + $chs = $results = array(); - foreach($xpath->query("//img[@data-src]") as $image) + foreach($xpath->query("//div[@class='isv-r PNCib MSM1fd BUooTd']") as $result) { - $alt = $image->getAttribute("alt"); - $src = $image->getAttribute("data-src"); + $image = $xpath->evaluate(".//img[@data-src]", $result)[0]; + $url = $xpath->evaluate(".//a/@href", $result)[0]->textContent; - if (!empty($alt)) + if (!empty($image)) { - $ch = curl_init($src); - curl_setopt_array($ch, $config->curl_settings); - array_push($chs, $ch); - curl_multi_add_handle($mh, $ch); + $alt = $image->getAttribute("alt"); + $src = $image->getAttribute("data-src"); - array_push($alts, $alt); + if (!empty($alt)) + { + $ch = curl_init($src); + curl_setopt_array($ch, $config->curl_settings); + array_push($chs, $ch); + curl_multi_add_handle($mh, $ch); + + array_push($results, + array ( + "base64" => "", + "alt" => htmlspecialchars($alt), + "url" => htmlspecialchars($url) + ) + ); + } } } @@ -34,12 +46,7 @@ for ($i=0; count($chs)>$i; $i++) { $img_base64 = base64_encode(curl_multi_getcontent($chs[$i])); - array_push($results, - array ( - "base64" => $img_base64, - "alt" => htmlspecialchars($alts[$i]) - ) - ); + $results[$i]["base64"] = $img_base64; } return $results; @@ -53,8 +60,9 @@ { $src = $result["base64"]; $alt = $result["alt"]; + $url = $result["url"]; - echo ""; + echo ""; echo ""; echo ""; } diff --git a/misc/header.php b/misc/header.php index 7ee5040..aa05de9 100644 --- a/misc/header.php +++ b/misc/header.php @@ -1,7 +1,6 @@ -