Gestion download au travers de premiumizeme
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

209 lines
6.9 KiB

<?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_CID'),
'params[pass]' => getenv('PREM_PIN'),
'params[link]' => $url
];
$httpFields = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.premiumize.me/api/transfer/directdl');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$curlResult = curl_exec($ch);
curl_close($ch);
$data = json_decode($curlResult);
$retour = get_object_vars($data->content[0]);
return $retour;
//TODO: regérer le reste :)
// 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&eacute;l&eacute;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&ecirc;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() {
# TODO : https://app.swaggerhub.com/apis/premiumize.me/api/1.0.0#/account/accountInfo
# TODO : https://blog.premiumize.me/old-api-phaseout-new-api-changes-and-best-practices-for-the-cache/
# TODO : All requests must include either parameters customer_id and pin (both located at www.premiumize.me/account)
$fields = [
'customer_id' => getenv('PREM_CID'),
'pin' => getenv('PREM_PIN')
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.premiumize.me/api/account/info');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$curlResult = curl_exec($ch);
curl_close($ch);
$data = json_decode($curlResult);
$retour = get_object_vars($data);
$tmpDT = new DateTime();
$tmpDT->setTimestamp($retour['premium_until']);
$retour['premium_until'] = $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;
}
public static function getCache()
{
$fields = [
'customer_id' => getenv('PREM_CID'),
'pin' => getenv('PREM_PIN'),
'items' => ['http://uploaded.net/file/ilrlophk']
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.premiumize.me/api/cache/check');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$curlResult = curl_exec($ch);
curl_close($ch);
$data = json_decode($curlResult);
$retour = get_object_vars($data);
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);
//PremiumizeMe::getCache();
// Pour le moment nous ne gérons que des cas avec un seul fichier retourné
$premInfos = PremiumizeMe::getLink($element->url);
var_dump('test');
if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['path']) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['path'])) {
$element->addEvent(['state' => 'dupp' ]);
$element->updateState('dupp');
return false;
}
// TODO : gérer les erreurs ici !!!!
$element->addEvent([
'state' => 'converted',
'premURL' => $premInfos['link'],
'filename' => $premInfos['path']
]);
var_dump('dl');
$fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['path'], 'w+');
$ch = curl_init($premInfos['link']);
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);
var_dump('end');
$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['path']);
rename(
DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['path'],
DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['path']
);
$element->updateState('downloaded');
return $retour;
}
}