1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console\Command; |
4
|
|
|
|
5
|
|
|
use Martial\Transmission\API\Argument\Torrent; |
6
|
|
|
use Popstas\Transmission\Console\Helpers\TorrentListUtils; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
11
|
|
|
|
12
|
|
|
class TorrentRemoveDuplicates extends Command |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
parent::configure(); |
17
|
|
|
$this |
18
|
|
|
->setName('torrent-remove-duplicates') |
19
|
|
|
->setAliases(['trd']) |
20
|
|
|
->setDescription('Remove duplicates obsolete torrents') |
21
|
|
|
->addOption('yes', 'y', InputOption::VALUE_NONE, 'Don\'t ask confirmation') |
22
|
|
|
->setHelp(<<<EOT |
23
|
|
|
## Remove series torrent duplicates |
24
|
|
|
|
25
|
|
|
While download series every time you add torrent with same name to Transmission. |
26
|
|
|
It corrupts `stats-send` command, therefore we need to remove old torrents. This command doing this. |
27
|
|
|
|
28
|
|
|
Just call: |
29
|
|
|
``` |
30
|
|
|
transmission-cli torrent-remove-duplicates |
31
|
|
|
``` |
32
|
|
|
|
33
|
|
|
Command removes all torrents with same name and same download directory from Transmission. |
34
|
|
|
**It not removes any files!** |
35
|
|
|
EOT |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$config = $this->getApplication()->getConfig(); |
42
|
|
|
$client = $this->getApplication()->getClient(); |
43
|
|
|
|
44
|
|
|
$torrentList = $client->getTorrentData(); |
45
|
|
|
$obsoleteList = TorrentListUtils::getObsoleteTorrents($torrentList); |
46
|
|
|
if (empty($obsoleteList)) { |
47
|
|
|
$output->writeln('There are no obsolete torrents in Transmission.'); |
48
|
|
|
return 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$output->writeln('Duplicate torrents for remove:'); |
52
|
|
|
TorrentListUtils::printTorrentsTable($obsoleteList, $output); |
53
|
|
|
|
54
|
|
View Code Duplication |
if (!$input->getOption('yes')) { |
|
|
|
|
55
|
|
|
$helper = $this->getHelper('question'); |
56
|
|
|
$question = new ConfirmationQuestion('Continue with this action? ', false); |
57
|
|
|
if (!$helper->ask($input, $output, $question)) { |
58
|
|
|
$output->writeln('Aborting.'); |
59
|
|
|
return 1; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->dryRun($input, $output, function () use ($client, $obsoleteList, $config, $input, $output) { |
64
|
|
|
try { |
65
|
|
|
$influxDbClient = $this->getApplication()->getInfluxDbClient( |
66
|
|
|
$config->get('influxdb-host'), |
67
|
|
|
$config->get('influxdb-port'), |
68
|
|
|
$config->get('influxdb-user'), |
69
|
|
|
$config->get('influxdb-password'), |
70
|
|
|
$config->get('influxdb-database') |
71
|
|
|
); |
72
|
|
|
$transmissionHost = $config->get('transmission-host'); |
73
|
|
|
$influxDbClient->sendTorrentPoints($obsoleteList, $transmissionHost); |
74
|
|
|
} catch (\Exception $exception) { |
75
|
|
|
$output->writeln('InfluxDB not available'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$client->removeTorrents($obsoleteList); |
79
|
|
|
$names = TorrentListUtils::getArrayField($obsoleteList, Torrent\Get::NAME); |
80
|
|
|
$output->writeln('Removed torrents:' . implode(', ', $names)); |
81
|
|
|
}, 'dry-run, don\'t really remove'); |
82
|
|
|
|
83
|
|
|
$output->writeln('Found and deleted ' . count($obsoleteList) . ' obsolete torrents from transmission.'); |
84
|
|
|
return 0; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.