Last active
August 29, 2015 14:17
-
-
Save lethak/9702c13b66561bd059dd to your computer and use it in GitHub Desktop.
Kimsufi JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author https://github.com/lethak | |
* Usage: | |
php dispo.php --vlc "/home/myuser/Music/GameofThronesThemeMetal.mp3" --longpoll 1 --ref "150sk30" -v=1 | |
*/ | |
// Script Default Arguments... | |
$defaultArgs = array( | |
'ref'=>null, //'150sk30', (null = all) | |
'asJson'=>false, | |
'vlc'=>null, // path to some mp3 file to be played by VLC if there is some availability instead for php/json output | |
'vlcexec'=>true, | |
'longpoll'=>false, | |
'v'=>false, //verbose | |
); | |
// Script Argument Building... | |
$params = array(); | |
$argKey = null; | |
$argValue = null; | |
foreach ($_SERVER['argv'] as $argv) | |
{ | |
if($argKey!==null && ( strlen(''.$argv)>2 && substr(''.$argv, 0,2)=="--" || strlen(''.$argv)>1 && substr(''.$argv, 0,1)=="-" )) | |
$argKey=null; | |
if($argKey==null && strlen(''.$argv)>2 && substr(''.$argv, 0,2)=="--") | |
{ | |
$argKey = substr(''.$argv, 2); | |
$eqPos = strpos($argKey, '='); | |
if($eqPos!==false) | |
{ | |
$argKey=substr(''.$argv,2,$eqPos); | |
$argValue=substr(''.$argv, $eqPos+3); | |
} | |
$params[$argKey] = ''.$argValue; | |
if($argValue!==null) $argKey=null; | |
$argValue=null; | |
} | |
elseif($argKey==null && strlen(''.$argv)>1 && substr(''.$argv, 0,1)=="-") | |
{ | |
$argKey = substr(''.$argv, 1); | |
$eqPos = strpos($argKey, '='); | |
if($eqPos!==false) | |
{ | |
$argKey=substr(''.$argv,1,$eqPos); | |
$argValue=substr(''.$argv, $eqPos+2); | |
} | |
$params[$argKey] = ''.$argValue; | |
if($argValue!==null) $argKey=null; | |
$argValue=null; | |
} | |
elseif($argKey!==null && $argValue===null && substr(''.$argv,0,1)!=="-") | |
{ | |
$argValue = ''.$argv; | |
$params[$argKey] = $argValue; | |
$argKey=$argValue=null; | |
} | |
} | |
foreach ($defaultArgs as $arg=>$defaultValue) | |
{ | |
if(!array_key_exists($arg, $params)) | |
$params[$arg] = $defaultValue; | |
} | |
if(!is_array($params['ref'])) | |
$params['ref']=explode(',', ''.$params['ref']); | |
foreach ($params['ref'] as $key => $value) | |
{ | |
if($value===null || trim(''.$value)=="") | |
{ | |
$params['ref'][$key]=null; | |
unset($params['ref'][$key]); | |
} | |
} | |
// Querying OVH... | |
function file_get_contents_curl($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return $data; | |
} | |
$iWhile = 0; | |
$continuePolling=true; | |
$maxIWhile = 86400; | |
while ((!$params['longpoll'] && $iWhile===0) || ($params['longpoll'] && $continuePolling)) | |
{ | |
if($iWhile!==0) | |
sleep(8); //max 500 query per 3600s | |
$url = 'https://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2?callback='; | |
$html = trim(''.file_get_contents_curl($url), ' ()'); | |
$json = json_decode($html, 1); | |
$dispo = array(); | |
if(is_array($json)) | |
{ | |
if($json['error']===null) | |
{ | |
$error = ''; | |
foreach($json['answer']['availability'] as $item) | |
{ | |
if(count($params['ref'])<=0 || in_array($item['reference'], $params['ref'])) | |
{ | |
foreach ($item['metaZones'] as $zone) | |
{ | |
if($zone['availability']!=="unknown" && $zone['availability']!=="unavailable") | |
$dispo[$item['reference']][$zone['zone']] = $zone['availability']; | |
} | |
foreach ($item['zones'] as $zone) | |
{ | |
if($zone['availability']!=="unknown" && $zone['availability']!=="unavailable") | |
$dispo[$item['reference']][$zone['zone']] = $zone['availability']; | |
} | |
} | |
} | |
} | |
else | |
{ | |
$error=' => ERROR:'.$json['error']['message']; | |
} | |
} | |
$iWhile++; | |
if(count($dispo)>0 || $iWhile>=$maxIWhile) | |
$continuePolling=false; | |
if ($continuePolling && $params['v']) | |
echo(PHP_EOL.'['.date('Y-m-d H:i:s').'] ('.(($iWhile%2)?".":"-").((!($iWhile%2))?".":"-").') No dispo for '.implode(',',$params['ref']).$error); | |
} | |
if($params['asJson']) | |
echo json_encode($dispo); | |
if(count($dispo)>0 && $params['v']) | |
echo(print_r($dispo, true)); | |
if(!$params['asJson'] && $params['vlc']!==null) | |
{ | |
if(count($dispo)>0) | |
{ | |
$vlc = "vlc ".$params['vlc']." -q"; // | |
if(!$params['vlcexec']) | |
echo $vlc; | |
else | |
exec($vlc); | |
} | |
} | |
else | |
echo print_r($dispo, true); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment