1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console\Helpers; |
4
|
|
|
|
5
|
|
|
use Martial\Transmission\API\Argument\Torrent; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
|
8
|
|
|
class TorrentListUtils |
9
|
|
|
{ |
10
|
|
|
public static function sumArrayField(array $rows, $fieldKey) |
11
|
|
|
{ |
12
|
|
|
$sum = 0; |
13
|
|
|
foreach ($rows as $row) { |
14
|
|
|
$sum += $row[$fieldKey]; |
15
|
|
|
} |
16
|
|
|
return $sum; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function getArrayField(array $rows, $fieldKey) |
20
|
|
|
{ |
21
|
|
|
$fields = []; |
22
|
|
|
foreach ($rows as $torrent) { |
23
|
|
|
$fields[] = $torrent[$fieldKey]; |
24
|
|
|
} |
25
|
|
|
return $fields; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $torrentList |
30
|
|
|
* |
31
|
|
|
* @param array $filters |
32
|
|
|
* filters[ |
33
|
|
|
* - age |
34
|
|
|
* - age_min |
35
|
|
|
* - age_max |
36
|
|
|
* ] |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function filterTorrents(array $torrentList, array $filters) |
41
|
|
|
{ |
42
|
|
|
if (isset($filters['name'])) { |
43
|
|
|
$filters[Torrent\Get::NAME] = ['type' => 'regex', 'value' => $filters['name']]; |
44
|
|
|
} |
45
|
|
|
if (isset($filters['age'])) { |
46
|
|
|
$filters['age'] = ['type' => 'numeric', 'value' => $filters['age']]; |
47
|
|
|
// TODO: move to getTorrentData() |
48
|
|
|
$torrentList = array_map(function ($torrent) { |
49
|
|
|
$torrent['age'] = TorrentUtils::getTorrentAgeInDays($torrent); |
50
|
|
|
return $torrent; |
51
|
|
|
}, $torrentList); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$filters = TableUtils::parseFilters($filters); |
55
|
|
|
$torrentList = TableUtils::filterRows($torrentList, $filters); |
56
|
|
|
return $torrentList; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function buildTableData(array $torrentList) |
60
|
|
|
{ |
61
|
|
|
$headers = ['Name', 'Id', 'Age', 'Size', 'Uploaded', 'Per day']; |
62
|
|
|
$rows = []; |
63
|
|
|
|
64
|
|
|
foreach ($torrentList as $torrent) { |
65
|
|
|
$age = TorrentUtils::getTorrentAgeInDays($torrent); |
66
|
|
|
$perDay = $age ? TorrentUtils::getSizeInGb($torrent[Torrent\Get::UPLOAD_EVER] / $age) : 0; |
67
|
|
|
|
68
|
|
|
$rows[] = [ |
69
|
|
|
$torrent[Torrent\Get::NAME], |
70
|
|
|
$torrent[Torrent\Get::ID], |
71
|
|
|
$age, |
72
|
|
|
TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]), |
73
|
|
|
TorrentUtils::getSizeInGb($torrent[Torrent\Get::UPLOAD_EVER]), |
74
|
|
|
$perDay, |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$totals = [ |
79
|
|
|
'Total', |
80
|
|
|
'', |
81
|
|
|
'', |
82
|
|
|
TorrentUtils::getSizeInGb(self::sumArrayField($torrentList, Torrent\Get::TOTAL_SIZE)), |
83
|
|
|
TorrentUtils::getSizeInGb(self::sumArrayField($torrentList, Torrent\Get::UPLOAD_EVER)), |
84
|
|
|
'' |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
'headers' => $headers, |
89
|
|
|
'rows' => $rows, |
90
|
|
|
'totals' => $totals |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function printTorrentsTable( |
95
|
|
|
array $torrentList, |
96
|
|
|
OutputInterface $output, |
97
|
|
|
$sortColumnNumber = 1, |
98
|
|
|
$limit = 0 |
99
|
|
|
) { |
100
|
|
|
$data = self::buildTableData($torrentList); |
101
|
|
|
TableUtils::printTable($data, $output, $sortColumnNumber, $limit); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function getObsoleteTorrents(array $torrentList) |
105
|
|
|
{ |
106
|
|
|
$all = []; |
107
|
|
|
$obsolete = []; |
108
|
|
|
|
109
|
|
|
foreach ($torrentList as $torrent) { |
110
|
|
|
$name = $torrent[Torrent\Get::NAME]; |
111
|
|
|
if (!isset($all[$name])) { |
112
|
|
|
$all[$name] = $torrent; |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$obs = self::detectObsoleteTorrent($all[$name], $torrent); |
117
|
|
|
if ($obs) { |
118
|
|
|
$obsolete[] = $obs; |
119
|
|
|
if ($obs[Torrent\Get::ID] !== $torrent[Torrent\Get::ID]) { |
120
|
|
|
$all[$name] = $torrent; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $obsolete; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private static function detectObsoleteTorrent($torrentInList, $torrentNotInList) |
129
|
|
|
{ |
130
|
|
|
if ($torrentInList[Torrent\Get::DOWNLOAD_DIR] !== $torrentNotInList[Torrent\Get::DOWNLOAD_DIR]) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $torrentInList[Torrent\Get::TOTAL_SIZE] < $torrentNotInList[Torrent\Get::TOTAL_SIZE] ? |
135
|
|
|
$torrentInList : $torrentNotInList; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|