Download API exemple

This commit is contained in:
alphayax 2016-03-05 20:05:35 +01:00
parent c60c93461b
commit 36d7069512
5 changed files with 139 additions and 11 deletions

View File

@ -4,14 +4,10 @@
require_once 'vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Exemple', '0.0.1');
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Exemple', '0.0.2');
$App->authorize();
$App->openSession();
$Downloads = new \alphayax\freebox\api\v3\services\download\Download( $App);
$a = $Downloads->getAll();
var_dump( $a);
$b = $Downloads->getLogFromId( 156);
var_dump( $b);
$toto = new \alphayax\freebox\api\v3\services\config\DHCP( $App);
var_dump( $toto->get_current_configuration());

View File

@ -32,4 +32,11 @@ class DhcpConfig extends Model {
/** @var array of string List of dns servers to include in DHCP reply */
protected $dns = [];
/**
* @return array
*/
public function getDNS(){
return $this->dns;
}
}

View File

@ -85,5 +85,83 @@ class Download extends Service {
return $rest->getCurlResponse()['result'];
}
/**
* @param string $download_url
* @param string $download_dir
* @param bool|false $recursive
* @param null $username
* @param null $password
* @param null $archive_password
* @param null $cookies
* @return int Download Id
*/
public function addFromUrl( $download_url, $download_dir = null, $recursive = false, $username = null, $password = null, $archive_password = null, $cookies = null){
$this->addFromUrls( [$download_url], $download_dir, $recursive, $username, $password, $archive_password, $cookies);
}
/**
* @param array $download_urls A list of URL
* @param string $download_dir The download destination directory (optional: will use the configuration download_dir by default)
* @param bool $recursive If true the download will be recursive
* @param string $username Auth username (optional)
* @param string $password Auth password (optional)
* @param string $archive_password The password required to extract downloaded content (only relevant for nzb)
* @param string $cookies The http cookies (to be able to pass session cookies along with url)
* @return int Download Id
* @throws \Exception
*/
public function addFromUrls( $download_urls = [], $download_dir = null, $recursive = false, $username = null, $password = null, $archive_password = null, $cookies = null){
$params = [];
if( empty( $download_urls)){
throw new \Exception( 'At lease one url is needed');
}
if( count( $download_urls) == 1){
$params['download_url'] = $download_urls[0];
} else {
$params['download_url_list'] = implode( PHP_EOL, $download_urls);
}
if( ! empty( $download_dir)){
$params['download_dir'] = base64_encode( $download_dir);
}
if( ! empty( $recursive)){
$params['download_dir'] = $recursive;
}
if( ! empty( $username)){
$params['username'] = $username;
$params['password'] = $password;
}
if( ! empty( $archive_password)){
$params['archive_password'] = $password;
}
if( ! empty( $cookies)){
$params['cookies'] = $password;
}
$data = http_build_query( $params);
$rest = $this->getAuthService( self::API_DOWNLOAD_ADD);
$rest->setContentType_XFormURLEncoded();
$rest->POST( $data);
$response = $rest->getCurlResponse();
return $response['result']['id'];
}
/**
* @param $download_file_rdi
* @param string $download_dir_rdi
* @param string $archive_password
*/
public function addFromFile( $download_file_rdi, $download_dir_rdi = '', $archive_password = ''){
$rest = $this->getAuthService( self::API_DOWNLOAD_ADD);
// TODO
}
}

View File

@ -53,18 +53,17 @@ class RestAuth extends alphayax\utils\Rest {
* Add the session token in the X-Fbx-App-Auth Header
*/
protected function add_XFbxAppAuth_Header(){
curl_setopt( $this->_curl_handler, CURLOPT_HTTPHEADER, array(
'X-Fbx-App-Auth: '. $this->session_token,
));
$this->http_headers[ 'X-Fbx-App-Auth'] = $this->session_token;
}
/**
* @throws \Exception
*/
protected function checkResponse(){
$response = $this->getCurlResponse();
if( false === $response['success']){
throw new \Exception( $response['msg']);
throw new \Exception( $response['msg'] . ' ('. $response['error_code'] . ')');
}
}

48
one_piece.php Normal file
View File

@ -0,0 +1,48 @@
<?php
/// Require Composer AutoLoader
use alphayax\utils\cli\IO;
require_once 'vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Exemple', '0.0.2');
$App->authorize();
$App->openSession();
//--
if( file_exists( 'one_piece')){
$toto = json_decode( file_get_contents( 'one_piece'), true);
} else {
$toto = [
'rss' => 'https://www.nyaa.se/?page=rss&user=175467',
'pattern' => '/One_Piece.*HD.*mp4/',
'last_date' => 1456388223,
];
}
$rss = simplexml_load_file( $toto['rss']);
foreach( $rss->xpath('//item') as $item){
$title = (string) $item->xpath('title')[0];
$date = (string) $item->xpath('pubDate')[0];
$link = (string) $item->xpath('link')[0];
$desc = (string) $item->xpath('description')[0];
if( preg_match( $toto['pattern'], $title)){
if( strtotime( $date) > $toto['last_date']){
// echo $title . ' '. $link . ' ' . $desc . PHP_EOL;
$toto['last_date'] = strtotime( $date);
$Downloads = new \alphayax\freebox\api\v3\services\download\Download( $App);
$dl_id = $Downloads->addFromUrl( $link);
IO::stdout( "Download $title added (id=$dl_id)", 0, true, IO::COLOR_GREEN);
}
}
}
file_put_contents( 'one_piece', json_encode( $toto, JSON_PRETTY_PRINT));