1
0

sc.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. "people" => "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){
  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. switch($type){
  82. case "any":
  83. $url = "https://api-v2.soundcloud.com/search";
  84. $params = [
  85. "q" => $search,
  86. "variant_ids" => "",
  87. "facet" => "model",
  88. "user_id" => config::SC_USER_ID,
  89. "client_id" => config::SC_CLIENT_TOKEN,
  90. "limit" => 20,
  91. "offset" => 0,
  92. "linked_partitioning" => 1,
  93. "app_version" => 1696577813,
  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. "user_id" => config::SC_USER_ID,
  104. "client_id" => config::SC_CLIENT_TOKEN,
  105. "limit" => 20,
  106. "offset" => 0,
  107. "linked_partitioning" => 1,
  108. "app_version" => 1696577813,
  109. "app_locale" => "en"
  110. ];
  111. break;
  112. case "people":
  113. $url = "https://api-v2.soundcloud.com/search/users";
  114. $params = [
  115. "q" => $search,
  116. "variant_ids" => "",
  117. "facet" => "place",
  118. "user_id" => config::SC_USER_ID,
  119. "client_id" => config::SC_CLIENT_TOKEN,
  120. "limit" => 20,
  121. "offset" => 0,
  122. "linked_partitioning" => 1,
  123. "app_version" => 1696577813,
  124. "app_locale" => "en"
  125. ];
  126. break;
  127. case "album":
  128. $url = "https://api-v2.soundcloud.com/search/albums";
  129. $params = [
  130. "q" => $search,
  131. "variant_ids" => "",
  132. "facet" => "genre",
  133. "user_id" => config::SC_USER_ID,
  134. "client_id" => config::SC_CLIENT_TOKEN,
  135. "limit" => 20,
  136. "offset" => 0,
  137. "linked_partitioning" => 1,
  138. "app_version" => 1696577813,
  139. "app_locale" => "en"
  140. ];
  141. break;
  142. case "playlist":
  143. $url = "https://api-v2.soundcloud.com/search/playlists_without_albums";
  144. $params = [
  145. "q" => $search,
  146. "variant_ids" => "",
  147. "facet" => "genre",
  148. "user_id" => config::SC_USER_ID,
  149. "client_id" => config::SC_CLIENT_TOKEN,
  150. "limit" => 20,
  151. "offset" => 0,
  152. "linked_partitioning" => 1,
  153. "app_version" => 1696577813,
  154. "app_locale" => "en"
  155. ];
  156. break;
  157. case "goplus":
  158. $url = "https://api-v2.soundcloud.com/search/tracks";
  159. $params = [
  160. "q" => $search,
  161. "variant_ids" => "",
  162. "filter.content_tier" => "SUB_HIGH_TIER",
  163. "facet" => "genre",
  164. "user_id" => config::SC_USER_ID,
  165. "client_id" => config::SC_CLIENT_TOKEN,
  166. "limit" => 20,
  167. "offset" => 0,
  168. "linked_partitioning" => 1,
  169. "app_version" => 1696577813,
  170. "app_locale" => "en"
  171. ];
  172. break;
  173. }
  174. }
  175. try{
  176. $json = $this->get($proxy, $url, $params);
  177. }catch(Exception $error){
  178. throw new Exception("Failed to fetch JSON");
  179. }
  180. /*
  181. $handle = fopen("scraper/soundcloud.json", "r");
  182. $json = fread($handle, filesize("scraper/soundcloud.json"));
  183. fclose($handle);
  184. */
  185. $json = json_decode($json, true);
  186. if($json === null){
  187. throw new Exception("Failed to decode JSON");
  188. }
  189. $out = [
  190. "status" => "ok",
  191. "npt" => null,
  192. "song" => [],
  193. "playlist" => [],
  194. "author" => []
  195. ];
  196. /*
  197. Get next page
  198. */
  199. if(isset($json["next_href"])){
  200. $params["query_urn"] = $json["query_urn"];
  201. $params["offset"] = $params["offset"] + 20;
  202. $params["url"] = $url; // we will remove this later
  203. $out["npt"] =
  204. $this->backend->store(
  205. json_encode($params),
  206. "music",
  207. $proxy
  208. );
  209. }
  210. /*
  211. Scrape items
  212. */
  213. foreach($json["collection"] as $item){
  214. switch($item["kind"]){
  215. case "user":
  216. // parse author
  217. $out["author"][] = [
  218. "title" => $item["username"],
  219. "followers" => $item["followers_count"],
  220. "description" => trim($item["track_count"] . " songs. " . $this->limitstrlen($item["description"])),
  221. "thumb" => [
  222. "url" => $item["avatar_url"],
  223. "ratio" => "1:1"
  224. ],
  225. "url" => $item["permalink_url"]
  226. ];
  227. break;
  228. case "playlist":
  229. // parse playlist
  230. $description = [];
  231. $count = 0;
  232. foreach($item["tracks"] as $song){
  233. $count++;
  234. if(!isset($song["title"])){
  235. continue;
  236. }
  237. $description[] = $song["title"];
  238. }
  239. if(count($description) != 0){
  240. $description = trim($count . " songs. " . implode(", ", $description));
  241. }
  242. if(
  243. isset($item["artwork_url"]) &&
  244. !empty($item["artwork_url"])
  245. ){
  246. $thumb = [
  247. "ratio" => "1:1",
  248. "url" => $item["artwork_url"]
  249. ];
  250. }elseif(
  251. isset($item["tracks"][0]["artwork_url"]) &&
  252. !empty($item["tracks"][0]["artwork_url"])
  253. ){
  254. $thumb = [
  255. "ratio" => "1:1",
  256. "url" => $item["tracks"][0]["artwork_url"]
  257. ];
  258. }else{
  259. $thumb = [
  260. "ratio" => null,
  261. "url" => null
  262. ];
  263. }
  264. $out["playlist"][] = [
  265. "title" => $item["title"],
  266. "description" => $this->limitstrlen($description),
  267. "author" => [
  268. "name" => $item["user"]["username"],
  269. "url" => $item["user"]["permalink_url"],
  270. "avatar" => $item["user"]["avatar_url"]
  271. ],
  272. "thumb" => $thumb,
  273. "date" => strtotime($item["created_at"]),
  274. "duration" => $item["duration"] / 1000,
  275. "url" => $item["permalink_url"]
  276. ];
  277. break;
  278. case "track":
  279. if(stripos($item["monetization_model"], "TIER") === false){
  280. $stream = [
  281. "endpoint" => "audio_sc",
  282. "url" =>
  283. $item["media"]["transcodings"][0]["url"] .
  284. "?client_id=" . config::SC_CLIENT_TOKEN .
  285. "&track_authorization=" .
  286. $item["track_authorization"]
  287. ];
  288. }else{
  289. $stream = [
  290. "endpoint" => null,
  291. "url" => null
  292. ];
  293. }
  294. // parse track
  295. $out["song"][] = [
  296. "title" => $item["title"],
  297. "description" => $item["description"] == "" ? null : $this->limitstrlen($item["description"]),
  298. "url" => $item["permalink_url"],
  299. "views" => $item["playback_count"],
  300. "author" => [
  301. "name" => $item["user"]["username"],
  302. "url" => $item["user"]["permalink_url"],
  303. "avatar" => $item["user"]["avatar_url"]
  304. ],
  305. "thumb" => [
  306. "ratio" => "1:1",
  307. "url" => $item["artwork_url"]
  308. ],
  309. "date" => strtotime($item["created_at"]),
  310. "duration" => (int)$item["full_duration"] / 1000,
  311. "stream" => $stream
  312. ];
  313. break;
  314. }
  315. }
  316. return $out;
  317. }
  318. private function limitstrlen($text){
  319. return
  320. explode(
  321. "\n",
  322. wordwrap(
  323. str_replace(
  324. ["\n\r", "\r\n", "\n", "\r"],
  325. " ",
  326. $text
  327. ),
  328. 300,
  329. "\n"
  330. ),
  331. 2
  332. )[0];
  333. }
  334. }