|
|
<?php
class PremiumizeMe { /** * Converti le lien d'un hoster en un lien premiumizeme + informations * * @param string $url Url d'un hoster * * @return array[string]mixed Informations sur le lien premiumizeme */ static function getLink($url) { $fields = [ 'method' => 'directdownloadlink', 'params[login]' => getenv('PREM_LOGIN'), 'params[pass]' => getenv('PREM_PASS'), 'params[link]' => $url ]; $httpFields = http_build_query($fields); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $curlResult = curl_exec($ch); curl_close($ch); $data = json_decode($curlResult); // TODO : exception et non exit bourrin
if (400 === $data->status) { echo 'Erreur : Je ne supporte pas de fournisseur, merci de revenir a la page des téléchargement et d\'en choisir un autre.<br><br>Erreur exacte :<br>'.$data->statusmessage; exit(); } if (null === $data->result) { if (503 === $data->status) { echo 'Erreur : Il semble que le lien ne fonctionne plus.<br>Vous pouvez verifier vous-même ici : <a href="'.$url.'">'.$url.'</a><br><br>'; echo 'Erreur exacte :<br>'.$data->statusmessage; } else { echo 'Erreur : <br>'.$data->statusmessage; } exit(); } else { $retour = get_object_vars($data->result); return $retour; } }
/** * Retourne les informations sur l'etat actuel du compte. * * @return array[string]mixed */ static function getStatus() { $fields = [ 'method' => 'accountstatus', 'params[login]' => getenv('PREM_LOGIN'), 'params[pass]' => getenv('PREM_PASS') ]; $httpFields = http_build_query($fields); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $curlResult = curl_exec($ch); curl_close($ch); $data = json_decode($curlResult); $retour = get_object_vars($data->result); $tmpDT = new DateTime(); $tmpDT->setTimestamp($retour['expires']); $retour['expires'] = $tmpDT; return $retour; } /** * Retourne la liste des hosters ainsi que les regexps lie. * * @return array[mixed]mixed */ static function getHosters() { $fields = [ 'method' => 'hosterlist', 'params[login]' => getenv('PREM_LOGIN'), 'params[pass]' => getenv('PREM_PASS') ]; $httpFields = http_build_query($fields); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $curlResult = curl_exec($ch); curl_close($ch); $data = json_decode($curlResult); $retour = get_object_vars($data->result);
foreach($retour['hosterlist'] as $hoster) { $retour['hostershort'][] = strstr($hoster, '.', true); } return $retour; }
/** * Telecharge le fichier * * @return array Informations sur le transfert */ public static function get(Download $element) { // On le creer tout de suite pour eviter des conflits de process
touch(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$element->filename);
$premInfos = PremiumizeMe::getLink($element->url);
if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename']) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename'])) { $element->addEvent(['state' => 'dupp' ]); $element->updateState('dupp'); return false; }
// TODO : gérer les erreurs ici !!!!
$element->addEvent([ 'state' => 'converted', 'premURL' => $premInfos['location'], 'filename' => $premInfos['filename'] ]);
$fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'], 'w+'); $ch = curl_init($premInfos['location']);
curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT_OUT); curl_exec($ch); $retour = curl_getinfo($ch); curl_close($ch); fclose($fp); $element->updateState('downloaded'); $element->addEvent([ 'state' => 'downloaded', 'curlinfo' => [ 'http_code' => $retour['http_code'], 'total_time' => $retour['total_time'], 'namelookup_time' => $retour['namelookup_time'], 'connect_time' => $retour['connect_time'], 'pretransfer_time' => $retour['pretransfer_time'], 'size_download' => $retour['size_download'], 'speed_download' => $retour['speed_download'], 'starttransfer_time' => $retour['starttransfer_time'] ] ]);
if (200 !== $retour['http_code']) { $element->updateState('error'); return false; }
$element->updateFilename($premInfos['filename']); rename( DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'], DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename'] ); $element->updateState('downloaded'); return $retour; }
}
|