mirror of
https://github.com/hnhx/librex.git
synced 2025-01-25 10:13:03 -03:00
slightly improved the API , made the file structure more organized
This commit is contained in:
parent
98c45eb1ab
commit
479aa0c3b1
10 changed files with 118 additions and 107 deletions
15
api.php
15
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);
|
||||
?>
|
|
@ -22,6 +22,7 @@
|
|||
<div class="info-container">
|
||||
<a href="/">LibreX</a>
|
||||
<a href="https://github.com/hnhx/librex/" target="_blank">Source code & Other instances</a>
|
||||
<a href="/api.php" target="_blank">API</a>
|
||||
<a href="/donate.xhtml" id="right">Donate ❤️</a>
|
||||
</div>
|
||||
</body>
|
||||
|
|
23
engines/special.php
Normal file
23
engines/special.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -26,6 +26,7 @@
|
|||
<div class="info-container">
|
||||
<a href="/">LibreX</a>
|
||||
<a href="https://github.com/hnhx/librex/" target="_blank">Source code & Other instances</a>
|
||||
<a href="/api.php" target="_blank">API</a>
|
||||
<a href="/donate.xhtml" id="right">Donate ❤️</a>
|
||||
</div>
|
||||
</body>
|
||||
|
|
77
misc/tools.php
Normal file
77
misc/tools.php
Normal file
|
@ -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>";
|
||||
}
|
||||
?>
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
function text_results($xpath)
|
||||
{
|
||||
require_once "tools.php";
|
||||
require_once "misc/tools.php";
|
||||
require "config.php";
|
||||
|
||||
$results = array();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
function video_results($xpath)
|
||||
{
|
||||
require_once "tools.php";
|
||||
require_once "misc/tools.php";
|
||||
require "config.php";
|
||||
|
||||
$results = array();
|
||||
|
|
96
search.php
96
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 & Other instances</a>
|
||||
<a href="/api.php" target="_blank">API</a>
|
||||
<a href="/donate.xhtml" id="right">Donate ❤️</a>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue