|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Martial\Transmission\API\Argument\Torrent; |
|
6
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
7
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
class TorrentList extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
protected function configure() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::configure(); |
|
17
|
|
|
$this |
|
18
|
|
|
->setName('torrent-list') |
|
19
|
|
|
->setAliases(['tl']) |
|
20
|
|
|
->setDescription('List torrents') |
|
21
|
|
|
->addOption('sort', null, InputOption::VALUE_OPTIONAL, 'Sort by column number', 4) |
|
22
|
|
|
->addOption('age', null, InputOption::VALUE_OPTIONAL, 'Sort by torrent age, ex. \'>1 <5\'') |
|
23
|
|
|
->addOption('name', null, InputOption::VALUE_OPTIONAL, 'Sort by torrent name (regexp)') |
|
24
|
|
|
->setHelp(<<<EOT |
|
25
|
|
|
The <info>torrent-list</info> list torrents. |
|
26
|
|
|
EOT |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
31
|
|
|
{ |
|
32
|
|
|
$headers = ['Name', 'Id', 'Age', 'Size', 'Uploaded', 'Per day']; |
|
33
|
|
|
|
|
34
|
|
|
$client = $this->getApplication()->getClient(); |
|
35
|
|
|
|
|
36
|
|
|
$torrentList = $client->getTorrentData(); |
|
37
|
|
|
|
|
38
|
|
|
$torrentList = $client->filterTorrents($torrentList, [ |
|
39
|
|
|
'age' => $input->getOption('age'), |
|
40
|
|
|
'name' => $input->getOption('name'), |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$rows = []; |
|
44
|
|
|
foreach ($torrentList as $torrent) { |
|
45
|
|
|
$age = $client->getTorrentAgeInDays($torrent); |
|
46
|
|
|
$perDay = $age ? round($torrent[Torrent\Get::UPLOAD_EVER] / $age / 1024 / 1000 / 1000, 2) : 0; |
|
47
|
|
|
|
|
48
|
|
|
$rows[] = [ |
|
49
|
|
|
$torrent[Torrent\Get::NAME], |
|
50
|
|
|
$torrent[Torrent\Get::ID], |
|
51
|
|
|
$age, |
|
52
|
|
|
round($torrent[Torrent\Get::DOWNLOAD_EVER] / 1024 / 1000 / 1000, 2), |
|
53
|
|
|
round($torrent[Torrent\Get::UPLOAD_EVER] / 1024 / 1000 / 1000, 2), |
|
54
|
|
|
$perDay, |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$totals = [ |
|
59
|
|
|
'Total', |
|
60
|
|
|
'', |
|
61
|
|
|
'', |
|
62
|
|
|
round($client->getTorrentsSize($torrentList) / 1024 / 1000 / 1000, 2), |
|
63
|
|
|
round($client->getTorrentsSize($torrentList, Torrent\Get::UPLOAD_EVER) / 1024 / 1000 / 1000, 2), |
|
64
|
|
|
'' |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
$rows = $this->sortTable($rows, $input); |
|
68
|
|
|
|
|
69
|
|
|
$table = new Table($output); |
|
70
|
|
|
$table->setHeaders($headers); |
|
71
|
|
|
$table->setRows($rows); |
|
72
|
|
|
$table->addRow(new TableSeparator()); |
|
73
|
|
|
$table->addRow($totals); |
|
74
|
|
|
$table->render(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function sortTable(array $rows, InputInterface $input) |
|
78
|
|
|
{ |
|
79
|
|
|
$rowsSorted = $rows; |
|
80
|
|
|
$columnsTotal = count(end($rows)); |
|
81
|
|
|
|
|
82
|
|
|
$sortColumn = max(1, min( |
|
83
|
|
|
$columnsTotal, |
|
84
|
|
|
$input->getOption('sort') |
|
85
|
|
|
)) - 1; |
|
86
|
|
|
|
|
87
|
|
|
usort($rowsSorted, function ($first, $second) use ($sortColumn) { |
|
88
|
|
|
return $first[$sortColumn] > $second[$sortColumn] ? 1 : -1; |
|
89
|
|
|
}); |
|
90
|
|
|
|
|
91
|
|
|
return $rowsSorted; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|