sc.php 11 KB

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