Download API exemple

This commit is contained in:
alphayax 2016-03-06 20:33:56 +01:00
parent 36d7069512
commit 5de23258c6
7 changed files with 74 additions and 57 deletions

1
.gitignore vendored
View File

@ -6,4 +6,5 @@
/composer.phar
# Project files
/exemple/dl_rss/app_token
/app_token

View File

@ -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);
```
```
### 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

View File

@ -1,10 +1,10 @@
<?php
/// Require Composer AutoLoader
require_once 'vendor/autoload.php';
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 = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Example', '0.0.2');
$App->authorize();
$App->openSession();

View File

@ -0,0 +1,5 @@
{
"rss": "https:\/\/www.nyaa.se\/?page=rss&user=175467",
"pattern": "\/One_Piece.*HD.*mp4\/",
"last_date": 1457259493
}

50
exemple/dl_rss/dl_rss.php Normal file
View File

@ -0,0 +1,50 @@
<?php
use alphayax\utils\cli\IO;
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Example', '0.0.2');
$App->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;
}

View File

@ -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);

View File

@ -1,48 +0,0 @@
<?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));