Browse Source

slightly improved the API , made the file structure more organized

hnhx 3 years ago
parent
commit
479aa0c3b1
10 changed files with 118 additions and 107 deletions
  1. 10 5
      api.php
  2. 1 0
      donate.xhtml
  3. 0 0
      engines/google.php
  4. 23 0
      engines/special.php
  5. 1 0
      index.xhtml
  6. 77 0
      misc/tools.php
  7. 1 1
      results/google/text.php
  8. 1 1
      results/google/video.php
  9. 4 92
      search.php
  10. 0 8
      tools.php

+ 10 - 5
api.php

@@ -1,13 +1,18 @@
 <?php
-    header('Content-Type: application/json');
-    
-    require "google.php";
+    require "engines/google.php";
+
+    if (!isset($_REQUEST["q"]))
+    {
+        echo "API usage: <a href=\"https://github.com/hnhx/librex/#api\">https://github.com/hnhx/librex/</a>";
+        die();
+    }
 
     $query = $_REQUEST["q"];
-    $page = (int) $_REQUEST["p"] * 10;
-    $type = (int) $_REQUEST["type"];
+    $page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
+    $type = isset($_REQUEST["type"]) ? (int) $_REQUEST["type"] : 0;
 
     $results = get_google_results($query, $page, $type);
 
+    header('Content-Type: application/json');
     echo json_encode($results, JSON_PRETTY_PRINT);
 ?>

+ 1 - 0
donate.xhtml

@@ -22,6 +22,7 @@
         <div class="info-container">
             <a href="/">LibreX</a>
             <a href="https://github.com/hnhx/librex/" target="_blank">Source code &amp; Other instances</a>
+            <a href="/api.php" target="_blank">API</a>
             <a href="/donate.xhtml" id="right">Donate ❤️</a>
         </div>
     </body>

+ 0 - 0
google.php → engines/google.php


+ 23 - 0
engines/special.php

@@ -0,0 +1,23 @@
+<?php
+    function check_for_special_search($query)
+    {
+        $query_lower = strtolower($query);
+
+        if (strpos($query_lower, "to"))
+        {
+            require_once "results/special/currency.php";
+            currency_results($query);
+        }
+        else if (strpos($query_lower, "mean"))
+        {
+            require_once "results/special/definition.php";
+            definition_results($query);
+        }
+        else if (5 > count(explode(" ", $query))) // long queries usually wont return a wiki result thats why this check exists
+        {
+            require_once "results/special/wikipedia.php";
+            wikipedia_results($query_lower);
+            return;
+        }
+    }
+?>

+ 1 - 0
index.xhtml

@@ -26,6 +26,7 @@
         <div class="info-container">
             <a href="/">LibreX</a>
             <a href="https://github.com/hnhx/librex/" target="_blank">Source code &amp; Other instances</a>
+            <a href="/api.php" target="_blank">API</a>
             <a href="/donate.xhtml" id="right">Donate ❤️</a>
         </div>
     </body>

+ 77 - 0
misc/tools.php

@@ -0,0 +1,77 @@
+<?php
+    function get_base_url($url)
+    {
+        $split_url = explode("/", $url);
+        $base_url = $split_url[0] . "//" . $split_url[2] . "/";
+        return $base_url;
+    }
+
+
+    function print_text_results($results) 
+    {
+        global $query , $page;
+
+        if ($page == 0)
+            check_for_special_search($query);
+        
+        foreach($results as $result)
+        {
+            $title = $result["title"];
+            $url = $result["url"];
+            $base_url = $result["base_url"];
+            $description = $result["description"];
+
+            echo "<div class=\"result-container\">";
+            echo "<a href=\"$url\">";
+            echo "$base_url";
+            echo "<h2>$title</h2>";
+            echo "</a>";
+            echo "<span>$description</span>";
+            echo "</div>";
+        }
+    }
+
+    function print_image_results($results)
+    {
+        echo "<div class=\"image-result-container\">";
+
+            foreach($results as $result)
+            {
+                $src = $result["base64"];
+                $alt = $result["alt"];
+
+                echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
+                echo "<img src=\"data:image/jpeg;base64,$src\" width=\"350\" height=\"200\">";
+                echo "</a>";
+            }
+
+            echo "</div>";
+    }
+
+    function print_video_results($results)
+    {
+            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>";
+            }
+    }
+
+    function print_next_page_button($page, $button_val, $q, $type) 
+    {
+        echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\" enctype=\"multipart/form-data\" autocomplete=\"off\">";
+        echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
+        echo "<input type=\"hidden\" name=\"q\" value=\"$q\" />";
+        echo "<input type=\"hidden\" name=\"type\" value=\"$type\" />";
+        echo "<button type=\"submit\">$button_val</button>";
+        echo "</form>"; 
+    }
+?>

