From f152dba43779897e29aae6629915c36cb954f37a Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 3 Apr 2022 14:41:28 +0300 Subject: [PATCH] implement scrape request --- announce.php | 13 ------------- network.php | 12 ++++++++++++ scrape.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 scrape.php diff --git a/announce.php b/announce.php index 3a06c2f..8ed89a4 100644 --- a/announce.php +++ b/announce.php @@ -6,19 +6,6 @@ $DB_NAME = "torrent"; $ANN_MIN_INTERVAL = 15; $ANN_INTERVAL = 60; -function bad_request($msg) -{ - http_response_code(400); - echo($msg); - exit; -} - -function do_error($msg) -{ - die($msg); -} - - if (!isset($_GET["info_hash"])) { bad_request("No info_hash"); } diff --git a/network.php b/network.php index 180811f..6174a0e 100644 --- a/network.php +++ b/network.php @@ -1,5 +1,17 @@ 40) { + bad_request("Invalid info_hash"); + } +} catch (Exception $e) { + bad_request("Invalid info_hash"); +} + +$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", $DB_USER, $DB_PASS); +$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +$stmt = $conn->prepare("SELECT count(*) FROM `peers` WHERE info_hash = :info_hash"); +$stmt->bindValue(":info_hash", $hash, PDO::PARAM_STR); +$stmt->execute(); + +$name = "torrent-" . $hash; +$count = intval($stmt->fetchColumn()); + +$response = array( + "files" => array( + hash_encode($hash) => array( + "complete" => $count, + "downloaded" => $count, + "incomplete" => 0, + "name" => $name, + ), + ), +); +echo(bencode($response)); + +?> \ No newline at end of file