Small improvements to scraping (#1042)

This commit is contained in:
Cønstantine Kovalensky 2023-11-05 09:31:27 +04:00 committed by GitHub
commit d1611c7d8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 15 deletions

View file

@ -51,7 +51,7 @@ function drop_fast_announce($lp_info, $lp_cached_peers = [])
function msg_die($msg) function msg_die($msg)
{ {
$output = \Arokettu\Bencode\Bencode::encode([ $output = \Arokettu\Bencode\Bencode::encode([
'min interval' => (int)1800, 'interval' => (int)1800,
'failure reason' => (string)$msg, 'failure reason' => (string)$msg,
]); ]);
@ -62,7 +62,6 @@ function dummy_exit($interval = 1800, $cache_dict = [])
{ {
$output = [ $output = [
'interval' => (int)$interval, 'interval' => (int)$interval,
'min interval' => (int)$interval,
'peers' => (string)DUMMY_PEER, 'peers' => (string)DUMMY_PEER,
]; ];
@ -71,6 +70,9 @@ function dummy_exit($interval = 1800, $cache_dict = [])
$output['incomplete'] = $cache_dict['incomplete']; $output['incomplete'] = $cache_dict['incomplete'];
$output['downloaded'] = $cache_dict['downloaded']; $output['downloaded'] = $cache_dict['downloaded'];
$output['warning message'] = 'Next statistics update in: ' . (floor($interval / 60) % 60) . ' minutes'; $output['warning message'] = 'Next statistics update in: ' . (floor($interval / 60) % 60) . ' minutes';
}
if (isset($cache_dict['peers6'])) {
$output['peers'] = $cache_dict['peers']; $output['peers'] = $cache_dict['peers'];
} }

View file

@ -31,15 +31,6 @@ if (!isset($info_hash)) {
// Store info hash in hex format // Store info hash in hex format
$info_hash_hex = mb_check_encoding($info_hash, 'UTF8') ? $info_hash : bin2hex($info_hash); $info_hash_hex = mb_check_encoding($info_hash, 'UTF8') ? $info_hash : bin2hex($info_hash);
// Check info_hash version
if (strlen($info_hash) == 32) {
$is_bt_v2 = true;
} elseif (strlen($info_hash) == 20) {
$is_bt_v2 = false;
} else {
msg_die('Invalid info_hash: ' . $info_hash_hex);
}
// Handle multiple hashes // Handle multiple hashes
preg_match_all('/info_hash=([^&]*)/i', $_SERVER['QUERY_STRING'], $info_hash_array); preg_match_all('/info_hash=([^&]*)/i', $_SERVER['QUERY_STRING'], $info_hash_array);
@ -51,11 +42,11 @@ foreach ($info_hash_array[1] as $hash) {
$decoded_hash = urldecode($hash); $decoded_hash = urldecode($hash);
if ($scrape_cache = CACHE('tr_cache')->get(SCRAPE_LIST_PREFIX . bin2hex(substr($hash, 0, 20)))) { if ($scrape_cache = CACHE('tr_cache')->get(SCRAPE_LIST_PREFIX . bin2hex($decoded_hash)) {
$torrents['files'][$info_key = array_key_first($scrape_cache)] = $scrape_cache[$info_key]; $torrents['files'][$info_key = array_key_first($scrape_cache)] = $scrape_cache[$info_key];
} }
else{ else{
$info_hashes[] = '\''. DB()->escape(($decoded_hash)) . '\''; $info_hashes[] = DB()->escape(($decoded_hash));
} }
} }
@ -67,8 +58,8 @@ if (!empty($info_hash_count)) {
$info_hashes = array_slice($info_hashes, 0, $bb_cfg['max_scrapes']); $info_hashes = array_slice($info_hashes, 0, $bb_cfg['max_scrapes']);
} }
$info_hashes_sql = implode(', ', $info_hashes); $info_hashes_sql = implode('\', \'', $info_hashes);
$info_hash_where = $is_bt_v2 ? "tor.info_hash_v2 IN ($info_hashes_sql)" : "tor.info_hash IN ($info_hashes_sql) OR SUBSTRING(tor.info_hash_v2, 1, 20) IN ($info_hashes_sql)"; $info_hash_where = "tor.info_hash IN ('$info_hashes_sql') OR SUBSTRING(tor.info_hash_v2, 1, 20) IN ('$info_hashes_sql')";
$sql = " $sql = "
SELECT tor.info_hash, tor.info_hash_v2, tor.complete_count, snap.seeders, snap.leechers SELECT tor.info_hash, tor.info_hash_v2, tor.complete_count, snap.seeders, snap.leechers