1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console; |
4
|
|
|
|
5
|
|
|
use Martial\Transmission\API; |
6
|
|
|
use Martial\Transmission\API\Argument\Torrent; |
7
|
|
|
use Popstas\Transmission\Console\Helpers\TorrentUtils; |
8
|
|
|
|
9
|
|
|
class TransmissionClient |
10
|
|
|
{ |
11
|
|
|
private $api; |
12
|
|
|
private $sessionId; |
13
|
|
|
|
14
|
|
|
public function __construct(API\TransmissionAPI $api) |
15
|
|
|
{ |
16
|
|
|
$this->api = $api; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function createSession() |
20
|
|
|
{ |
21
|
|
|
$this->sessionId = $this->getSessionId($this->sessionId); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function getSessionId($sessionId) |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
$this->api->sessionGet($sessionId); |
28
|
|
|
} catch (API\CSRFException $e) { |
29
|
|
|
// The session has been reinitialized. Fetch the new session ID with the method getSessionId(). |
30
|
|
|
$sessionId = $e->getSessionId(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $sessionId; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getTorrentData(array $ids = [], $fields = []) |
37
|
|
|
{ |
38
|
|
|
if (empty($fields)) { |
39
|
|
|
$fields = [ |
40
|
|
|
Torrent\Get::ID, |
41
|
|
|
Torrent\Get::NAME, |
42
|
|
|
Torrent\Get::TOTAL_SIZE, |
43
|
|
|
Torrent\Get::DOWNLOAD_DIR, |
44
|
|
|
Torrent\Get::UPLOAD_EVER, |
45
|
|
|
Torrent\Get::DOWNLOAD_EVER, |
46
|
|
|
Torrent\Get::DONE_DATE, |
47
|
|
|
Torrent\Get::ADDED_DATE, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$cleanedIds = array_map(function ($torrentId) { |
52
|
|
|
return (int)$torrentId; |
53
|
|
|
}, $ids); |
54
|
|
|
|
55
|
|
|
$this->createSession(); |
56
|
|
|
$torrentList = $this->api->torrentGet($this->sessionId, $cleanedIds, $fields); |
57
|
|
|
|
58
|
|
|
$torrentList = array_map(function ($torrent) { |
59
|
|
|
$torrent['age'] = TorrentUtils::getTorrentAgeInDays($torrent); |
60
|
|
|
return $torrent; |
61
|
|
|
}, $torrentList); |
62
|
|
|
|
63
|
|
|
return $torrentList; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $torrentList Array of Torrent data or torrent_ids |
68
|
|
|
* @param bool $deleteLocalData |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function removeTorrents(array $torrentList, $deleteLocalData = false) |
72
|
|
|
{ |
73
|
|
|
if (empty($torrentList)) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$torrentIds = []; |
78
|
|
|
|
79
|
|
|
foreach ($torrentList as $torrent) { |
80
|
|
|
$torrentIds[] = $torrent[Torrent\Get::ID]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->createSession(); |
84
|
|
|
$this->api->torrentRemove($this->sessionId, $torrentIds, $deleteLocalData); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|