+ 1 - 1
results/google/text.php

@@ -2,7 +2,7 @@
 
     function text_results($xpath) 
     {
-        require_once "tools.php";
+        require_once "misc/tools.php";
         require "config.php";
 
         $results = array();

+ 1 - 1
results/google/video.php

@@ -1,7 +1,7 @@
 <?php
     function video_results($xpath)
     {
-        require_once "tools.php";
+        require_once "misc/tools.php";
         require "config.php";
 
         $results = array();

+ 4 - 92
search.php

@@ -45,100 +45,11 @@
 
         <?php
 
-            require_once "google.php";
+            require_once "engines/google.php";
+            require_once "engines/special.php";
+            require_once "misc/tools.php";
             require_once "config.php";
 
-            function check_for_special_search($query)
-            {
-                $query_lower = strtolower($query);
-
-                if (strpos($query_lower, "to"))
-                {
-                    require_once "results/special/currency.php";
-                    currency_results($query);
-                }
-                else if (strpos($query_lower, "mean"))
-                {
-                    require_once "results/special/definition.php";
-                    definition_results($query);
-                }
-                else if (5 > count(explode(" ", $query))) // long queries usually wont return a wiki result thats why this check exists
-                {
-                    require_once "results/special/wikipedia.php";
-                    wikipedia_results($query_lower);
-                    return;
-                }
-            }
-
-
-            function print_next_page_button($page, $button_val, $q, $type) 
-            {
-                echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\" enctype=\"multipart/form-data\" autocomplete=\"off\">";
-                echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
-                echo "<input type=\"hidden\" name=\"q\" value=\"$q\" />";
-                echo "<input type=\"hidden\" name=\"type\" value=\"$type\" />";
-                echo "<button type=\"submit\">$button_val</button>";
-                echo "</form>"; 
-            }
-
-            function print_text_results($results) 
-            {
-                global $query , $page;
-
-                if ($page == 0)
-                    check_for_special_search($query);
-                
-                foreach($results as $result)
-                {
-                    $title = $result["title"];
-                    $url = $result["url"];
-                    $base_url = $result["base_url"];
-                    $description = $result["description"];
-
-                    echo "<div class=\"result-container\">";
-                    echo "<a href=\"$url\">";
-                    echo "$base_url";
-                    echo "<h2>$title</h2>";
-                    echo "</a>";
-                    echo "<span>$description</span>";
-                    echo "</div>";
-                }
-            }
-
-            function print_image_results($results)
-            {
-                echo "<div class=\"image-result-container\">";
-
-                    foreach($results as $result)
-                    {
-                        $src = $result["base64"];
-                        $alt = $result["alt"];
-        
-                        echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
-                        echo "<img src=\"data:image/jpeg;base64,$src\" width=\"350\" height=\"200\">";
-                        echo "</a>";
-                    }
-
-                    echo "</div>";
-            }
-
-            function print_video_results($results)
-            {
-                    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>";
-                    }
-            }
-
             $page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
             $type = isset($_REQUEST["type"]) ? (int) $_REQUEST["type"] : 0;
 
@@ -191,6 +102,7 @@
         <div class="info-container">
             <a href="/">LibreX</a>
             <a href="https://github.com/hnhx/librex/" target="_blank">Source code &amp; Other instances</a>
+            <a href="/api.php" target="_blank">API</a>
             <a href="/donate.xhtml" id="right">Donate ❤️</a>
         </div>
     </body>

+ 0 - 8
tools.php

@@ -1,8 +0,0 @@
-<?php
-    function get_base_url($url)
-    {
-        $split_url = explode("/", $url);
-        $base_url = $split_url[0] . "//" . $split_url[2] . "/";
-        return $base_url;
-    }
-?>