sc.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. class sc{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("sc");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. private function get($proxy, $url, $get = []){
  10. $curlproc = curl_init();
  11. if($get !== []){
  12. $get = http_build_query($get);
  13. $url .= "?" . $get;
  14. }
  15. curl_setopt($curlproc, CURLOPT_URL, $url);
  16. // http2 bypass
  17. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  18. $headers =
  19. ["User-Agent: " . config::USER_AGENT,
  20. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  21. "Accept-Language: en-US,en;q=0.5",
  22. "Accept-Encoding: gzip",
  23. "DNT: 1",
  24. "Sec-GPC: 1",
  25. "Connection: keep-alive",
  26. "Upgrade-Insecure-Requests: 1",
  27. "Sec-Fetch-Dest: document",
  28. "Sec-Fetch-Mode: navigate",
  29. "Sec-Fetch-Site: none",
  30. "Sec-Fetch-User: ?1",
  31. "Priority: u=0, i"];
  32. $this->backend->assign_proxy($curlproc, $proxy);
  33. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  34. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  35. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  36. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  37. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  38. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  39. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  40. $data = curl_exec($curlproc);
  41. if(curl_errno($curlproc)){
  42. throw new Exception(curl_error($curlproc));
  43. }
  44. curl_close($curlproc);
  45. return $data;
  46. }
  47. function resolve($id){
  48. if(
  49. !preg_match(
  50. '/^(t|p)[0-9]+$/',
  51. $id
  52. )
  53. ){
  54. throw new Exception("ID is invalid");
  55. }
  56. $type = $id[0] == "t" ? "track" : "playlist";
  57. $id = substr($id, 1);
  58. try{
  59. $html =
  60. $this->get(
  61. $this->backend->get_ip(),
  62. "https://w.soundcloud.com/player/",
  63. [
  64. "url" => "http://api.soundcloud.com/{$type}s/{$id}",
  65. "show_artwork" => "true"
  66. ]
  67. );
  68. }catch(Exception $error){
  69. throw new Exception("Failed to fetch song embed page");
  70. }
  71. $this->fuckhtml->load($html);
  72. $link =
  73. $this->fuckhtml
  74. ->getElementsByAttributeValue(
  75. "rel",
  76. "canonical",
  77. "link"
  78. );
  79. if(count($link) === 0){
  80. throw new Exception("Soundcloud could not resolve the song ID to an URL");
  81. }
  82. return
  83. $this->fuckhtml
  84. ->getTextContent(
  85. $link[0]
  86. ["attributes"]
  87. ["href"]
  88. );
  89. }
  90. }