Browse Source

added video results , added replace_yt to config , slightly improved css

hnhx 3 years ago
parent
commit
d33f027606
10 changed files with 147 additions and 36 deletions
  1. 8 0
      config.php
  2. 13 5
      google.php
  3. 1 0
      index.xhtml
  4. 9 2
      results/text.php
  5. 39 0
      results/video.php
  6. 36 2
      search.php
  7. BIN
      static/image_result.png
  8. 41 27
      static/styles.css
  9. BIN
      static/text_result.png
  10. BIN
      static/video_result.png

+ 8 - 0
config.php

@@ -8,6 +8,14 @@
     // Results will be in this language
     $config_google_language = "en";
 
+    /*
+        youtube.com results will be replaced with the given invidious instance
+        Get online invidious instances from here: https://docs.invidious.io/Invidious-Instances.md
+        
+        Set as null if you don't want to replace YouTube results
+    */
+    $config_replace_yt_with_invidious = "yewtu.be";
+
     /*
         To send requests trough a proxy uncomment CURLOPT_PROXY and CURLOPT_PROXYTYPE:
 

+ 13 - 5
google.php

@@ -1,15 +1,18 @@
 <?php
         function get_google_results($query, $page, $type=0) 
         {
-            require_once "config.php";
+            require "config.php";
             require_once "results/image.php";
-            require_once "results/text.php"; 
+            require_once "results/text.php";
+            require_once "results/video.php"; 
 
             $query_encoded =  urlencode($query);
 
             $google = "https://www.google.$config_google_domain/search?&q=$query_encoded&start=$page&hl=$config_google_language";
             if ($type == 1)
                 $google .= "&tbm=isch";
+            else if ($type == 2)
+                $google .= "&tbm=vid";
                 
             $ch = curl_init($google);
             curl_setopt_array($ch, $config_curl_settings);
@@ -29,9 +32,14 @@
 
             switch ($type)
             {
-                case 0: return text_results($xpath); 
-                case 1: return image_results($xpath);
-                default: return text_results($xpath);
+                case 0: 
+                    return text_results($xpath); 
+                case 1: 
+                    return image_results($xpath);
+                case 2: 
+                    return video_results($xpath);
+                default: 
+                    return text_results($xpath);
             }
         }
 ?>

+ 1 - 0
index.xhtml

@@ -14,6 +14,7 @@
                 <h1>Libre<span style="color:#bd93f9;">X</span></h1>
                 <input type="text" name="q"/>
                 <input type="hidden" name="p" value="0"/>
+                <input type="hidden" name="type" value="0"/>
                 <input type="submit" style="display:none"/>
                 <div class="search-button-wrapper">
                     <button name="type" value="0" type="submit">Search with LibreX</button>

+ 9 - 2
results/text.php

@@ -3,6 +3,7 @@
     function text_results($xpath) 
     {
         require_once "tools.php";
+        require "config.php";
 
         $results = array();
 
@@ -17,14 +18,20 @@
                 if (end($results)["url"] == $url->textContent)
                     continue;
 
+            $url = $url->textContent;
+            if ($config_replace_yt_with_invidious != null)
+            {
+                $url = str_replace("youtube.com", $config_replace_yt_with_invidious, $url);
+            }
+            
             $title = $xpath->evaluate(".//h3", $result)[0];
             $description = $xpath->evaluate(".//div[contains(@class, 'VwiC3b')]", $result)[0];
 
             array_push($results, 
                 array (
                     "title" => $title->textContent,
-                    "url" =>  $url->textContent,
-                    "base_url" => get_base_url($url->textContent),
+                    "url" =>  $url,
+                    "base_url" => get_base_url($url),
                     "description" => $description == null ? "No description was provided for this site." : $description->textContent
                 )
             );

+ 39 - 0
results/video.php

@@ -0,0 +1,39 @@
+<?php
+    function video_results($xpath)
+    {
+        require_once "tools.php";
+        require "config.php";
+
+        $results = array();
+
+        foreach($xpath->query("//div[@id='search']//div[contains(@class, 'g')]") as $result)
+        {
+            $url = $xpath->evaluate(".//a/@href", $result)[0];
+
+            if ($url == null)
+                continue;
+            
+            if (!empty($results)) // filter duplicate results
+                if (end($results)["url"] == $url->textContent)
+                    continue;
+
+            $url = $url->textContent;
+            if ($config_replace_yt_with_invidious != null)
+            {
+                $url = str_replace("youtube.com", $config_replace_yt_with_invidious, $url);
+            }
+            
+            $title = $xpath->evaluate(".//h3", $result)[0];
+            
+            array_push($results, 
+                array (
+                    "title" => $title->textContent,
+                    "url" =>  $url,
+                    "base_url" => get_base_url($url)
+                )
+            );
+        }
+
+        return $results;
+    }
+?>

+ 36 - 2
search.php

@@ -34,14 +34,19 @@
             ?>
             <button type="submit" style="display:none;"></button>
             <div class="result-change">
-                <button name="type" value="0">Text results</button>
-                <button name="type" value="1">Image results</button>
+                <button name="type" value="0"><img src="static/text_result.png" id="change-image" style="width:20px;">Text</button>
+                <button name="type" value="1"><img src="static/image_result.png" id="change-image">Images</button>
+                <button name="type" value="2"><img src="static/video_result.png" id="change-image" style="width:40px;">Videos</button>
             </div>
             
         <hr>
         </form>
 
         <?php
+            ini_set('display_errors', 1);
+            ini_set('display_startup_errors', 1);
+            error_reporting(E_ALL);
+
             function print_next_pages($page, $button_val, $q) 
             {
                 echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\" enctype=\"multipart/form-data\" autocomplete=\"off\">";
@@ -53,6 +58,7 @@
 
             require_once "google.php";
             require_once "tools.php";
+            require_once "config.php";
 
             $page = (int) htmlspecialchars($_REQUEST["p"]);
             $type = (int) $_REQUEST["type"];
@@ -63,6 +69,7 @@
 
             echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
             
+
             if ($type == 0) // text search
             {
                 check_for_special_search($query);
@@ -118,6 +125,33 @@
                     echo "</a>";
                 }
 
+                echo "</div>";
+            }
+            else if ($type == 2) // video search
+            {
+                echo "<div class=\"results-wrapper\">";
+
+                if ($config_replace_yt_with_invidious != null)
+                {
+                    echo "<p id=\"special-result\">";
+                    echo  "YouTube results got replaced with a privacy friendly Invidious instance.";
+                    echo "</p>";
+                }
+
+                foreach($results as $result)
+                {
+                    $title = $result["title"];
+                    $url = $result["url"];
+                    $base_url = $result["base_url"];
+
+                    echo "<div class=\"result-container\">";
+                    echo "<a href=\"$url\">";
+                    echo "$base_url";
+                    echo "<h2>$title</h2>";
+                    echo "</a>";
+                    echo "</div>";
+                }
+
                 echo "</div>";
             }
         ?>

BIN
static/image_result.png


+ 41 - 27
static/styles.css

@@ -5,7 +5,7 @@ html {
     font-family: Arial, Helvetica, sans-serif;
     font-size: 18px;
 
-    overflow-x: hidden; 
+    overflow-x: hidden;
 } 
 
 hr { 
@@ -198,6 +198,10 @@ img {
         text-decoration: underline; 
     }
 
+    .results-wrapper {
+        height: 105vh;
+    }
+
 /* .result-container END */
 
 
@@ -209,8 +213,6 @@ img {
         margin-right:8%;
         
         margin-bottom:30px;
-        
-        max-height: 100vh;
     }
 
 /* .image-result-container END */
@@ -262,7 +264,8 @@ img {
 
         border:none;
 
-        text-decoration: underline;
+        margin-right: 20px;
+
         font-size: 18px;
     }
 
@@ -270,6 +273,13 @@ img {
         cursor: pointer;
     }
 
+    .result-change #change-image {
+        width: 25px;
+        height: 25px;
+        vertical-align: middle;
+        border:none;
+    }
+
 /* .result-change END */
 
 
@@ -302,6 +312,27 @@ img {
 
 
 
+.result-container , 
+#time , 
+#special-result {
+    margin-left: 10%;
+}
+
+#special-result {
+    padding: 10px;
+    border: 1px solid #bdc1c6;
+    width: 500px;
+}
+
+#logo {
+    vertical-align: middle;
+    width: 80px;
+    height: 80px;
+    border: none;
+}
+
+
+
 /* @media START */
 
     @media only screen and (min-width:900px) {
@@ -381,7 +412,7 @@ img {
             margin-left: auto;
             margin-right: auto;
             text-align:  center;
-            margin-top: -10px;
+            margin-top: 5px;
         }
 
         img {
@@ -392,28 +423,11 @@ img {
             margin-bottom: 30px;
 
             width: 50%;
+        }
+
+        #special-result {
+            width: 80%;
         } 
     }
 
-/* @media END */
-
-
-
-.result-container , 
-#time , 
-#special-result {
-    margin-left: 10%;
-}
-
-#special-result {
-    padding: 10px;
-    border: 1px solid #bdc1c6;
-    width: 500px;
-}
-
-#logo {
-    vertical-align: middle;
-    width: 80px;
-    height: 80px;
-    border: none;
-}
+/* @media END */

BIN
static/text_result.png


BIN
static/video_result.png