Skip to content

Commit

Permalink
refactor(*): everything (merge pull request #18 from davidovski/the_r…
Browse files Browse the repository at this point in the history
…efactor)

Major Refactor
  • Loading branch information
Ahwxorg authored Aug 25, 2023
2 parents 85ef67c + a7d1836 commit 2c36d28
Show file tree
Hide file tree
Showing 34 changed files with 1,175 additions and 1,372 deletions.
56 changes: 5 additions & 51 deletions api.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
$config = require "config.php";
require "misc/tools.php";
require "misc/search_engine.php";

if (!isset($_REQUEST["q"]))
{
$opts = load_opts();

if (!$opts->query) {
echo "<p>Example API request: <a href=\"./api.php?q=gentoo&p=2&t=0\">./api.php?q=gentoo&p=2&t=0</a></p>
<br/>
<p>\"q\" is the keyword</p>
Expand All @@ -16,54 +17,7 @@
die();
}

$query = $_REQUEST["q"];
$query_encoded = urlencode($query);
$page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
$type = isset($_REQUEST["t"]) ? (int) $_REQUEST["t"] : 0;

$results = array();

switch ($type)
{
case 0:
$engine=$config->preferred_engines['text'];
if (is_null($engine))
$engine = "google";
require "engines/$engine/text.php";
$results = get_text_results($query, $page);
break;
case 1:
require "engines/qwant/image.php";
$results = get_image_results($query_encoded, $page);
break;
case 2:
require "engines/invidious/video.php";
$results = get_video_results($query_encoded);
break;
case 3:
if ($config->disable_bittorent_search)
$results = array("error" => "disabled");
else
{
require "engines/bittorrent/merge.php";
$results = get_merged_torrent_results($query_encoded);
}
break;
case 4:
if ($config->disable_hidden_service_search)
$results = array("error" => "disabled");
else
{
require "engines/ahmia/hidden_service.php";
$results = get_hidden_service_results($query_encoded);
}
break;
default:
require "engines/google/text.php";
$results = get_text_results($query_encoded, $page);
break;
}

$results = fetch_search_results($opts, false);
header("Content-Type: application/json");
echo json_encode($results);
?>
43 changes: 20 additions & 23 deletions config.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
// e.g.: fr -> https://google.fr/
"google_domain" => "com",

// Google results will be in this language
"google_language_site" => "",
"google_language_results" => "",
"google_number_of_results" => 10,

// You can set a language for results in wikipedia
"wikipedia_language" => "en",
// Results will be in this language
"language" => "",
"number_of_results" => 10,

// You can use any Invidious instance here
"invidious_instance_for_video_results" => "https://invidious.snopyta.org",
Expand All @@ -21,7 +17,8 @@
"disable_hidden_service_search" => false,

// Fallback to another librex instance if google search fails
"instance_fallback" => false, // This might generate a 504 Gateway Timeout error, we are looking into this.
// This may greatly increase the time it takes to get a result and in some cases results in 504 errors
"instance_fallback" => false,

/*
Preset privacy friendly frontends for users, these can be overwritten by users in the settings
Expand All @@ -31,74 +28,74 @@
"frontends" => array(
"invidious" => array(
"instance_url" => "",
"project_url" => "https://docs.invidious.io/instances/",
"project_url" => "https://docs.invidious.io/instances/",
"original_name" => "YouTube",
"original_url" => "youtube.com"
),
"rimgo" => array(
"instance_url" => "",
"project_url" => "https://codeberg.org/video-prize-ranch/rimgo#instances",
"project_url" => "https://codeberg.org/video-prize-ranch/rimgo#instances",
"original_name" => "Imgur",
"original_url" => "imgur.com"
),
"scribe" => array(
"instance_url" => "",
"project_url" => "https://git.sr.ht/~edwardloveall/scribe/tree/main/docs/instances.md",
"project_url" => "https://git.sr.ht/~edwardloveall/scribe/tree/main/docs/instances.md",
"original_name" => "Medium",
"original_url" => "medium.com"
),
"gothub" => array(
"instance_url" => "",
"project_url" => "https://codeberg.org/gothub/gothub#instances",
"project_url" => "https://codeberg.org/gothub/gothub#instances",
"original_name" => "GitHub",
"original_url" => "github.com"
),
"nitter" => array(
"instance_url" => "",
"project_url" => "https://github.com/zedeus/nitter/wiki/Instances",
"project_url" => "https://github.com/zedeus/nitter/wiki/Instances",
"original_name" => "Twitter",
"original_url" => "twitter.com"
),

"libreddit" => array(
"instance_url" => "",
"project_url" => "https://github.com/libreddit/libreddit-instances/blob/master/instances.md",
"project_url" => "https://github.com/libreddit/libreddit-instances/blob/master/instances.md",
"original_name" => "Reddit",
"original_url" => "reddit.com"
),
"proxitok" => array(
"instance_url" => "",
"project_url" => "https://github.com/pablouser1/ProxiTok/wiki/Public-instances",
"project_url" => "https://github.com/pablouser1/ProxiTok/wiki/Public-instances",
"original_name" => "TikTok",
"original_url" => "tiktok.com"
),
"wikiless" => array(
"instance_url" => "",
"project_url" => "https://github.com/Metastem/wikiless#instances",
"project_url" => "https://github.com/Metastem/wikiless#instances",
"original_name" => "Wikipedia",
"original_url" => "wikipedia.org"
),
"quetre" => array(
"instance_url" => "",
"project_url" => "https://github.com/zyachel/quetre#instances",
"project_url" => "https://github.com/zyachel/quetre#instances",
"original_name" => "Quora",
"original_url" => "quora.com"
),
"libremdb" => array(
"instance_url" => "",
"project_url" => "https://github.com/zyachel/libremdb#instances",
"project_url" => "https://github.com/zyachel/libremdb#instances",
"original_name" => "IMDb",
"original_url" => "imdb.com"
),
"breezewiki" => array(
"instance_url" => "",
"project_url" => "https://docs.breezewiki.com/Links.html",
"project_url" => "https://docs.breezewiki.com/Links.html",
"original_name" => "Fandom",
"original_url" => "fandom.com"
),
"anonymousoverflow" => array(
"instance_url" => "",
"project_url" => "https://github.com/httpjamesm/AnonymousOverflow#clearnet-instances",
"project_url" => "https://github.com/httpjamesm/AnonymousOverflow#clearnet-instances",
"original_name" => "StackOverflow",
"original_url" => "stackoverflow.com"
),
Expand All @@ -115,10 +112,10 @@
"original_url" => "goodreads.com"
)
),


"preferred_engines" => array(

/* replace with "text" => "duckduckgo" to use duckduckgo instead
* (recommended if being ratelimited */
"text" => "google"
Expand Down Expand Up @@ -150,7 +147,7 @@
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 18,
CURLOPT_TIMEOUT => 3,
CURLOPT_VERBOSE => false
)
);
Expand Down
77 changes: 32 additions & 45 deletions engines/ahmia/hidden_service.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
<?php
function get_hidden_service_results($query)
{
global $config;
require "engines/text/text.php";

$url = "https://ahmia.fi/search/?q=$query";
$response = request($url);
$xpath = get_xpath($response);

$results = array();

foreach($xpath->query("//ol[@class='searchResults']//li[@class='result']") as $result)
{
$url = "http://" . $xpath->evaluate(".//cite", $result)[0]->textContent;
$title = remove_special($xpath->evaluate(".//h4", $result)[0]->textContent);
$description = $xpath->evaluate(".//p", $result)[0]->textContent;

array_push($results,
array (
"title" => $title ? htmlspecialchars($title) : "No description provided",
"url" => htmlspecialchars($url),
"base_url" => htmlspecialchars(get_base_url($url)),
"description" => htmlspecialchars($description)
)
);
class TorSearch extends EngineRequest {
public function get_request_url() {
return "https://ahmia.fi/search/?q=" . urlencode($this->query);
}

return $results;
}

function print_hidden_service_results($results)
{
echo "<div class=\"text-result-container\">";

foreach($results as $result)
{
$title = $result["title"];
$url = $result["url"];
$base_url = $result["base_url"];
$description = $result["description"];

echo "<div class=\"text-result-wrapper\">";
echo "<a href=\"$url\">";
echo "$base_url";
echo "<h2>$title</h2>";
echo "</a>";
echo "<span>$description</span>";
echo "</div>";
public function get_results() {
$response = curl_multi_getcontent($this->ch);
$results = array();
$xpath = get_xpath($response);

if (!$xpath)
return $results;

foreach($xpath->query("//ol[@class='searchResults']//li[@class='result']") as $result)
{
$url = "http://" . $xpath->evaluate(".//cite", $result)[0]->textContent;
$title = remove_special($xpath->evaluate(".//h4", $result)[0]->textContent);
$description = $xpath->evaluate(".//p", $result)[0]->textContent;

array_push($results,
array (
"title" => $title ? htmlspecialchars($title) : "No description provided",
"url" => htmlspecialchars($url),
"base_url" => htmlspecialchars(get_base_url($url)),
"description" => htmlspecialchars($description)
)
);
}

return $results;
}

echo "</div>";
public static function print_results($results) {
TextSearch::print_results($results);
}
}
?>
58 changes: 32 additions & 26 deletions engines/bittorrent/1337x.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
<?php
$_1337x_url = "https://1337x.to/search/$query/1/";
class _1337xRequest extends EngineRequest {
public function get_request_url() {
$query = urlencode($this->query);
return "https://1337x.to/search/$query/1/";
}

function get_1337x_results($response)
{
global $config;
$xpath = get_xpath($response);
$results = array();
public function get_results() {
$response = curl_multi_getcontent($this->ch);

foreach($xpath->query("//table/tbody/tr") as $result)
{
$xpath = get_xpath($response);
$results = array();

$name = $xpath->evaluate(".//td[@class='coll-1 name']/a", $result)[1]->textContent;
$magnet = "./engines/bittorrent/get_magnet_1337x.php?url=https://1337x.to" . $xpath->evaluate(".//td[@class='coll-1 name']/a/@href", $result)[1]->textContent;
$size_unformatted = explode(" ", $xpath->evaluate(".//td[contains(@class, 'coll-4 size')]", $result)[0]->textContent);
$size = $size_unformatted[0] . " " . preg_replace("/[0-9]+/", "", $size_unformatted[1]);
$seeders = $xpath->evaluate(".//td[@class='coll-2 seeds']", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//td[@class='coll-3 leeches']", $result)[0]->textContent;
if (!$xpath)
return $results;

array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "1337x.to"
)
);
}
foreach($xpath->query("//table/tbody/tr") as $result) {
$name = $xpath->evaluate(".//td[@class='coll-1 name']/a", $result)[1]->textContent;
$magnet = "./engines/bittorrent/get_magnet_1337x.php?url=https://1337x.to" . $xpath->evaluate(".//td[@class='coll-1 name']/a/@href", $result)[1]->textContent;
$size_unformatted = explode(" ", $xpath->evaluate(".//td[contains(@class, 'coll-4 size')]", $result)[0]->textContent);
$size = $size_unformatted[0] . " " . preg_replace("/[0-9]+/", "", $size_unformatted[1]);
$seeders = $xpath->evaluate(".//td[@class='coll-2 seeds']", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//td[@class='coll-3 leeches']", $result)[0]->textContent;

return $results;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "1337x.to"
)
);
}

return $results;
}
}
?>
Loading

0 comments on commit 2c36d28

Please sign in to comment.