From b07e6fa0a59f3084f6c0d08e4a3ea0fbce6a525d Mon Sep 17 00:00:00 2001 From: hnhx Date: Wed, 27 Apr 2022 13:15:17 +0200 Subject: [PATCH] improved performance for torrent results via curl_multi --- engines/bittorrent/merge.php | 53 ++++++++++++++++++++++++++--- engines/bittorrent/nyaa.php | 8 ++--- engines/bittorrent/rutor.php | 8 ++--- engines/bittorrent/thepiratebay.php | 10 ++---- engines/bittorrent/yts.php | 10 ++---- 5 files changed, 59 insertions(+), 30 deletions(-) diff --git a/engines/bittorrent/merge.php b/engines/bittorrent/merge.php index d1ee79d..6ff967c 100644 --- a/engines/bittorrent/merge.php +++ b/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; } diff --git a/engines/bittorrent/nyaa.php b/engines/bittorrent/nyaa.php index 4a30968..6d4d6e3 100644 --- a/engines/bittorrent/nyaa.php +++ b/engines/bittorrent/nyaa.php @@ -1,12 +1,10 @@ query("//tbody/tr") as $result) diff --git a/engines/bittorrent/rutor.php b/engines/bittorrent/rutor.php index 060eb5b..4ef3ec3 100644 --- a/engines/bittorrent/rutor.php +++ b/engines/bittorrent/rutor.php @@ -1,12 +1,10 @@