Add Download BT Tracker API

This commit is contained in:
alphayax 2016-05-26 22:38:44 +02:00
parent 08bc5e89c9
commit e036eeb39b
6 changed files with 239 additions and 1 deletions

View File

@ -37,6 +37,7 @@ Jusqu'a présent, les fonctionalités suivantes ont été implémentées :
- Download Stats
- Download Files
- Download Configuration
- Bittorent Trackers
- Configuration
- Connection
- Connection (Core)

View File

@ -2,7 +2,7 @@
- ~~Download~~
- ~~Download Stats~~
- ~~Download Files~~
- Download Trackers [UNSTABLE]
- ~~Download Trackers [UNSTABLE]~~
- Download Peers [UNSTABLE]
- Download Blacklist [UNSTABLE]
- Download Feeds

View File

@ -0,0 +1,21 @@
<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.download', 'PHP API Example (Download)', '1.0.0');
$App->authorize();
$App->openSession();
/// List of all downloads
$DownloadService = new \alphayax\freebox\api\v3\services\download\Download( $App);
$Downloads = $DownloadService->getAll();
print_r( $Downloads);
$TrackerService = new \alphayax\freebox\api\v3\services\download\Tracker( $App);
$a = $TrackerService->getAll( $Downloads[1]->getId());
print_r( $a);

View File

@ -0,0 +1,111 @@
<?php
namespace alphayax\freebox\api\v3\models\Download;
use alphayax\freebox\api\v3\Model;
/**
* Class Tracker
* @package alphayax\freebox\api\v3\models\Download
*/
class Tracker extends Model {
/** @var string (Read-only) : tracker announce URL */
protected $announce;
/** @var bool (Read-only) : true if the tracker is a backup tracker (the downloader wont connect to this tracker unless the primary tracker fails) */
protected $is_backup;
/**
* @var string (Read-only) : tracker status
* @see alphayax\freebox\api\v3\symbols\Download\Tracker\Status
*/
protected $status;
/** @var int (Read-only) : desired interval between two announces (in seconds) */
protected $interval;
/** @var int (Read-only) : minimum interval between two announces (in seconds) */
protected $min_interval;
/** @var int (Read-only) : time left before reannounce (in seconds) */
protected $reannounce_in;
/** @var int (Read-only) : number of seeders announced on tracker */
protected $nseeders;
/** @var int (Read-only) : number of leechers announced on tracker */
protected $nleechers;
/** @var bool : is the tracker enabled */
protected $is_enabled;
/**
* @return string
*/
public function getAnnounce() {
return $this->announce;
}
/**
* @return boolean
*/
public function isIsBackup() {
return $this->is_backup;
}
/**
* @return string
*/
public function getStatus() {
return $this->status;
}
/**
* @return int
*/
public function getInterval() {
return $this->interval;
}
/**
* @return int
*/
public function getMinInterval() {
return $this->min_interval;
}
/**
* @return int
*/
public function getReannounceIn() {
return $this->reannounce_in;
}
/**
* @return int
*/
public function getNseeders() {
return $this->nseeders;
}
/**
* @return int
*/
public function getNleechers() {
return $this->nleechers;
}
/**
* @return boolean
*/
public function isIsEnabled() {
return $this->is_enabled;
}
/**
* @param boolean $is_enabled
*/
public function setIsEnabled( $is_enabled) {
$this->is_enabled = $is_enabled;
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace alphayax\freebox\api\v3\services\download;
use alphayax\freebox\api\v3\Service;
use alphayax\freebox\api\v3\models;
/**
* Class Tracker
* @package alphayax\freebox\api\v3\services\download
* @experimental
*/
class Tracker extends Service {
const API_DOWNLOAD_TRACKER = '/api/v3/downloads/%u/trackers';
const API_DOWNLOAD_TRACKER_ITEM = '/api/v3/downloads/%u/trackers/%s';
/**
* Each torrent Download task has one or more DownloadTracker.
* Each tracker is identified by its announce URL.
* @param int $downloadTaskId
* @return models\Download\Tracker
*/
public function getAll( $downloadTaskId){
$service = sprintf( self::API_DOWNLOAD_TRACKER, $downloadTaskId);
$rest = $this->getAuthService( $service);
$rest->GET();
$DownloadTracker_xs = @$rest->getCurlResponse()['result'] ?: [];
$DownloadTrackers = [];
foreach( $DownloadTracker_xs as $DownloadTracker_x) {
$DownloadTrackers[] = new models\Download\Tracker( $DownloadTracker_x);
}
return $DownloadTrackers;
}
/**
* Add a new tracker
* Attempting to call this method on a download other than bittorent will fail
* @param int $downloadTaskId
* @param string $announceUrl (eg: udp://tracker.openbittorrent.com:80)
* @return bool
*/
public function add( $downloadTaskId, $announceUrl) {
$service = sprintf( self::API_DOWNLOAD_TRACKER, $downloadTaskId);
$rest = $this->getAuthService( $service);
$rest->POST([
'announce' => $announceUrl,
]);
return (bool) $rest->getCurlResponse()['success'];
}
/**
* Remove a tracker
* Attempting to call this method on a download other than bittorent will fail
* @param int $downloadTaskId
* @param string $announceUrl (eg: udp://tracker.openbittorrent.com:80)
* @return bool
*/
public function remove( $downloadTaskId, $announceUrl) {
$service = sprintf( self::API_DOWNLOAD_TRACKER_ITEM, $downloadTaskId, $announceUrl);
$rest = $this->getAuthService( $service);
$rest->DELETE();
return (bool) $rest->getCurlResponse()['success'];
}
/**
* Update a tracker
* Attempting to call this method on a download other than bittorent will fail
* @param int $downloadTaskId
* @param string $announceUrl
* @param models\Download\Tracker $Tracker
* @return bool
*/
public function update( $downloadTaskId, $announceUrl, models\Download\Tracker $Tracker) {
$service = sprintf( self::API_DOWNLOAD_TRACKER_ITEM, $downloadTaskId, $announceUrl);
$rest = $this->getAuthService( $service);
$rest->PUT( $Tracker->toArray());
return (bool) $rest->getCurlResponse()['success'];
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace alphayax\freebox\api\v3\symbols\Download\Tracker;
/**
* Symbol Status
* @package alphayax\freebox\api\v3\symbols\Download\Tracker
* @see alphayax\freebox\api\v3\models\Download\Tracker
*/
interface Status {
/** not announced */
const UNANNOUNCED = 'unannounced';
/** announcing */
const ANNOUNCING = 'announcing';
/** an error occurred while trying to announce */
const ANNOUNCE_FAILED = 'announce_failed';
/** announced */
const ANNOUNCED = 'announced';
}