special text results are now using curl_multi , removed image from wikipedia result because it slowed down things too much

This commit is contained in:
hnhx 2022-03-01 17:28:37 +01:00
parent 2265569b72
commit 075b30d2f7
12 changed files with 113 additions and 107 deletions

View File

@ -1,5 +1,5 @@
<?php
// This user agent will be used to access Google
// This user agent will be used when parsing the results
$config_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36";
// e.g.: fr -> https://google.fr/

View File

@ -1,5 +1,5 @@
<?php require "static/header.html"; ?>
<title>LibreX - Donate</title>
</head>
<body>
<div class="donate-container">

View File

@ -1,15 +1,89 @@
<?php
function check_for_special_search($query)
{
$query_lower = strtolower($query);
$split_query = explode(" ", $query);
if (strpos($query_lower, "to") && count($split_query) >= 4) // currency
{
$amount_to_convert = floatval($split_query[0]);
if ($amount_to_convert != 0)
return 1;
}
else if (strpos($query_lower, "mean") && count($split_query) >= 2) // definition
return 2;
else if (3 > count(explode(" ", $query))) // wikipedia
return 3;
return 0;
}
function get_text_results($query, $page=0)
{
require "config.php";
require "misc/tools.php";
$url = "https://www.google.$config_google_domain/search?&q=$query&start=$page&hl=$config_google_language";
$response = request($url);
$xpath = get_xpath($response);
$mh = curl_multi_init();
$query_lower = strtolower($query);
$query_encoded = urlencode($query);
$results = array();
$url = "https://www.google.$config_google_domain/search?&q=$query_encoded&start=$page&hl=$config_google_language";
$google_ch = curl_init($url);
curl_setopt_array($google_ch, $config_curl_settings);
curl_multi_add_handle($mh, $google_ch);
$special_search = $page == 0 ? check_for_special_search($query) : 0;
$special_ch = null;
$url = null;
if ($special_search != 0)
{
switch ($special_search)
{
case 1:
$url = "https://cdn.moneyconvert.net/api/latest.json";
break;
case 2:
$split_query = explode(" ", $query);
$reversed_split_q = array_reverse($split_query);
$word_to_define = $reversed_split_q[1];
$url = "https://api.dictionaryapi.dev/api/v2/entries/en/$word_to_define";
break;
case 3:
$url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&redirects=1&titles=$query_encoded";
break;
}
$special_ch = curl_init($url);
curl_setopt_array($special_ch, $config_curl_settings);
curl_multi_add_handle($mh, $special_ch);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
switch ($special_search)
{
case 1:
require "engines/special/currency.php";
currency_results($query, curl_multi_getcontent($special_ch));
break;
case 2:
require "engines/special/definition.php";
definition_results($query, curl_multi_getcontent($special_ch));
break;
case 3:
require "engines/special/wikipedia.php";
wikipedia_results($query, curl_multi_getcontent($special_ch));
break;
}
$xpath = get_xpath(curl_multi_getcontent($google_ch));
foreach($xpath->query("//div[@id='search']//div[contains(@class, 'g')]") as $result)
{
@ -23,10 +97,8 @@
continue;
$url = $url->textContent;
if ($config_replace_yt_with_invidious != null)
{
$url = str_replace("youtube.com", $config_replace_yt_with_invidious, $url);
}
if ($config_replace_yt_with_invidious != null && strpos($url, "youtube.com"))
$url = "https://" . $config_replace_yt_with_invidious . explode("youtube.com", $url)[1];
$title = $xpath->evaluate(".//h3", $result)[0];
$description = $xpath->evaluate(".//div[contains(@class, 'VwiC3b')]", $result)[0];
@ -49,8 +121,6 @@
function print_text_results($results)
{
global $query , $page;
//check_for_special_search($query);
echo "<div class=\"text-result-container\">";
foreach($results as $result)

View File

@ -22,10 +22,8 @@
continue;
$url = $url->textContent;
if ($config_replace_yt_with_invidious != null)
{
$url = str_replace("youtube.com", $config_replace_yt_with_invidious, $url);
}
if ($config_replace_yt_with_invidious != null && strpos($url, "youtube.com"))
$url = "https://" . $config_replace_yt_with_invidious . explode("youtube.com", $url)[1];
$title = $xpath->evaluate(".//h3", $result)[0];

View File

@ -1,38 +1,30 @@
<?php
function currency_results($query)
function currency_results($query, $response)
{
require "config.php";
require_once "misc/tools.php";
$split_query = explode(" ", $query);
if (count($split_query) >= 4)
{
$amount_to_convert = floatval($split_query[0]);
if ($amount_to_convert != 0)
{
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$url = "https://cdn.moneyconvert.net/api/latest.json";
$response = request($url);
$json_response = json_decode($response, true);
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$amount_to_convert = floatval($split_query[0]);
$json_response = json_decode($response, true);
$rates = $json_response["rates"];
$rates = $json_response["rates"];
if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
{
$base_currency_response = $rates[$base_currency];
$currency_to_convert_response = $rates[$currency_to_convert];
if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
{
$base_currency_response = $rates[$base_currency];
$currency_to_convert_response = $rates[$currency_to_convert];
$conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
$conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
echo "<p class=\"special-result-container\">";
echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
echo "</p>";
}
}
}
echo "<p class=\"special-result-container\">";
echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
echo "<a href=\"https://moneyconvert.net/\" target=\"_blank\">moneyconvert.net</a>";
echo "</p>";
}
}
?>

View File

@ -1,18 +1,13 @@
<?php
function definition_results($query)
function definition_results($query, $response)
{
require "config.php";
require_once "misc/tools.php";
require "config.php";
require_once "misc/tools.php";
$split_query = explode(" ", $query);
if (count($split_query) >= 2)
{
$split_query = explode(" ", $query);
$reversed_split_q = array_reverse($split_query);
$word_to_define = $reversed_split_q[1];
$url = "https://api.dictionaryapi.dev/api/v2/entries/en/$word_to_define";
$response = request($url);
$json_response = json_decode($response, true);
if (!array_key_exists("title", $json_response))
@ -22,8 +17,9 @@
echo "<p class=\"special-result-container\">";
echo "$word_to_define meaning<br/>";
echo "<br/>" . $definition . "<br/>";
echo "<a href=\"https://dictionaryapi.dev/\" target=\"_blank\">dictionaryapi.dev</a>";
echo "</p>";
}
}
}
?>

View File

@ -1,13 +1,11 @@
<?php
function wikipedia_results($query)
function wikipedia_results($query, $response)
{
require "config.php";
require_once "misc/tools.php";
$query_encoded = urlencode($query);
$url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded";
$response = request($url);
$json_response = json_decode($response, true);
$first_page = array_values($json_response["query"]["pages"])[0];
@ -20,23 +18,8 @@
return;
echo "<p class=\"special-result-container\">";
if (array_key_exists("thumbnail", $first_page))
{
$img_src = $first_page["thumbnail"]["source"];
$url = $first_page["thumbnail"]["source"];
$image_response = request($url);
$base64_image = base64_encode($image_response);
echo "<a href=\"data:image/jpeg;base64,$base64_image\" target=\"_blank\">";
echo "<img src=\"data:image/jpeg;base64,$base64_image\">";
echo "</a>";
}
echo "$description";
echo "<a href=\"https://en.wikipedia.org/wiki/$query\">";
echo "Wikipedia";
echo "</a>";
echo "<a href=\"https://en.wikipedia.org/wiki/$query\">wikipedia.org</a>";
echo "</p>";
}

View File

@ -1,5 +1,6 @@
<?php require "static/header.html"; ?>
<title>LibreX</title>
</head>
<body>
<form class="search-container" action="search.php" method="post" enctype="multipart/form-data" autocomplete="off">

View File

@ -38,28 +38,6 @@
<?php
require "config.php";
function check_for_special_search($query)
{
$query_lower = strtolower($query);
if (strpos($query_lower, "to"))
{
require "engines/special/currency.php";
currency_results($query);
}
else if (strpos($query_lower, "mean"))
{
require "engines/special/definition.php";
definition_results($query);
}
else if (3 > count(explode(" ", $query))) // long queries usually wont return a wiki result thats why this check exists
{
require "engines/special/wikipedia.php";
wikipedia_results($query_lower);
return;
}
}
$page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
$type = isset($_REQUEST["type"]) ? (int) $_REQUEST["type"] : 0;
@ -69,9 +47,7 @@
{
case 0:
require "engines/google/text.php";
$results = get_text_results($query_encoded, $page);
if ($page == 0)
check_for_special_search($query);
$results = get_text_results($query, $page);
print_text_results($results);
break;

View File

@ -2,7 +2,7 @@
<a href="/">LibreX</a>
<a href="https://github.com/hnhx/librex/" target="_blank">Source &amp; Instance list</a>
<a href="/api.php" target="_blank">API</a>
<a href="/donate.php" id="right">Donate ❤️</a>
<a href="/donate.php">Donate ❤️</a>
</div>
</body>
</html>

View File

@ -1,8 +1,8 @@
<!DOCTYPE html >
<html lang="en">
<head>
<meta http-equiv="Content-type" content="application/xhtml+xml;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta charset="UTF-8"/>
<meta name="description" content="A privacy respecting meta search engine."/>
<meta name="referrer" content="no-referrer"/>
<link rel="stylesheet" type="text/css" href="static/styles.css"/>

View File

@ -136,15 +136,6 @@ a:hover, .text-result-wrapper h2:hover {
margin-top: 1px;
}
.special-result-container img {
display: flex;
max-width: 50%;
max-height: 200px;
padding-bottom:10px;
margin-left:auto;
margin-right:auto;
}
.special-result-container a {
display: flex;
margin-top: 10px;
@ -273,7 +264,6 @@ a:hover, .text-result-wrapper h2:hover {
align-items: center;
}
.special-result-container {
max-width: 80%;
}