sc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. public function getfilters($page){
  10. return [
  11. "type" => [
  12. "display" => "Type",
  13. "option" => [
  14. "any" => "Any type",
  15. "track" => "Tracks",
  16. "author" => "People",
  17. "album" => "Albums",
  18. "playlist" => "Playlists",
  19. "goplus" => "Go+ Tracks"
  20. ]
  21. ]
  22. ];
  23. }
  24. private function get($proxy, $url, $get = [], $web_req = false){
  25. $curlproc = curl_init();
  26. if($get !== []){
  27. $get = http_build_query($get);
  28. $url .= "?" . $get;
  29. }
  30. curl_setopt($curlproc, CURLOPT_URL, $url);
  31. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  32. // use http2
  33. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  34. if($web_req === false){
  35. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  36. ["User-Agent: " . config::USER_AGENT,
  37. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  38. "Accept-Language: en-US,en;q=0.5",
  39. "Accept-Encoding: gzip",
  40. "Referer: https://soundcloud.com/",
  41. "Origin: https://soundcloud.com",
  42. "DNT: 1",
  43. "Connection: keep-alive",
  44. "Sec-Fetch-Dest: empty",
  45. "Sec-Fetch-Mode: cors",
  46. "Sec-Fetch-Site: same-site"]
  47. );
  48. }else{
  49. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  50. ["User-Agent: " . config::USER_AGENT,
  51. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  52. "Accept-Language: en-US,en;q=0.5",
  53. "Accept-Encoding: gzip",
  54. "DNT: 1",
  55. "Connection: keep-alive",
  56. "Upgrade-Insecure-Requests: 1",
  57. "Sec-Fetch-Dest: document",
  58. "Sec-Fetch-Mode: navigate",
  59. "Sec-Fetch-Site: cross-site",
  60. "Priority: u=1",
  61. "TE: trailers"]
  62. );
  63. }
  64. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  65. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  66. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  67. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  68. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  69. $this->backend->assign_proxy($curlproc, $proxy);
  70. $data = curl_exec($curlproc);
  71. if(curl_errno($curlproc)){
  72. throw new Exception(curl_error($curlproc));
  73. }
  74. curl_close($curlproc);
  75. return $data;
  76. }
  77. public function music($get, $last_attempt = false){
  78. if($get["npt"]){
  79. [$params, $proxy] = $this->backend->get($get["npt"], "music");
  80. $params = json_decode($params, true);
  81. $url = $params["url"];
  82. unset($params["url"]);
  83. }else{
  84. // normal search:
  85. // https://api-v2.soundcloud.com/search?q=freddie%20dredd&variant_ids=&facet=model&user_id=351062-302234-707916-795081&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
  86. // soundcloud go+ search:
  87. // https://api-v2.soundcloud.com/search/tracks?q=freddie%20dredd&variant_ids=&filter.content_tier=SUB_HIGH_TIER&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
  88. // tracks search:
  89. // https://api-v2.soundcloud.com/search/tracks?q=freddie%20dredd&variant_ids=&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
  90. // users search:
  91. // https://api-v2.soundcloud.com/search/users?q=freddie%20dredd&variant_ids=&facet=place&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
  92. // albums search:
  93. // https://api-v2.soundcloud.com/search/albums?q=freddie%20dredd&variant_ids=&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
  94. // playlists search:
  95. // https://api-v2.soundcloud.com/search/playlists_without_albums?q=freddie%20dredd&variant_ids=&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
  96. $search = $get["s"];
  97. if(strlen($search) === 0){
  98. throw new Exception("Search term is empty!");
  99. }
  100. $type = $get["type"];
  101. $proxy = $this->backend->get_ip();
  102. $token = $this->get_token($proxy);
  103. switch($type){
  104. case "any":
  105. $url = "https://api-v2.soundcloud.com/search";
  106. $params = [
  107. "q" => $search,
  108. "variant_ids" => "",
  109. "facet" => "model",
  110. "client_id" => $token,
  111. "limit" => 20,
  112. "offset" => 0,
  113. "linked_partitioning" => 1,
  114. "app_version" => 1713542117,
  115. "app_locale" => "en"
  116. ];
  117. break;
  118. case "track":
  119. $url = "https://api-v2.soundcloud.com/search/tracks";
  120. $params = [
  121. "q" => $search,
  122. "variant_ids" => "",
  123. "facet_genre" => "",
  124. "client_id" => $token,
  125. "limit" => 20,
  126. "offset" => 0,
  127. "linked_partitioning" => 1,
  128. "app_version" => 1713542117,
  129. "app_locale" => "en"
  130. ];
  131. break;
  132. case "author":
  133. $url = "https://api-v2.soundcloud.com/search/users";
  134. $params = [
  135. "q" => $search,
  136. "variant_ids" => "",
  137. "facet" => "place",
  138. "client_id" => $token,
  139. "limit" => 20,
  140. "offset" => 0,
  141. "linked_partitioning" => 1,
  142. "app_version" => 1713542117,
  143. "app_locale" => "en"
  144. ];
  145. break;
  146. case "album":
  147. $url = "https://api-v2.soundcloud.com/search/albums";
  148. $params = [
  149. "q" => $search,
  150. "variant_ids" => "",
  151. "facet" => "genre",
  152. "client_id" => $token,
  153. "limit" => 20,
  154. "offset" => 0,
  155. "linked_partitioning" => 1,
  156. "app_version" => 1713542117,
  157. "app_locale" => "en"
  158. ];
  159. break;
  160. case "playlist":
  161. $url = "https://api-v2.soundcloud.com/search/playlists_without_albums";
  162. $params = [
  163. "q" => $search,
  164. "variant_ids" => "",
  165. "facet" => "genre",
  166. "client_id" => $token,
  167. "limit" => 20,
  168. "offset" => 0,
  169. "linked_partitioning" => 1,
  170. "app_version" => 1713542117,
  171. "app_locale" => "en"
  172. ];
  173. break;
  174. case "goplus":
  175. $url = "https://api-v2.soundcloud.com/search/tracks";
  176. $params = [
  177. "q" => $search,
  178. "variant_ids" => "",
  179. "filter.content_tier" => "SUB_HIGH_TIER",
  180. "facet" => "genre",
  181. "client_id" => $token,
  182. "limit" => 20,
  183. "offset" => 0,
  184. "linked_partitioning" => 1,
  185. "app_version" => 1713542117,
  186. "app_locale" => "en"
  187. ];
  188. break;
  189. }
  190. }
  191. try{
  192. $json = $this->get($proxy, $url, $params);
  193. }catch(Exception $error){
  194. throw new Exception("Failed to fetch JSON");
  195. }
  196. /*
  197. $handle = fopen("scraper/soundcloud.json", "r");
  198. $json = fread($handle, filesize("scraper/soundcloud.json"));
  199. fclose($handle);
  200. */
  201. $json = json_decode($json, true);
  202. if($json === null){
  203. if($last_attempt === true){
  204. throw new Exception("Fetched an invalid token (please report!!)");
  205. }
  206. // token might've expired, get a new one and re-try search
  207. $this->get_token($proxy);
  208. return $this->music($get, true);
  209. }
  210. $out = [
  211. "status" => "ok",
  212. "npt" => null,
  213. "song" => [],
  214. "playlist" => [],
  215. "album" => [],
  216. "podcast" => [],
  217. "author" => [],
  218. "user" => []
  219. ];
  220. /*
  221. Get next page
  222. */
  223. if(isset($json["next_href"])){
  224. $params["query_urn"] = $json["query_urn"];
  225. $params["offset"] = $params["offset"] + 20;
  226. $params["url"] = $url; // we will remove this later
  227. $out["npt"] =
  228. $this->backend->store(
  229. json_encode($params),
  230. "music",
  231. $proxy
  232. );
  233. }
  234. /*
  235. Scrape items
  236. */
  237. foreach($json["collection"] as $item){
  238. switch($item["kind"]){
  239. case "user":
  240. // parse author
  241. $out["author"][] = [
  242. "title" => $item["username"],
  243. "followers" => $item["followers_count"],
  244. "description" => trim($item["track_count"] . " songs. " . $this->limitstrlen($item["description"])),
  245. "thumb" => [
  246. "url" => $item["avatar_url"],
  247. "ratio" => "1:1"
  248. ],
  249. "url" => $item["permalink_url"]
  250. ];
  251. break;
  252. case "playlist":
  253. // parse playlist
  254. $description = [];
  255. $count = 0;
  256. foreach($item["tracks"] as $song){
  257. $count++;
  258. if(!isset($song["title"])){
  259. continue;
  260. }
  261. $description[] = $song["title"];
  262. }
  263. if(count($description) != 0){
  264. $description = trim($count . " songs. " . implode(", ", $description));
  265. }
  266. if(
  267. isset($item["artwork_url"]) &&
  268. !empty($item["artwork_url"])
  269. ){
  270. $thumb = [
  271. "ratio" => "1:1",
  272. "url" => $item["artwork_url"]
  273. ];
  274. }elseif(
  275. isset($item["tracks"][0]["artwork_url"]) &&
  276. !empty($item["tracks"][0]["artwork_url"])
  277. ){
  278. $thumb = [
  279. "ratio" => "1:1",
  280. "url" => $item["tracks"][0]["artwork_url"]
  281. ];
  282. }else{
  283. $thumb = [
  284. "ratio" => null,
  285. "url" => null
  286. ];
  287. }
  288. $out["playlist"][] = [
  289. "title" => $item["title"],
  290. "description" => $this->limitstrlen($description),
  291. "author" => [
  292. "name" => $item["user"]["username"],
  293. "url" => $item["user"]["permalink_url"],
  294. "avatar" => $item["user"]["avatar_url"]
  295. ],
  296. "thumb" => $thumb,
  297. "date" => strtotime($item["created_at"]),
  298. "duration" => $item["duration"] / 1000,
  299. "url" => $item["permalink_url"]
  300. ];
  301. break;
  302. case "track":
  303. if(stripos($item["monetization_model"], "TIER") === false){
  304. $stream = [
  305. "endpoint" => "sc",
  306. "url" =>
  307. $item["media"]["transcodings"][0]["url"] .
  308. "?client_id=" . $token .
  309. "&track_authorization=" .
  310. $item["track_authorization"]
  311. ];
  312. }else{
  313. $stream = [
  314. "endpoint" => null,
  315. "url" => null
  316. ];
  317. }
  318. // parse track
  319. $out["song"][] = [
  320. "title" => $item["title"],
  321. "description" => $item["description"] == "" ? null : $this->limitstrlen($item["description"]),
  322. "url" => $item["permalink_url"],
  323. "views" => $item["playback_count"],
  324. "author" => [
  325. "name" => $item["user"]["username"],
  326. "url" => $item["user"]["permalink_url"],
  327. "avatar" => $item["user"]["avatar_url"]
  328. ],
  329. "thumb" => [
  330. "ratio" => "1:1",
  331. "url" => $item["artwork_url"]
  332. ],
  333. "date" => strtotime($item["created_at"]),
  334. "duration" => (int)$item["full_duration"] / 1000,
  335. "stream" => $stream
  336. ];
  337. break;
  338. }
  339. }
  340. return $out;
  341. }
  342. public function get_token($proxy){
  343. $token = apcu_fetch("sc_token");
  344. if($token !== false){
  345. return $token;
  346. }
  347. // search through all javascript components on the main page
  348. try{
  349. $html =
  350. $this->get(
  351. $proxy,
  352. "https://soundcloud.com",
  353. []
  354. );
  355. }catch(Exception $error){
  356. throw new Exception("Failed to fetch front page");
  357. }
  358. $this->fuckhtml->load($html);
  359. $scripts =
  360. $this->fuckhtml
  361. ->getElementsByTagName(
  362. "script"
  363. );
  364. foreach($scripts as $script){
  365. if(
  366. !isset($script["attributes"]["src"]) ||
  367. strpos($script["attributes"]["src"], "sndcdn.com") === false
  368. ){
  369. continue;
  370. }
  371. try{
  372. $js =
  373. $this->get(
  374. $proxy,
  375. $script["attributes"]["src"],
  376. []
  377. );
  378. }catch(Exception $error){
  379. throw new Exception("Failed to fetch search token");
  380. }
  381. preg_match(
  382. '/client_id=([^"]+)/',
  383. $js,
  384. $token
  385. );
  386. if(isset($token[1])){
  387. apcu_store("sc_token", $token[1]);
  388. return $token[1];
  389. break;
  390. }
  391. }
  392. throw new Exception("Did not find a Soundcloud token in the Javascript blobs");
  393. }
  394. private function limitstrlen($text){
  395. return
  396. explode(
  397. "\n",
  398. wordwrap(
  399. str_replace(
  400. ["\n\r", "\r\n", "\n", "\r"],
  401. " ",
  402. $text
  403. ),
  404. 300,
  405. "\n"
  406. ),
  407. 2
  408. )[0];
  409. }
  410. }