Browse Source

improved performance for torrent results via curl_multi

hnhx 3 years ago
parent
commit
b07e6fa0a5

+ 48 - 5
engines/bittorrent/merge.php

@@ -2,19 +2,62 @@
 
     function get_merged_torrent_results($query)
     {
+        global $config;
+
         require "engines/bittorrent/thepiratebay.php";
         require "engines/bittorrent/rutor.php";
         require "engines/bittorrent/nyaa.php";
         require "engines/bittorrent/yts.php";
 
-        $results = array_merge(get_thepiratebay_results($query),
-                               get_rutor_results($query),
-                               get_nyaa_results($query),
-                               get_yts_results($query));
+        $query = urlencode($query);
+
+        $torrent_urls = array(
+            $thepiratebay_url,
+            $rutor_url,
+            $nyaa_url,
+            $yts_url
+        );
+ 
+        $mh = curl_multi_init();
+        $chs = $results = array();
+
+        foreach ($torrent_urls as $url)
+        {
+            $ch = curl_init($url);
+            curl_setopt_array($ch, $config->curl_settings);
+            array_push($chs, $ch);
+            curl_multi_add_handle($mh, $ch);    
+        }
+
+        $running = null;
+        do {
+            curl_multi_exec($mh, $running);
+        } while ($running);
+
+        for ($i=0; count($chs)>$i; $i++)
+        {
+            $response = curl_multi_getcontent($chs[$i]);
 
+            switch ($i)
+            {
+                case 0:
+                    $results = array_merge($results, get_thepiratebay_results($response));
+                    break;
+                case 1:
+                    $results = array_merge($results, get_rutor_results($response));
+                    break;
+                case 2:
+                    $results = array_merge($results, get_nyaa_results($response));
+                    break;
+                case 3:
+                    $results = array_merge($results, get_yts_results($response));
+                    break;
+            }
+        }
+        
         $seeders = array_column($results, "seeders");
         array_multisort($seeders, SORT_DESC, $results);
-        
+
         return $results; 
     }
 

+ 3 - 5
engines/bittorrent/nyaa.php

@@ -1,12 +1,10 @@
 <?php
-    function get_nyaa_results($query)
+    $nyaa_url = "https://nyaa.si/?q=$query";
+
+    function get_nyaa_results($response)
     {
         global $config;
-
-        $url = "https://nyaa.si/?q=$query";
-        $response = request($url);
         $xpath = get_xpath($response);
-
         $results = array();
 
         foreach($xpath->query("//tbody/tr") as $result)

+ 3 - 5
engines/bittorrent/rutor.php

@@ -1,12 +1,10 @@
 <?php
-    function get_rutor_results($query)
+    $rutor_url = "http://rutor.info/search/$query";
+
+    function get_rutor_results($response)
     {
         global $config;
-
-        $url = "http://rutor.info/search/$query";
-        $response = request($url);
         $xpath = get_xpath($response);
-
         $results = array();
 
 

+ 3 - 7
engines/bittorrent/thepiratebay.php

@@ -1,15 +1,11 @@
 <?php
 
-    function get_thepiratebay_results($query)
+    $thepiratebay_url = "https://apibay.org/q.php?q=$query";
+
+    function get_thepiratebay_results($response)
     {
         global $config;
-
-        $query = urlencode($query);
-
         $results = array();
-
-        $url = "https://apibay.org/q.php?q=$query";
-        $response = request($url);
         $json_response = json_decode($response, true);
 
         foreach ($json_response as $response)

+ 2 - 8
engines/bittorrent/yts.php

@@ -1,18 +1,12 @@
 <?php
+    $yts_url = "https://yts.mx/api/v2/list_movies.json?query_term=$query";
 
-    function get_yts_results($query)
+    function get_yts_results($response)
     {
         global $config;
-
-        $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["status"] == "ok" && $json_response["data"]["movie_count"] != 0)
         {
             foreach ($json_response["data"]["movies"] as $movie)