Parcourir la source

added yts, nyaa torrent results, added torrent tracker config option

hnhx il y a 3 ans
Parent
commit
06f4f76959

+ 2 - 2
api.php

@@ -32,8 +32,8 @@
                 $results = array("error" => "disabled");
             else
             {
-                require "engines/bittorrent/thepiratebay.php";
-                $results = get_thepiratebay_results($query_encoded);
+                require "engines/bittorrent/merge.php";
+                $results = get_merged_torrent_results($query_encoded);
             }       
             break;
         default:

+ 2 - 0
config.php

@@ -11,6 +11,8 @@
     // Disable BitTorrent search
     $config_disable_bittorent_search = false;
 
+    $config_bittorent_trackers = "&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce";
+
     /*
         youtube.com results will be replaced with the given invidious instance
         Get online invidious instances from here: https://docs.invidious.io/Invidious-Instances.md

+ 46 - 0
engines/bittorrent/merge.php

@@ -0,0 +1,46 @@
+<?php
+
+    function get_merged_torrent_results($query)
+    {
+        require "engines/bittorrent/thepiratebay.php";
+        require "engines/bittorrent/nyaa.php";
+        require "engines/bittorrent/yts.php";
+
+        $results = array_merge(get_thepiratebay_results($query), 
+                               get_nyaa_results($query),
+                               get_yts_results($query));
+
+        $seeders = array_column($results, "seeders");
+        array_multisort($seeders, SORT_DESC, $results);
+        
+        return $results; 
+    }
+
+    function print_merged_torrent_results($results)
+    {
+        echo "<div class=\"text-result-container\">";
+
+            foreach($results as $result)
+            {
+                $source = $result["source"];
+                $name = $result["name"];
+                $magnet = $result["magnet"];
+                $seeders = $result["seeders"];
+                $leechers = $result["leechers"];
+                $size = $result["size"];
+
+                echo "<div class=\"text-result-wrapper\">";
+                echo "<a href=\"$magnet\">";
+                echo "$source";
+                echo "<h2>$name</h2>";
+                echo "</a>";
+                echo "<span>SE: <span style=\"color:#50fa7b\">$seeders</span> - ";
+                echo "LE: <span style=\"color:#ff79c6\">$leechers</span> - ";
+                echo "$size</span>";
+                echo "</div>";
+            }
+
+        echo "</div>";
+    }
+
+?>

+ 40 - 0
engines/bittorrent/nyaa.php

@@ -0,0 +1,40 @@
+<?php
+    function get_nyaa_results($query)
+    {
+        require_once "config.php";
+        require_once "misc/tools.php";
+
+        $url = "https://nyaa.si/?q=$query";
+        $response = request($url);
+        $xpath = get_xpath($response);
+
+        $results = array();
+
+        foreach($xpath->query("//tbody/tr") as $result)
+        {
+            global $config_bittorent_trackers;
+
+            $name = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result)[0]->textContent;
+            $centered = $xpath->evaluate(".//td[@class='text-center']", $result);
+            $magnet = $xpath->evaluate(".//a[2]/@href", $centered[0])[0]->textContent;
+            $magnet_without_tracker = explode("&tr=", $magnet)[0];
+            $magnet = $magnet_without_tracker . $config_bittorent_trackers;
+            $size =  $centered[1]->textContent;
+            $seeders =  $centered[3]->textContent;
+            $leechers =  $centered[4]->textContent;
+
+            array_push($results, 
+                array (
+                    "name" => $name,
+                    "seeders" => (int) $seeders,
+                    "leechers" => (int) $leechers,
+                    "magnet" => $magnet,
+                    "size" => $size,
+                    "source" => "nyaa.si"
+                )
+            );
+        }
+
+        return $results;
+    }
+?>

+ 12 - 32
engines/bittorrent/thepiratebay.php

@@ -2,8 +2,8 @@
 
     function get_thepiratebay_results($query)
     {
-        require "config.php";
-        require "misc/tools.php";
+        require_once "config.php";
+        require_once "misc/tools.php";
 
         $query = urlencode($query);
 
@@ -16,20 +16,24 @@
         foreach ($json_response as $response)
         {
 
-            $hash = $response["info_hash"];
+            global $config_bittorent_trackers;
+
+            $size = human_filesize($response["size"]);
+            $hash = $response["info_hash"]; 
             $name = $response["name"];
-            $seeders = $response["seeders"];
-            $leechers = $response["leechers"];
+            $seeders = (int) $response["seeders"];
+            $leechers = (int) $response["leechers"];
 
-            $magnet = "magnet:?xt=urn:btih:$hash&dn=$name";
+            $magnet = "magnet:?xt=urn:btih:$hash&dn=$name$config_bittorent_trackers";
 
             array_push($results, 
                 array (
-                    "hash" => $hash,
+                    "size" => $size,
                     "name" => $name,
                     "seeders" => $seeders,
                     "leechers" => $leechers,
-                    "magnet" => $magnet
+                    "magnet" => $magnet,
+                    "source" => "thepiratebay.org"
                 )
             );
         }
@@ -37,28 +41,4 @@
         return $results;
        
     }
-
-    function print_thepiratebay_results($results)
-    {
-        echo "<div class=\"text-result-container\">";
-
-            foreach($results as $result)
-            {
-                $hash = $result["hash"];
-                $name = $result["name"];
-                $seeders = $result["seeders"];
-                $leechers = $result["leechers"];
-                $magnet = $result["magnet"];
-
-                echo "<div class=\"text-result-wrapper\">";
-                echo "<a href=\"$magnet\">";
-                echo "$hash";
-                echo "<h2>$name</h2>";
-                echo "</a>";
-                echo "<span>SE: $seeders - LE: $leechers</span>";
-                echo "</div>";
-            }
-
-        echo "</div>";
-    }
 ?>

+ 52 - 0
engines/bittorrent/yts.php

@@ -0,0 +1,52 @@
+<?php
+
+    function get_yts_results($query)
+    {
+        require_once "config.php";
+        require_once "misc/tools.php";
+
+        $query = urlencode($query);
+
+        $results = array();
+
+        $url = "https://yts.mx/api/v2/list_movies.json?query_term=$query";
+        $response = request($url);
+        $json_response = json_decode($response, true);
+
+        if ($json_response["data"]["movie_count"] != 0)
+        {
+            foreach ($json_response["data"]["movies"] as $movie)
+            {
+                    $name = $movie["title"];
+                    $name_encoded = urlencode($name);
+
+                    foreach ($movie["torrents"] as $torrent)
+                    {
+                        global $config_bittorent_trackers;
+
+                        $hash = $torrent["hash"];
+                        $seeders = $torrent["seeds"];
+                        $leechers = $torrent["peers"];
+                        $size = $torrent["size"];
+
+                        $magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$config_bittorent_trackers";
+
+                        array_push($results, 
+                        array (
+                            "size" => $size,
+                            "name" => $name,
+                            "seeders" => $seeders,
+                            "leechers" => $leechers,
+                            "magnet" => $magnet,
+                            "source" => "yts.mx"
+                        )
+                    );
+                    
+                    }
+            }
+        }
+
+        return $results;
+       
+    }
+?>

+ 8 - 0
misc/tools.php

@@ -26,6 +26,14 @@
         return $response;
     }
 
+    function human_filesize($bytes, $dec = 2) 
+    {
+        $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
+        $factor = floor((strlen($bytes) - 1) / 3);
+
+        return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
+    }
+
     function print_next_page_button($text, $page, $query, $type) 
     {
         echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\" enctype=\"multipart/form-data\" autocomplete=\"off\">";

+ 4 - 3
search.php

@@ -92,9 +92,10 @@
                         echo "<p class=\"text-result-container\">The host disabled this feature! :C</p>";
                     else
                     {
-                        require "engines/bittorrent/thepiratebay.php";
-                        $results = get_thepiratebay_results($query_encoded);
-                        print_thepiratebay_results($results);
+                        require "engines/bittorrent/merge.php";
+                        $results = get_merged_torrent_results($query_encoded);
+                        print_merged_torrent_results($results);
+                        break;
                     }
                     
                     break;

+ 1 - 0
static/styles.css

@@ -120,6 +120,7 @@ a:hover, .text-result-wrapper h2:hover {
 
 .text-result-wrapper {
     max-width: 500px;
+    word-wrap: break-word;
     margin-top: 35px;
 }