Browse Source

replace google images with qwant, added ddg bang seach, made the code for privacy frontends prettier

kysnigger 2 years ago
parent
commit
c847b0c874
10 changed files with 124 additions and 83 deletions
  1. 2 2
      api.php
  2. 1 1
      config.php.example
  3. 0 58
      engines/google/image.php
  4. 1 1
      engines/google/text.php
  5. 60 0
      engines/qwant/image.php
  6. 3 2
      image_proxy.php
  7. 51 15
      misc/tools.php
  8. 5 3
      search.php
  9. 1 1
      static/css/styles.css
  10. 0 0
      static/misc/ddg_bang.json

+ 2 - 2
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";

+ 1 - 1
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/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,

+ 0 - 58
engines/google/image.php

@@ -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>";
-    }
-?>

+ 1 - 1
engines/google/text.php

@@ -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 - 0
engines/qwant/image.php

@@ -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>";
+    }
+?>

+ 3 - 2
engines/google/image_proxy.php → image_proxy.php

@@ -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");

+ 51 - 15
misc/tools.php

@@ -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;

+ 5 - 3
search.php

@@ -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\">";
 

+ 1 - 1
static/css/styles.css

@@ -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%;

File diff suppressed because it is too large
+ 0 - 0
static/misc/ddg_bang.json


Some files were not shown because too many files changed in this diff