From 5de23258c6295ecf136041b58e67173c9d7c7004 Mon Sep 17 00:00:00 2001 From: alphayax Date: Sun, 6 Mar 2016 20:33:56 +0100 Subject: [PATCH] Download API exemple --- .gitignore | 1 + README.md | 19 +++++-- .../dhcp_config/exemple.php | 4 +- exemple/dl_rss/config/one_piece.json | 5 ++ exemple/dl_rss/dl_rss.php | 50 +++++++++++++++++++ freebox/api/v3/services/download/Download.php | 4 +- one_piece.php | 48 ------------------ 7 files changed, 74 insertions(+), 57 deletions(-) rename exemple.php => exemple/dhcp_config/exemple.php (75%) create mode 100644 exemple/dl_rss/config/one_piece.json create mode 100644 exemple/dl_rss/dl_rss.php delete mode 100644 one_piece.php diff --git a/.gitignore b/.gitignore index 494f9c4..739ada0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ /composer.phar # Project files +/exemple/dl_rss/app_token /app_token \ No newline at end of file diff --git a/README.md b/README.md index d0a0e9a..5d5c562 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # Freebox v6 PHP API v3 -Implementation PHP de l'API de la freebox. +Implementation PHP de l'API de la freebox (dans sa version 3). ## Prérequis -Ce projet est basé sur composer. Pensez a installer les dependences :) +Ce projet est basé sur **composer**. Pensez à installer les dependences :) ## Utilisation @@ -22,8 +22,10 @@ $App->openSession(); ``` ### Services -Les appels aux services de l'API se font de la maniere suivante : -Voiuci un exemple d'utilisation de l'API System. +Les appels aux services de l'API se font par l'intermédiaire de services. +Ces derniers possedent les méthodes pour récuperer, ajouter ou mettre a jour des données. + +Voici un exemple d'utilisation de l'API System. 1. Nous créons un nouveau service "System" 2. Nous demandons de récuperer la configuration actuelle 3. Nous utilisons le modele retourné pour acceder a la donnée `uptime` @@ -35,4 +37,11 @@ $System = new \alphayax\freebox\api\v3\services\config\System( $App); $SystemConfig = $System->getConfiguration(); \alphayax\utils\cli\IO::stdout( 'Uptime : '. $SystemConfig->uptime); -``` \ No newline at end of file +``` + +### Exemples + +Les exemples sont disponibles dans le repertoire `exemple`: +- `dhcp_config` : Un script pour récuperer la configuration courrante du DHCP +- `dl_rss` : Un script qui parse les flux RSS et qui rajoute en téléchagement les items correspondant a une expression réguliere + \ No newline at end of file diff --git a/exemple.php b/exemple/dhcp_config/exemple.php similarity index 75% rename from exemple.php rename to exemple/dhcp_config/exemple.php index 3012d6e..f5d5790 100644 --- a/exemple.php +++ b/exemple/dhcp_config/exemple.php @@ -1,10 +1,10 @@ authorize(); $App->openSession(); diff --git a/exemple/dl_rss/config/one_piece.json b/exemple/dl_rss/config/one_piece.json new file mode 100644 index 0000000..62b006e --- /dev/null +++ b/exemple/dl_rss/config/one_piece.json @@ -0,0 +1,5 @@ +{ + "rss": "https:\/\/www.nyaa.se\/?page=rss&user=175467", + "pattern": "\/One_Piece.*HD.*mp4\/", + "last_date": 1457259493 +} \ No newline at end of file diff --git a/exemple/dl_rss/dl_rss.php b/exemple/dl_rss/dl_rss.php new file mode 100644 index 0000000..afc860e --- /dev/null +++ b/exemple/dl_rss/dl_rss.php @@ -0,0 +1,50 @@ +authorize(); +$App->openSession(); + +/// Scan files for configs +$config_rfi_s = glob( 'config/*.json'); +foreach( $config_rfi_s as $config_rfi){ + $config = json_decode( file_get_contents( $config_rfi), true); + $config = checkRSS( $config, $App); + file_put_contents( $config_rfi, json_encode( $config, JSON_PRETTY_PRINT)); +} + +/** + * @param $config + * @param $App + * @return array + */ +function checkRSS( $config, $App){ + IO::stdout( "Scan RSS {$config['rss']} for pattern {$config['pattern']}", 0, true, IO::COLOR_YELLOW); + $rss = simplexml_load_file( $config['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( $config['pattern'], $title)){ + if( strtotime( $date) > $config['last_date']){ + $config['last_date'] = strtotime( $date); + + $Downloads = new \alphayax\freebox\api\v3\services\download\Download( $App); + $Downloads->addFromUrl( $link); + + IO::stdout( "Add download $title ($desc)", 0, true, IO::COLOR_GREEN); + } + } + } + return $config; +} + + + + + diff --git a/freebox/api/v3/services/download/Download.php b/freebox/api/v3/services/download/Download.php index c9306bb..5f9e0b2 100644 --- a/freebox/api/v3/services/download/Download.php +++ b/freebox/api/v3/services/download/Download.php @@ -136,11 +136,11 @@ class Download extends Service { } if( ! empty( $archive_password)){ - $params['archive_password'] = $password; + $params['archive_password'] = $archive_password; } if( ! empty( $cookies)){ - $params['cookies'] = $password; + $params['cookies'] = $cookies; } $data = http_build_query( $params); diff --git a/one_piece.php b/one_piece.php deleted file mode 100644 index 8b56971..0000000 --- a/one_piece.php +++ /dev/null @@ -1,48 +0,0 @@ -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)); - - - -