1
0

swisscows.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. class swisscows{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("swisscows");
  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. "track" => "Tracks",
  15. "playlist" => "Playlists"
  16. ]
  17. ]
  18. ];
  19. }
  20. private function get($proxy, $url, $get = [], $web_req = false){
  21. $curlproc = curl_init();
  22. if($get !== []){
  23. $get = http_build_query($get);
  24. $url .= "?" . $get;
  25. }
  26. curl_setopt($curlproc, CURLOPT_URL, $url);
  27. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  28. // use http2
  29. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  30. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  31. ["User-Agent: " . config::USER_AGENT,
  32. "Accept: */*",
  33. "Accept-Language: en-US,en;q=0.5",
  34. "Accept-Encoding: gzip, deflate, br, zstd",
  35. "Access-Control-Request-Method: GET",
  36. "Access-Control-Request-Headers: cache-control",
  37. "Referer: https://swisscows.com/",
  38. "Origin: https://swisscows.com",
  39. "DNT: 1",
  40. "Sec-GPC: 1",
  41. "Connection: keep-alive",
  42. "Sec-Fetch-Dest: empty",
  43. "Sec-Fetch-Mode: cors",
  44. "Sec-Fetch-Site: same-site",
  45. "Priority: u=4",
  46. "TE: trailers"]
  47. );
  48. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  49. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  50. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  51. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  52. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  53. $this->backend->assign_proxy($curlproc, $proxy);
  54. $data = curl_exec($curlproc);
  55. if(curl_errno($curlproc)){
  56. throw new Exception(curl_error($curlproc));
  57. }
  58. curl_close($curlproc);
  59. return $data;
  60. }
  61. public function music($get, $last_attempt = false){
  62. if($get["npt"]){
  63. [$params, $proxy] = $this->backend->get($get["npt"], "music");
  64. $params = json_decode($params, true);
  65. $type = $params["type"];
  66. $search = $params["s"];
  67. $offset = $params["offset"];
  68. }else{
  69. $search = $get["s"];
  70. if(strlen($search) === 0){
  71. throw new Exception("Search term is empty!");
  72. }
  73. $type = $get["type"];
  74. $offset = 0;
  75. $proxy = $this->backend->get_ip();
  76. }
  77. try{
  78. $json =
  79. $this->get(
  80. $proxy,
  81. "https://api.swisscows.com/audio/search/{$type}s",
  82. [
  83. "query" => $search,
  84. "offset" => $offset,
  85. "itemsCount" => 20,
  86. "region" => "en-US"
  87. ]
  88. );
  89. }catch(Exception $error){
  90. throw new Exception("Failed to fetch JSON");
  91. }
  92. $json = json_decode($json, true);
  93. if($json === null){
  94. throw new Exception("Failed to decode JSON");
  95. }
  96. $out = [
  97. "status" => "ok",
  98. "npt" => null,
  99. "song" => [],
  100. "playlist" => [],
  101. "album" => [],
  102. "podcast" => [],
  103. "author" => [],
  104. "user" => []
  105. ];
  106. if(!isset($json["items"])){
  107. throw new Exception("Swisscows did not return an items object");
  108. }
  109. if($type == "track"){
  110. foreach($json["items"] as $item){
  111. $tags = $item["tags"];
  112. if(!empty($item["genre"])){
  113. $tags[] = $item["genre"];
  114. }
  115. $out["song"][] = [
  116. "title" => $item["title"],
  117. "description" => implode(", ", $tags),
  118. "url" => "/resolver?scraper=sc&target=t{$item["id"]}",
  119. "views" => null,
  120. "author" => [
  121. "name" => null,
  122. "url" => null,
  123. "avatar" => null
  124. ],
  125. "thumb" => [
  126. "ratio" => "1:1",
  127. "url" => $item["artworkUrl"]
  128. ],
  129. "date" => null,
  130. "duration" => $this->convert_time($item["duration"]),
  131. "stream" => [
  132. "endpoint" => null,
  133. "url" => null
  134. ]
  135. ];
  136. }
  137. }else{
  138. foreach($json["items"] as $item){
  139. $out["playlist"][] = [
  140. "title" => $item["title"],
  141. "description" => $this->limitstrlen($item["description"]),
  142. "author" => [
  143. "name" => null,
  144. "url" => null,
  145. "avatar" => null
  146. ],
  147. "thumb" => [
  148. "ratio" => "1:1",
  149. "url" => $item["artworkUrl"]
  150. ],
  151. "date" => null,
  152. "duration" => $this->convert_time($item["duration"]),
  153. "url" => "/resolver?scraper=sc&target=p{$item["id"]}",
  154. ];
  155. }
  156. }
  157. //
  158. // get NPT
  159. //
  160. if(
  161. isset($json["nextOffset"]) &&
  162. $json["nextOffset"] !== null
  163. ){
  164. $out["npt"] =
  165. $this->backend->store(
  166. json_encode(
  167. [
  168. "type" => $type,
  169. "s" => $search,
  170. "offset" => $json["nextOffset"]
  171. ]
  172. ),
  173. "music",
  174. $proxy
  175. );
  176. }
  177. return $out;
  178. }
  179. private function limitstrlen($text){
  180. return
  181. explode(
  182. "\n",
  183. wordwrap(
  184. str_replace(
  185. ["\n\r", "\r\n", "\n", "\r"],
  186. " ",
  187. $text
  188. ),
  189. 300,
  190. "\n"
  191. ),
  192. 2
  193. )[0];
  194. }
  195. private function convert_time($time){
  196. list($hours, $minutes, $seconds) = explode(':', $time);
  197. return
  198. ((int)$hours * 3600) +
  199. ((int)$minutes * 60) +
  200. (float)$seconds;
  201. }
  202. }