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.

177 lines
5.9 KiB

6 years ago
1 year ago
6 years ago
6 years ago
4 years ago
6 years ago
1 year ago
6 years ago
1 year ago
1 year ago
6 years ago
1 year ago
6 years ago
1 year ago
1 year ago
1 year ago
6 years ago
1 year ago
6 years ago
6 years ago
6 years ago
1 year ago
6 years ago
  1. <?php
  2. class PremiumizeMe {
  3. /**
  4. * Converti le lien d'un hoster en un lien premiumizeme + informations
  5. *
  6. * @param string $url Url d'un hoster
  7. *
  8. * @return array[string]mixed Informations sur le lien premiumizeme
  9. */
  10. static function getLink($url) {
  11. $fields = [
  12. 'method' => 'directdownloadlink',
  13. 'params[login]' => getenv('PREM_CID'),
  14. 'params[pass]' => getenv('PREM_PIN'),
  15. 'params[link]' => $url
  16. ];
  17. $httpFields = http_build_query($fields);
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. $curlResult = curl_exec($ch);
  22. curl_close($ch);
  23. $data = json_decode($curlResult);
  24. // TODO : exception et non exit bourrin
  25. if (400 === $data->status) {
  26. 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;
  27. exit();
  28. }
  29. if (null === $data->result) {
  30. if (503 === $data->status) {
  31. 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>';
  32. echo 'Erreur exacte :<br>'.$data->statusmessage;
  33. } else {
  34. echo 'Erreur : <br>'.$data->statusmessage;
  35. }
  36. exit();
  37. } else {
  38. $retour = get_object_vars($data->result);
  39. return $retour;
  40. }
  41. }
  42. /**
  43. * Retourne les informations sur l'etat actuel du compte.
  44. *
  45. * @return array[string]mixed
  46. */
  47. static function getStatus() {
  48. # TODO : https://app.swaggerhub.com/apis/premiumize.me/api/1.0.0#/account/accountInfo
  49. # TODO : https://blog.premiumize.me/old-api-phaseout-new-api-changes-and-best-practices-for-the-cache/
  50. # TODO : All requests must include either parameters customer_id and pin (both located at www.premiumize.me/account)
  51. $fields = [
  52. 'customer_id' => getenv('PREM_CID'),
  53. 'pin' => getenv('PREM_PIN')
  54. ];
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, 'https://www.premiumize.me/api/account/info');
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58. curl_setopt($ch, CURLOPT_POST, 1);
  59. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  60. $curlResult = curl_exec($ch);
  61. curl_close($ch);
  62. $data = json_decode($curlResult);
  63. $retour = get_object_vars($data);
  64. $tmpDT = new DateTime();
  65. $tmpDT->setTimestamp($retour['premium_until']);
  66. $retour['premium_until'] = $tmpDT;
  67. return $retour;
  68. }
  69. /**
  70. * Retourne la liste des hosters ainsi que les regexps lie.
  71. *
  72. * @return array[mixed]mixed
  73. */
  74. static function getHosters() {
  75. $fields = [
  76. 'method' => 'hosterlist',
  77. 'params[login]' => getenv('PREM_LOGIN'),
  78. 'params[pass]' => getenv('PREM_PASS')
  79. ];
  80. $httpFields = http_build_query($fields);
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  84. $curlResult = curl_exec($ch);
  85. curl_close($ch);
  86. $data = json_decode($curlResult);
  87. $retour = get_object_vars($data->result);
  88. foreach($retour['hosterlist'] as $hoster) {
  89. $retour['hostershort'][] = strstr($hoster, '.', true);
  90. }
  91. return $retour;
  92. }
  93. /**
  94. * Telecharge le fichier
  95. *
  96. * @return array Informations sur le transfert
  97. */
  98. public static function get(Download $element)
  99. {
  100. // On le creer tout de suite pour eviter des conflits de process
  101. touch(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$element->filename);
  102. var_dump($premInfos);
  103. exit();
  104. $premInfos = PremiumizeMe::getLink($element->url);
  105. if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename']) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename'])) {
  106. $element->addEvent(['state' => 'dupp' ]);
  107. $element->updateState('dupp');
  108. return false;
  109. }
  110. // TODO : gérer les erreurs ici !!!!
  111. $element->addEvent([
  112. 'state' => 'converted',
  113. 'premURL' => $premInfos['location'],
  114. 'filename' => $premInfos['filename']
  115. ]);
  116. $fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'], 'w+');
  117. $ch = curl_init($premInfos['location']);
  118. curl_setopt($ch, CURLOPT_FILE, $fp);
  119. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  122. curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT_OUT);
  123. curl_exec($ch);
  124. $retour = curl_getinfo($ch);
  125. curl_close($ch);
  126. fclose($fp);
  127. $element->updateState('downloaded');
  128. $element->addEvent([
  129. 'state' => 'downloaded',
  130. 'curlinfo' => [
  131. 'http_code' => $retour['http_code'],
  132. 'total_time' => $retour['total_time'],
  133. 'namelookup_time' => $retour['namelookup_time'],
  134. 'connect_time' => $retour['connect_time'],
  135. 'pretransfer_time' => $retour['pretransfer_time'],
  136. 'size_download' => $retour['size_download'],
  137. 'speed_download' => $retour['speed_download'],
  138. 'starttransfer_time' => $retour['starttransfer_time']
  139. ]
  140. ]);
  141. if (200 !== $retour['http_code']) {
  142. $element->updateState('error');
  143. return false;
  144. }
  145. $element->updateFilename($premInfos['filename']);
  146. rename(
  147. DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'],
  148. DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename']
  149. );
  150. $element->updateState('downloaded');
  151. return $retour;
  152. }
  153. }