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.

207 lines
6.8 KiB

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