mirror of
https://github.com/hnhx/librex.git
synced 2025-04-29 14:09:27 -04:00
improved the wikipedia search , made the file structure more organized
This commit is contained in:
parent
7e5f51f38e
commit
98c45eb1ab
7 changed files with 77 additions and 86 deletions
46
results/special/wikipedia.php
Normal file
46
results/special/wikipedia.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
function wikipedia_results($query)
|
||||
{
|
||||
require "config.php";
|
||||
|
||||
$query_encoded = urlencode($query);
|
||||
|
||||
$ch = curl_init("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded");
|
||||
curl_setopt_array($ch, $config_curl_settings);
|
||||
$response = curl_exec($ch);
|
||||
|
||||
$json_response = json_decode($response, true);
|
||||
|
||||
$first_page = array_values($json_response["query"]["pages"])[0];
|
||||
|
||||
if (!array_key_exists("missing", $first_page))
|
||||
{
|
||||
$description = substr($first_page["extract"], 0, 250) . "...";
|
||||
|
||||
if (strpos($description, "may refer to"))
|
||||
return;
|
||||
|
||||
echo "<p id=\"special-result\">";
|
||||
|
||||
if (array_key_exists("thumbnail", $first_page))
|
||||
{
|
||||
$img_src = $first_page["thumbnail"]["source"];
|
||||
$ch_image = curl_init($first_page["thumbnail"]["source"]);
|
||||
curl_setopt_array($ch_image, $config_curl_settings);
|
||||
$image_response = curl_exec($ch_image);
|
||||
$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\" id=\"wiki-image\">";
|
||||
echo "</a>";
|
||||
}
|
||||
|
||||
echo "$description";
|
||||
echo "<a id=\"wiki-link\" href=\"https://en.wikipedia.org/wiki/$query\">";
|
||||
echo "Wikipedia";
|
||||
echo "</a>";
|
||||
|
||||
echo "</p>";
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -1,59 +0,0 @@
|
|||
<?php
|
||||
function wikipedia_results($query)
|
||||
{
|
||||
require "config.php";
|
||||
|
||||
$query_encoded = urlencode($query);
|
||||
|
||||
$mh = curl_multi_init();
|
||||
|
||||
$ch_desc = curl_init("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=$query_encoded");
|
||||
curl_setopt_array($ch_desc, $config_curl_settings);
|
||||
curl_multi_add_handle($mh, $ch_desc);
|
||||
|
||||
$ch_image = curl_init("https://en.wikipedia.org/w/api.php?action=query&titles=$query_encoded&prop=pageimages&format=json&pithumbsize=1000");
|
||||
curl_setopt_array($ch_image, $config_curl_settings);
|
||||
curl_multi_add_handle($mh, $ch_image);
|
||||
|
||||
$running = null;
|
||||
do {
|
||||
curl_multi_exec($mh, $running);
|
||||
} while ($running);
|
||||
|
||||
$json_response_desc = json_decode(curl_multi_getcontent($ch_desc), true);
|
||||
$json_response_image = json_decode(curl_multi_getcontent($ch_image), true);
|
||||
|
||||
$first_page_desc = array_values($json_response_desc["query"]["pages"])[0];
|
||||
$first_page_image = array_values($json_response_image["query"]["pages"])[0];
|
||||
|
||||
if (!array_key_exists("missing", $first_page_desc))
|
||||
{
|
||||
$description = substr($first_page_desc["extract"], 0, 250) . "...";
|
||||
|
||||
if (strpos($description, "may refer to"))
|
||||
return;
|
||||
|
||||
echo "<p id=\"special-result\">";
|
||||
|
||||
if (array_key_exists("thumbnail", $first_page_image))
|
||||
{
|
||||
$img_src = $first_page_image["thumbnail"]["source"];
|
||||
$ch_image = curl_init($first_page_image["thumbnail"]["source"]);
|
||||
curl_setopt_array($ch_image, $config_curl_settings);
|
||||
$image_response = curl_exec($ch_image);
|
||||
$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\" id=\"wiki-image\">";
|
||||
echo "</a>";
|
||||
}
|
||||
|
||||
echo "$description";
|
||||
echo "<a id=\"wiki-link\" href=\"https://en.wikipedia.org/wiki/$query\">";
|
||||
echo "Wikipedia";
|
||||
echo "</a>";
|
||||
|
||||
echo "</p>";
|
||||
}
|
||||
}
|
||||
?>
|
30
search.php
30
search.php
|
@ -47,7 +47,29 @@
|
|||
|
||||
require_once "google.php";
|
||||
require_once "config.php";
|
||||
require_once "tools.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)
|
||||
{
|
||||
|
@ -61,8 +83,10 @@
|
|||
|
||||
function print_text_results($results)
|
||||
{
|
||||
global $query;
|
||||
check_for_special_search($query);
|
||||
global $query , $page;
|
||||
|
||||
if ($page == 0)
|
||||
check_for_special_search($query);
|
||||
|
||||
foreach($results as $result)
|
||||
{
|
||||
|
|
|
@ -340,11 +340,13 @@ button {
|
|||
border: none;
|
||||
display: flex;
|
||||
|
||||
width: 50%;
|
||||
max-width: 50%;
|
||||
max-height: 200px;
|
||||
|
||||
padding-bottom:10px;
|
||||
|
||||
margin-left:auto;
|
||||
margin-right:auto;
|
||||
}
|
||||
|
||||
#wiki-link {
|
||||
|
@ -420,7 +422,7 @@ button {
|
|||
margin-right: auto;
|
||||
margin-top: 30px;
|
||||
|
||||
width: 50%;
|
||||
width: 80%;
|
||||
}
|
||||
|
||||
.small-search-container a {
|
||||
|
|
22
tools.php
22
tools.php
|
@ -5,26 +5,4 @@
|
|||
$base_url = $split_url[0] . "//" . $split_url[2] . "/";
|
||||
return $base_url;
|
||||
}
|
||||
|
||||
function check_for_special_search($query)
|
||||
{
|
||||
$query_lower = strtolower($query);
|
||||
|
||||
if (strpos($query_lower, "to"))
|
||||
{
|
||||
require_once "results/currency.php";
|
||||
currency_results($query);
|
||||
}
|
||||
else if (strpos($query_lower, "mean"))
|
||||
{
|
||||
require_once "results/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/wikipedia.php";
|
||||
wikipedia_results($query_lower);
|
||||
return;
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue