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