replace google images with qwant, added ddg bang seach, made the code for privacy frontends prettier
This commit is contained in:
parent
6aa9f9b9e7
commit
c847b0c874
10 changed files with 125 additions and 83 deletions
4
api.php
4
api.php
|
@ -30,8 +30,8 @@
|
|||
$results = get_text_results($query, $page);
|
||||
break;
|
||||
case 1:
|
||||
require "engines/google/image.php";
|
||||
$results = get_image_results($query_encoded);
|
||||
require "engines/qwant/image.php";
|
||||
$results = get_image_results($query_encoded, $page);
|
||||
break;
|
||||
case 2:
|
||||
require "engines/google/video.php";
|
||||
|
|
|
@ -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/102.0.5005.63 Safari/537.36",
|
||||
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
CURLOPT_CUSTOMREQUEST => "GET",
|
||||
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
<?php
|
||||
function get_image_results($query)
|
||||
{
|
||||
global $config;
|
||||
|
||||
$url = "https://www.google.$config->google_domain/search?&q=$query&hl=$config->google_language&tbm=isch";
|
||||
$response = request($url);
|
||||
$xpath = get_xpath($response);
|
||||
|
||||
$mh = curl_multi_init();
|
||||
$chs = $results = array();
|
||||
|
||||
foreach($xpath->query("//div[@class='isv-r PNCib MSM1fd BUooTd']") as $result)
|
||||
{
|
||||
$image = $xpath->evaluate(".//img[@data-src]", $result)[0];
|
||||
|
||||
$url = $xpath->evaluate(".//a/@href", $result)[0]->textContent;
|
||||
$url = check_for_privacy_frontend($url);
|
||||
|
||||
if (!empty($image))
|
||||
{
|
||||
$alt = $image->getAttribute("alt");
|
||||
$thumbnail = $image->getAttribute("data-src");
|
||||
|
||||
if (!empty($alt))
|
||||
{
|
||||
array_push($results,
|
||||
array (
|
||||
"thumbnail" => $thumbnail,
|
||||
"alt" => htmlspecialchars($alt),
|
||||
"url" => htmlspecialchars($url)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function print_image_results($results)
|
||||
{
|
||||
echo "<div class=\"image-result-container\">";
|
||||
|
||||
foreach($results as $result)
|
||||
{
|
||||
$thumbnail = $result["thumbnail"];
|
||||
$alt = $result["alt"];
|
||||
$url = $result["url"];
|
||||
|
||||
echo "<a title=\"$alt\" href=\"$url\" target=\"_blank\">";
|
||||
echo "<img src=\"engines/google/image_proxy.php?url=$thumbnail\">";
|
||||
echo "</a>";
|
||||
}
|
||||
|
||||
echo "</div>";
|
||||
}
|
||||
?>
|
|
@ -144,7 +144,7 @@
|
|||
if (array_key_exists("image", $special["special_response"]))
|
||||
{
|
||||
$image_url = $special["special_response"]["image"];
|
||||
echo "<img src=\"engines/google/image_proxy.php?url=$image_url\">";
|
||||
echo "<img src=\"image_proxy.php?url=$image_url\">";
|
||||
}
|
||||
echo $response;
|
||||
echo "<a href=\"$source\" target=\"_blank\">$source</a>";
|
||||
|
|
60
engines/qwant/image.php
Normal file
60
engines/qwant/image.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
function get_image_results($query, $page)
|
||||
{
|
||||
global $config;
|
||||
|
||||
$page = $page / 10 + 1; // qwant has a different page system
|
||||
|
||||
$url = "https://lite.qwant.com/?q=$query&t=images&p=$page";
|
||||
$response = request($url);
|
||||
$xpath = get_xpath($response);
|
||||
|
||||
$results = array();
|
||||
|
||||
foreach($xpath->query("//a[@rel='noopener']") as $result)
|
||||
{
|
||||
$image = $xpath->evaluate(".//img", $result)[0];
|
||||
|
||||
if ($image)
|
||||
{
|
||||
$encoded_url = $result->getAttribute("href");
|
||||
$encoded_url_split1 = explode("==/", $encoded_url)[1];
|
||||
$encoded_url_split2 = explode("?position", $encoded_url_split1)[0];
|
||||
$real_url = urldecode(base64_decode($encoded_url_split2));
|
||||
$real_url = check_for_privacy_frontend($real_url);
|
||||
|
||||
$alt = $image->getAttribute("alt");
|
||||
$thumbnail = urlencode($image->getAttribute("src"));
|
||||
|
||||
array_push($results,
|
||||
array (
|
||||
"thumbnail" => $thumbnail,
|
||||
"alt" => htmlspecialchars($alt),
|
||||
"url" => htmlspecialchars($real_url)
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function print_image_results($results)
|
||||
{
|
||||
echo "<div class=\"image-result-container\">";
|
||||
|
||||
foreach($results as $result)
|
||||
{
|
||||
$thumbnail = $result["thumbnail"];
|
||||
$alt = $result["alt"];
|
||||
$url = $result["url"];
|
||||
|
||||
echo "<a title=\"$alt\" href=\"$url\" target=\"_blank\">";
|
||||
echo "<img src=\"image_proxy.php?url=$thumbnail\">";
|
||||
echo "</a>";
|
||||
}
|
||||
|
||||
echo "</div>";
|
||||
}
|
||||
?>
|
|
@ -1,9 +1,10 @@
|
|||
<?php
|
||||
|
||||
$config = require "../../config.php";
|
||||
require "../../misc/tools.php";
|
||||
$config = require "config.php";
|
||||
require "misc/tools.php";
|
||||
|
||||
$image = $_REQUEST["url"];
|
||||
|
||||
$image_src = request($image);
|
||||
|
||||
header("Content-Type: image/jpeg");
|
|
@ -6,7 +6,7 @@
|
|||
return $base_url;
|
||||
}
|
||||
|
||||
function try_replace_with_frontend($url, $frontend, $tobereplaced)
|
||||
function try_replace_with_frontend($url, $frontend, $original)
|
||||
{
|
||||
$config = require "config.php";
|
||||
|
||||
|
@ -19,13 +19,13 @@
|
|||
else if (!empty($config->$frontend))
|
||||
$frontend = $config->$frontend;
|
||||
|
||||
if ($tobereplaced == "instagram.com")
|
||||
if ($original == "instagram.com")
|
||||
{
|
||||
if (!strpos($url, "/p/"))
|
||||
$frontend .= "/u";
|
||||
}
|
||||
|
||||
$url = $frontend . explode($tobereplaced, $url)[1];
|
||||
$url = $frontend . explode($original, $url)[1];
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
@ -35,22 +35,58 @@
|
|||
|
||||
function check_for_privacy_frontend($url)
|
||||
{
|
||||
if (strpos($url, "youtube.com"))
|
||||
$url = try_replace_with_frontend($url, "invidious", "youtube.com");
|
||||
else if (strpos($url, "instagram.com"))
|
||||
$url = try_replace_with_frontend($url, "bibliogram", "instagram.com");
|
||||
else if (strpos($url, "twitter.com"))
|
||||
$url = try_replace_with_frontend($url, "nitter", "twitter.com");
|
||||
else if (strpos($url, "reddit.com"))
|
||||
$url = try_replace_with_frontend($url, "libreddit", "reddit.com");
|
||||
else if (strpos($url, "tiktok.com"))
|
||||
$url = try_replace_with_frontend($url, "proxitok", "tiktok.com");
|
||||
else if (strpos($url, "wikipedia.org"))
|
||||
$url = try_replace_with_frontend($url, "wikiless", "wikipedia.org");
|
||||
$frontends = array(
|
||||
"youtube.com" => "invidious",
|
||||
"instagram.com" => "bibliogram",
|
||||
"twitter.com" => "nitter",
|
||||
"reddit.com" => "libreddit",
|
||||
"tiktok.com" => "proxitok",
|
||||
"wikipedia.org" => "wikiless"
|
||||
);
|
||||
|
||||
foreach($frontends as $original => $frontend)
|
||||
{
|
||||
if (strpos($url, $original))
|
||||
{
|
||||
$url = try_replace_with_frontend($url, $frontend, $original);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
function check_ddg_bang($query)
|
||||
{
|
||||
|
||||
$bangs_json = file_get_contents("static/misc/ddg_bang.json");
|
||||
$bangs = json_decode($bangs_json, true);
|
||||
|
||||
$search_word = substr(explode(" ", $query)[0], 1);
|
||||
$bang_url = null;
|
||||
|
||||
foreach($bangs as $bang)
|
||||
{
|
||||
if ($bang["t"] == $search_word)
|
||||
{
|
||||
$bang_url = $bang["u"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($bang_url)
|
||||
{
|
||||
$bang_query_array = explode("!" . $search_word, $query);
|
||||
$bang_query = trim(implode("", $bang_query_array));
|
||||
|
||||
$request_url = str_replace("{{{s}}}", $bang_query, $bang_url);
|
||||
$request_url = check_for_privacy_frontend($request_url);
|
||||
|
||||
header("Location: " . $request_url);
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
function get_xpath($response)
|
||||
{
|
||||
$htmlDom = new DOMDocument;
|
||||
|
|
|
@ -45,6 +45,8 @@
|
|||
switch ($type)
|
||||
{
|
||||
case 0:
|
||||
if (substr($query, 0, 1) == "!")
|
||||
check_ddg_bang($query);
|
||||
require "engines/google/text.php";
|
||||
$results = get_text_results($query, $page);
|
||||
print_elapsed_time($start_time);
|
||||
|
@ -52,8 +54,8 @@
|
|||
break;
|
||||
|
||||
case 1:
|
||||
require "engines/google/image.php";
|
||||
$results = get_image_results($query_encoded);
|
||||
require "engines/qwant/image.php";
|
||||
$results = get_image_results($query_encoded, $page);
|
||||
print_elapsed_time($start_time);
|
||||
print_image_results($results);
|
||||
break;
|
||||
|
@ -88,7 +90,7 @@
|
|||
}
|
||||
|
||||
|
||||
if ($type == 0 || $type == 2 )
|
||||
if ($type != 3)
|
||||
{
|
||||
echo "<div class=\"next-page-button-wrapper\">";
|
||||
|
||||
|
|
|
@ -220,7 +220,7 @@ a:hover, .text-result-wrapper h2:hover {
|
|||
|
||||
.image-result-container {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
|
||||
grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
|
||||
grid-gap: 1.5rem;
|
||||
justify-items: center;
|
||||
margin-left: 9%;
|
||||
|
|
1
static/misc/ddg_bang.json
Normal file
1
static/misc/ddg_bang.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue