From 36d7069512d4f7d2950db2885e43965a65c759d9 Mon Sep 17 00:00:00 2001 From: alphayax Date: Sat, 5 Mar 2016 20:05:35 +0100 Subject: [PATCH] Download API exemple --- exemple.php | 10 +-- freebox/api/v3/models/DhcpConfig.php | 7 ++ freebox/api/v3/services/download/Download.php | 78 +++++++++++++++++++ freebox/utils/RestAuth.php | 7 +- one_piece.php | 48 ++++++++++++ 5 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 one_piece.php diff --git a/exemple.php b/exemple.php index fd06256..3012d6e 100644 --- a/exemple.php +++ b/exemple.php @@ -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); \ No newline at end of file +$toto = new \alphayax\freebox\api\v3\services\config\DHCP( $App); +var_dump( $toto->get_current_configuration()); diff --git a/freebox/api/v3/models/DhcpConfig.php b/freebox/api/v3/models/DhcpConfig.php index fcffbdc..1567304 100644 --- a/freebox/api/v3/models/DhcpConfig.php +++ b/freebox/api/v3/models/DhcpConfig.php @@ -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; + } + } diff --git a/freebox/api/v3/services/download/Download.php b/freebox/api/v3/services/download/Download.php index dcc4e54..c9306bb 100644 --- a/freebox/api/v3/services/download/Download.php +++ b/freebox/api/v3/services/download/Download.php @@ -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 + + } } diff --git a/freebox/utils/RestAuth.php b/freebox/utils/RestAuth.php index 0db9ce5..fd5b7a4 100644 --- a/freebox/utils/RestAuth.php +++ b/freebox/utils/RestAuth.php @@ -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'] . ')'); } } diff --git a/one_piece.php b/one_piece.php new file mode 100644 index 0000000..8b56971 --- /dev/null +++ b/one_piece.php @@ -0,0 +1,48 @@ +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)); + + + +