forked from marsalans/voltage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoltage.php
More file actions
executable file
·92 lines (68 loc) · 2.16 KB
/
voltage.php
File metadata and controls
executable file
·92 lines (68 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
require_once __DIR__.'/classes/Util.php';
require_once __DIR__.'/classes/Torrent.php';
require_once __DIR__.'/classes/Download.php';
if (count($argv) < 3) {
die("USAGE: voltage <dir> <torrent>\n");
}
$dir = $argv[1];
$torrentFile = $argv[2];
if (preg_match('#^[a-f0-9]{40}$#i', $torrentFile)) {
$hash = strtoupper($torrentFile);
$torrentData = file_get_contents("http://itorrents.org/torrent/$hash.torrent");
if (strlen($torrentData) <= 0) {
die("ERROR: Failed to download torrent from itorrents.org\n");
}
if ($torrentData[0] !== 'd') {
$torrentData = gzdecode($torrentData);
}
} else {
if (!file_exists($torrentFile)) {
die("ERROR: Unable to open torrent file '$torrentFile'\n");
}
$torrentData = file_get_contents($torrentFile);
if (strlen($torrentData) <= 0) {
die("ERROR: Failed to read torrent '$torrentFile'\n");
}
}
try {
$torrent = Torrent::read($torrentData);
} catch (Exception $e) {
trigger_error($e);
die("ERROR: Failed to parse torrent\n");
}
echo " Info Hash: {$torrent->getInfoHashHex()}\n";
echo " Piece Size: ".Util::formatSize($torrent->getPieceSize())."\n";
echo " Total Size: ".Util::formatSize($torrent->getTotalSize())."\n";
$download = new Download($torrent, $dir);
$download->on('allocate', function ($finished, $total) {
$p = (int)($finished / $total * 100.0);
echo "Allocating Files ... ($p%) \r";
});
$download->on('check', function ($finished, $total) {
$p = (int)($finished / $total * 100.0);
echo "Checking Files ... ($p%) \r";
});
$download->start();
echo "Starting download ... \r";
$lastTotal = 0;
$lastTime = time();
$downloadSpeed = '0K/s';
while (!$download->isComplete()) {
$download->tick();
$p = $download->getProgress();
$x = $download->getConnectedPeerCount();
$y = $download->getPeerCount();
$now = time();
$span = $now - $lastTime;
if ($span > 0) {
$lastTime = $now;
$total = $download->getDownloaded();
$delta = $total - $lastTotal;
$lastTotal = $total;
$downloadSpeed = Util::formatSize($delta).'/s';
}
echo "Downloading ... ($p%) ($x/$y peers) ($downloadSpeed) \r";
//sleep(1);
}
$download->finish();